Exam  II - Lab                                                                                   Name:

Computer Graphics – April 15, 2009

 Submit each of the programs as you will complete them

 

1. (30 pts)

The following program creates the breaks shown in Figure (1). Modify the program to generate the brick pattern shown in Figure (2). Please note that I am looking for the exact number of blocks and the same exact pattern.

 

#include <GL/glut.h>

typedef GLfloat point3[3];

point3 p[8]={

        {0,0,0},  //black

        {0,0,1},  //blue

        {1,0,1},  //magenta

        {1,0,0},  //red

        {0,1,0},  //green

        {0,1,1},  //green+blue

        {1,1,1},  //white

        {1,1,0}   //yellow

};

 

void drawPoint(int i){

        glColor3fv(p[i]);

        glVertex3fv(p[i]);

}

void drawFace(int i1,int i2,int i3,int i4){

        glBegin(GL_POLYGON);

        drawPoint(i1);

        drawPoint(i2);

        drawPoint(i3);

        drawPoint(i4);

        glEnd();

}

       

 

void myinit(void)

{

 

/* attributes */

        glClearColor(1.0, 1.0, 1.0, 1.0); /* white background */

        glColor3f(0.0, 0.0, 0.0); /* draw in black */

/* set up viewing */

        glMatrixMode(GL_PROJECTION);

        glLoadIdentity();

        glOrtho(-1, 1, -1, 1,-10,10);

        glMatrixMode(GL_MODELVIEW);

        glEnable(GL_DEPTH_TEST);

}

void drawCube(){

        drawFace(0,3,2,1); //bottom = black,

        drawFace(1,2,6,5); //front = blue

        drawFace(2,3,7,6); //right side = magenta

        drawFace(3,0,4,7); //back side = red

        drawFace(4,5,6,7); //top = green

        drawFace(5,4,0,1); //left side = green + blue

}

 

void drawBrick(){

        glPushMatrix();

        glScalef(.5,.125,.25);

        glTranslatef(-.5,-.5,-.5);

        drawCube();

        glPopMatrix();

}

                        

void display( void )

{

        glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);  /*clear the window */

        glMatrixMode(GL_MODELVIEW);

        glLoadIdentity();

        glRotatef(20,1,0,0);

        glRotatef(20,0,1,0);

        glPushMatrix();

 

        // draw two bricks in same row

        drawBrick();

        glTranslatef(.6,0,0);

        drawBrick();

 

        //return to start of row, go up one brick and over half a brick

        glPopMatrix();

        glTranslatef(.3,.14,0);

        drawBrick();

    glFlush(); /* clear buffers */

 }

 

 

void main(int argc, char** argv)

{

/* Standard GLUT initialization */

        glutInit(&argc,argv);

        glutInitDisplayMode (GLUT_SINGLE| GLUT_RGB|GLUT_DEPTH);

 

        glutInitWindowSize(500,500); /* 500 x 500 pixel window */

        glutInitWindowPosition(0,0); /* place window top left on display */

        glutCreateWindow("Test"); /* window title */

        glutDisplayFunc(display); /* display callback invoked when window opened */

        myinit(); /* set attributes */

        glutMainLoop(); /* enter event loop */

}

 

 

Figure (1) – This is the pattern that is generated by the program given

 

 

 

Figure (2) – This is the pattern that your program will generate

 

 

 

 

 

 

 

 

 

 

2. (70 pts) – Draw an Object and its Shadow

Draw a cubical, 100 units wide, 100 units deep, and 200 units high at the center of your screen.  Then create its shadow with respect to a light source located at 100 units to left edge of the box, 200 units above the top of the cubical, and z = 0.