Lab (2) - Graphics

To be conducted on the week of April 1-5
Post Lab Due: Monday April 11, 2002

We discussed the procedure for implementing the cube.Now let's look at scaling and translating of our cube to make "bricks". We'll need to call glPushMatrix() and glPopMatrix() to save and restore the ModelView matrix, so that we can get back to a previous position from which some new offset is desired. Here's a compound example:

#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 */
}
Here's the image:


 

Post-Lab

Your assignment due on April 11, 2002 is to generate an image similar to this:

Email the program to me before class on Monday April 11, and turn in hardcopy in class as well.