#include #include #include #define TRUE 1 #define FALSE 0 #define PI 3.1415926535 static int old_x, old_y; GLfloat trans_z = -40.0; GLfloat trans_y = 0.0; GLfloat trans_x = 0.0; GLfloat scale_x = 1.0; GLfloat scale_y = 1.0; GLfloat scale_z = 1.0; GLfloat rot_y = 0.0; GLfloat rot_x = 0.0; GLfloat rot_z = 0.0; GLboolean XY = TRUE; GLboolean DEPTH = TRUE; GLboolean CULL = FALSE; GLboolean LIGHTING = TRUE; /* Prototypes */ void doCube(void); void checkDepth(void); void checkCulling(void); /*////////////////////////////////////////////////////////////////////////////*/ static GLfloat v[8][3]= { {-5, 5, 5}, /*/ 0 */ { 5, 5, 5}, /*/ 1*/ { 5, -5, 5}, /*/ 2*/ {-5, -5, 5}, /*/ 3*/ {-5, -5, -5}, /*/ 4*/ {-5, 5, -5}, /*/ 5*/ { 5, 5, -5}, /*/ 6*/ { 5, -5, -5} /*/ 7*/ }; /*/ end v*/ static GLfloat colors[6][3]= { {1.0, 0.0, 0.0}, {0.0, 1.0, 0.0}, {0.0, 0.0, 1.0}, {0.0, 1.0, 1.0}, {1.0, 0.0, 1.0}, {1.0, 1.0, 0.0} }; static GLuint indices[6][4] = { {0, 1, 2, 3}, /*/f*/ {1, 6, 7, 2}, /*/l*/ {5, 0, 3, 4}, /*/r*/ {5, 6, 1, 0}, /*/t*/ {3, 2, 7, 4}, /*/b*/ {6, 5, 4, 7} /*/bk*/ }; static GLfloat norms[6][3]= { {0, 0, 1}, {1, 0, 0}, {-1, 0, 0}, {0, 1, 0}, {0, -1, 0}, {0, 0, -1} }; void doCube(void) { int face, vertex; /* ******* This is where the polygons go ******* */ glBegin(GL_QUADS); // if triangles then do glBegin(GL_TRIANGLES); for (face = 0; face < 6; face++){ for(vertex=0; vertex<4; vertex++){ glNormal3fv (norms[face]); glColor3fv (colors[face]); glVertex3fv(v[indices[face][vertex]]); } } glEnd(); } /*****************************************************/ void init(void) /*****************************************************/ { glShadeModel(GL_SMOOTH); glClearColor(0.0, 0.0, 0.0, 1.0); /*/ SetUp the lighting conditions */ glEnable(GL_LIGHTING); GLfloat light_ambient[] = { 0.0, 0.0, 0.0, 1.0 }; GLfloat light_diffuse[] = { 0.6, 0.6, 0.6, 1.0 }; GLfloat light_specular[] = { 0.6, 0.6, 0.6, 1.0 }; GLfloat light_position[] = { 10.0, 0.0, -1.0, 1.0 }; glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient); glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse); glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular); glLightfv(GL_LIGHT0, GL_POSITION, light_position); glEnable(GL_LIGHT0); GLfloat mat[3] = { 0.9, 0.9, 0.9 }; glMaterialfv(GL_FRONT, GL_SPECULAR, mat); glMaterialf(GL_FRONT, GL_SHININESS, 128.0); glEnable(GL_COLOR_MATERIAL); glColorMaterial(GL_FRONT, GL_DIFFUSE); glEnable(GL_NORMALIZE); } /*****************************************************/ void checkDepth(void) /*****************************************************/ { if (DEPTH) glEnable(GL_DEPTH_TEST); else if (!DEPTH) glDisable(GL_DEPTH_TEST); glutPostRedisplay(); } /*****************************************************/ void checkLighting(void) /*****************************************************/ { if (LIGHTING) glEnable(GL_LIGHTING); else if (!LIGHTING) glDisable(GL_LIGHTING); glutPostRedisplay(); } /*****************************************************/ void checkCulling(void) /*****************************************************/ { if (CULL) { glEnable(GL_CULL_FACE); /*/glCullFace(GL_BACK); */ glCullFace(GL_FRONT); glFrontFace(GL_CCW); } else if (!CULL) glDisable(GL_CULL_FACE); glutPostRedisplay(); } /*****************************************************/ void rotate (int rotX, int rotY) /*****************************************************/ { if (XY) { glRotatef(rotX, 1.0, 0.0, 0.0); glRotatef(rotY, 0.0, 1.0, 0.0); } else { glRotatef(rotY, 0.0, 1.0, 0.0); glRotatef(rotX, 1.0, 0.0, 0.0); } glutPostRedisplay(); } /*****************************************************/ void display(void) /*****************************************************/ { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glLoadIdentity(); checkDepth(); checkCulling(); checkLighting(); glColor3f(1.0, 1.0, 1.0); glPushMatrix(); glTranslatef(0.0, 0.0, trans_z); rotate (rot_x, rot_y); doCube(); glPopMatrix(); glutSwapBuffers(); } /*****************************************************/ void reshape(int width, int height) /*****************************************************/ { GLfloat left; GLfloat right; GLfloat top; GLfloat bottom; glViewport(0, 0, width, height); glMatrixMode(GL_PROJECTION); glLoadIdentity(); right = (width/100)/2; left = -right; top = (height/100)/2; bottom = -top; glFrustum(left, right, bottom, top, 10.0, 100.0); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); } /*****************************************************/ void motion(int x, int y) /*****************************************************/ { trans_z += (old_y - y); old_y = y; if (trans_z <= -99.0) trans_z = -99.0; else if (trans_z >= -10.0) trans_z = -10.0; glutPostRedisplay(); } /*****************************************************/ void rotation(int x, int y) /*****************************************************/ { rot_x += (old_y - y); old_y = y; if (rot_x >= 360) rot_x -= 360.0; rot_y += (old_x - x); old_x = x; if (rot_y >= 360) rot_y -= 360.0; glutPostRedisplay(); } /*****************************************************/ void scale(int x, int y) /*****************************************************/ { scale_x += (old_y - y); old_y = y; if (scale_x >= 4.0) scale_x = 4.0; else if (scale_x <= 1.0) scale_x = 1.0; scale_y += (old_x - x); old_x = x; if (scale_y >= 4.0) scale_y = 4.0; else if (scale_y <= 1.0) scale_y = 1.0; } /*****************************************************/ void mouse(int button, int state, int x, int y) /*****************************************************/ { old_x = x; old_y = y; switch(button) { case GLUT_LEFT_BUTTON: if (state == GLUT_DOWN) { glutMotionFunc(motion); } if (state == GLUT_UP) glutPassiveMotionFunc(rotation); break; case GLUT_RIGHT_BUTTON: if (state == GLUT_DOWN) { glutMotionFunc(scale); } break; default: break; } glutPostRedisplay(); } /*****************************************************/ void keys(unsigned char key, int x, int y) /*****************************************************/ { switch (key) { case 'O': /* Toggle rotation axes */ case 'o': if (XY) { XY = FALSE; } else if (!XY) { XY = TRUE; } break; case 'Z': case 'z': /* Toggles z-buffering */ if (DEPTH) { DEPTH = FALSE; } else if (!DEPTH) { DEPTH = TRUE; } break; case 'c': case 'C': /* Toggles back-face culling */ if (CULL) { CULL = FALSE; } else if (!CULL) { CULL = TRUE; } break; case 'l': case 'L': /* Toggle lighting */ if (LIGHTING) { LIGHTING = FALSE; } else if (!LIGHTING) { LIGHTING = TRUE; } break; case 13: /* ascii for carriage return (RETURN); reset the parameters. */ rot_y = 0.0; rot_x = 0.0; trans_z = -40.0; scale_x = 1.0; scale_y = 1.0; glutPostRedisplay(); break; case 27: printf("\n\n"); /*exit 0;*/ } } /*****************************************************/ int main(int argc, char** argv) /*****************************************************/ { glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH); glutInitWindowPosition(50, 50); glutInitWindowSize(640, 480); glutInit(&argc, argv); glutCreateWindow("CS455 - Project 5"); init(); glutDisplayFunc(display); glutReshapeFunc(reshape); glutMouseFunc(mouse); glutMotionFunc(NULL); glutPassiveMotionFunc(rotation); glutKeyboardFunc(keys); glutMainLoop(); return 0; }