/* In this lab, we define the color cube. You will find a related discussion in Section 4.5 (pg. 187 - 195) in the textbook. Also note that we do not call gluLookAt anywhere, we just take the default. Finally, notice that we're using the "v" call for vertices, since we've defined each vertex in an array. PAY VERY CLOSE ATTENTION TO HOW THE ROTATIONS ARE HANDLED, you only need to update the calls in display(). To Do: 1. define the color cube with vertex definitions and calls to quad 2. rotate the cube about the x, y, or z axis 3. demo your working code We will discus the questions below in class. You do NOT need to turn these in. --------------------------------------------------------------------------------- How would you define an entire face to have one color, rather than having a color for each vertex? How would you render axes which are fixed, don't move, and are in the coordinate system of the camera view? If you render axes which are fixed to the coordinate system of the camera view, and you use the default camera position, can you see the z-axis? Why? How would you render axes which follow the cube? */ #include #include // just a convenience, since each point has three coordinates typedef GLfloat point3[3]; point3 vertices[8] = { { , , } , { , , } , { , , } , { , , } , { , , } , { , , } , { , , } , { , , } }; GLfloat colors[][3] = { {0.0,0.0,0.0} , {1.0,0.0,0.0}, {1.0,1.0,0.0} , {0.0,1.0,0.0}, {0.0,0.0,1.0} , {1.0,0.0,1.0}, {1.0,1.0,1.0} , {0.0,1.0,1.0} }; void quad(int a, int b, int c , int d) { /* draw a polygon via list of vertices */ //GL_POLYGON would also work because we're only defining one 4-sided quad at a time. glBegin(GL_QUADS); glColor3fv(colors[a]); glVertex3fv(vertices[a]); glColor3fv(colors[b]); glVertex3fv(vertices[b]); glColor3fv(colors[c]); glVertex3fv(vertices[c]); glColor3fv(colors[d]); glVertex3fv(vertices[d]); glEnd(); } void colorcube(void) { quad( , , , ); quad( , , , ); quad( , , , ); quad( , , , ); quad( , , , ); quad( , , , ); } static GLfloat theta[] = {0.0,0.0,0.0}; static GLint axis = 2; void display(void) { /* display callback, clear frame buffer and z buffer, rotate cube and draw, swap buffers */ glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glLoadIdentity(); glRotatef( , 1.0, 0.0, 0.0); glRotatef( , 0.0, 1.0, 0.0); glRotatef( , 0.0, 0.0, 1.0); colorcube(); glutSwapBuffers(); } void keyboard (unsigned char key, int x , int y) { /* keyboard callback, selects an axis about which to rotate */ if (key=='x' || key=='X') {axis = 0;glutPostRedisplay();} if (key=='y' || key=='Y') {axis = 1;glutPostRedisplay();} if (key=='z' || key=='Z') {axis = 2;glutPostRedisplay();} if (key=='q' || key=='Q') exit(0); theta[axis] += 2.0; if( theta[axis] > 360.0 ) theta[axis] -= 360.0; } void myReshape(int w, int h) { glViewport(0, 0, w, h); glMatrixMode(GL_PROJECTION); glLoadIdentity(); if (w <= h) glOrtho(-4.0, 4.0, -4.0 * (GLfloat) h / (GLfloat) w, 4.0 * (GLfloat) h / (GLfloat) w, -10.0, 10.0); else glOrtho(-4.0 * (GLfloat) w / (GLfloat) h, 4.0 * (GLfloat) w / (GLfloat) h, -4.0, 4.0, -10.0, 10.0); glMatrixMode(GL_MODELVIEW); } void main(int argc, char **argv) { glutInit(&argc, argv); // need both double buffering and z buffer glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH); glutInitWindowSize(500, 500); glutCreateWindow("colorcube"); glutReshapeFunc(myReshape); glutDisplayFunc(display); glutKeyboardFunc(keyboard); glEnable(GL_DEPTH_TEST); /* Enable hidden--surface--removal */ glutMainLoop(); }