# Post-Lab for Lab (4), Assignment (6) - Graphics

Due: 5:00 pm - Wed. March 16, 2005

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);
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);
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:

With some modifications you can create the following image.

If you correctly produce the above wall (pay attention to number of rows and the setting) you will get 60% of the grade.  To receive the full 100% grade, modify your program to create a castle that looks like the image shown below.

Note that the sides actually look something like this in larger scale.