/* 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? Assign the color before quad is called, rather than before each vertex inside quad. How would you render axes which are fixed, don't move, and are in the coordinate system of the camera view? Push GL_MODELVIEW, LoadIdentity, render axes, Pop GL_MODELVIEW 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? No, because the default camera position looks down the z-axis toward the origin. How would you render axes which follow the cube? Draw the axes without pushing and popping the current modelview matrix. */ #include #include // just a convenience, since each point has three coordinates typedef GLfloat point3[3]; point3 vertices[8] = { {-1,-1, 1} , {-1, 1, 1} , { 1, 1, 1} , { 1,-1, 1} , {-1,-1,-1} , {-1, 1,-1} , { 1, 1,-1} , { 1,-1,-1} }; 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( 0, 3, 2, 1); quad( 2, 3, 7, 6); quad( 0, 4, 7, 3); quad( 1, 2, 6, 5); quad( 4, 5, 6, 7); quad( 0, 1, 5, 4); } 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( theta[0] , 1.0, 0.0, 0.0); glRotatef( theta[1] , 0.0, 1.0, 0.0); glRotatef( theta[2] , 0.0, 0.0, 1.0); colorcube(); /* // fixed axes glPushMatrix(); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glLineWidth(5); glColor3f(1,1,1); glBegin(GL_LINES); //x axis glVertex3f(0,0,0); glVertex3f(5,0,0); // y axis glVertex3f(0,0,0); glVertex3f(0,5,0); // z axis glVertex3f(0,0,0); glVertex3f(0,0,5); glEnd(); glPopMatrix(); */ //axes which follow the cube glLineWidth(5); glColor3f(1,1,1); glBegin(GL_LINES); //x axis glVertex3f(0,0,0); glVertex3f(5,0,0); // y axis glVertex3f(0,0,0); glVertex3f(0,5,0); // z axis glVertex3f(0,0,0); glVertex3f(0,0,5); glEnd(); 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(); }