/* prac8b.c */ /* Purpose: An example of how to do various model manipulations on mouse clicks. */ #ifdef __APPLE__ #include #else #include #endif #include #include #define KEY_ESC 27 /* glut doesn't define this one */ /* Global Variables */ static const GLfloat mNearClip = 5.0; /* near clipping plane */ static const GLfloat mFarClip = 10.0; /* far clipping plane */ enum Actions {ROTATE, NONE}; static enum Actions mAction; static float mRotX; static float mRotY; static float mStartX; static float mStartY; void printHelp( void ) { fprintf(stdout, "\nRotate using the mouse\n\n" "Escape key - exit the program\n\n"); } GLvoid keyboard (GLubyte key, GLint x, GLint y) { switch(key) { case KEY_ESC: /* exit when escape pressed */ exit (0); break; default : break ; } } GLvoid specialkeys( GLint key, GLint x, GLint y) { switch (key) { case GLUT_KEY_F1: /* print Help information */ printHelp ( ); break; } } GLvoid checkError( const char* const label ) { GLenum error; error = glGetError(); while ( GL_NO_ERROR != error ) { fprintf( stderr,"%s: %s\n", label, gluErrorString(error) ); error = glGetError(); } } GLvoid reshape (GLint w, GLint h) { /* Note this alternative method of maintaining the aspect ratio of the scene. You should try to understand the advantages and disadvantages of this approach. */ if (w >= h) { glViewport(0, 0, h, h); } else { glViewport(0, 0, w, w); } glMatrixMode (GL_PROJECTION); glLoadIdentity (); gluPerspective (30, 1.0, mNearClip, mFarClip); glMatrixMode (GL_MODELVIEW); glLoadIdentity (); } void rotateXAxis (float incr) { mRotX += incr; } /* The y value stays constant and the x and z values change The total distance from the y axis stays constant. */ void rotateYAxis (float incr) { mRotY += incr; } GLvoid mouse (GLint button, GLint state, GLint x, GLint y) { if (state == GLUT_DOWN) { switch (button) { case (GLUT_LEFT_BUTTON): mAction = ROTATE; mStartX = x; mStartY = y; break; default: mAction = NONE; break; } } else { mAction = NONE; } } GLvoid motion (GLint x, GLint y) { float rot_Factor = 0.5; float xAxisAngle; float yAxisAngle; switch (mAction) { case (ROTATE): yAxisAngle = (((float) mStartX - x) * rot_Factor); xAxisAngle = (((float) mStartY - y) * rot_Factor); mStartY = y; mStartX = x; rotateXAxis (-xAxisAngle); rotateYAxis (-yAxisAngle); break; case (NONE): break; default: break; } glutPostRedisplay(); } /* Displays 6 faces of a cube */ GLvoid drawModel (GLvoid) { glBegin(GL_QUADS); glColor4f(0.0,0.0,1.0,1.0); glVertex3f(-0.5,-0.5,0.5); glVertex3f(0.5,-0.5,0.5); glVertex3f(0.5,0.5,0.5); glVertex3f(-0.5,0.5,0.5); glEnd(); glBegin(GL_QUADS); glColor4f(1.0,0.0,0.0,1.0); glVertex3f(0.5,-0.5,0.5); glVertex3f(0.5,-0.5,-0.5); glVertex3f(0.5,0.5,-0.5); glVertex3f(0.5,0.5,0.5); glEnd(); glBegin(GL_QUADS); glColor4f(1.0,1.0,0.0,1.0); glVertex3f(0.5,-0.5,-0.5); glVertex3f(-0.5,-0.5,-0.5); glVertex3f(-0.5,0.5,-0.5); glVertex3f(0.5,0.5,-0.5); glEnd(); glBegin(GL_QUADS); glColor4f(0.0,1.0,0.0,1.0); glVertex3f(-0.5,-0.5,-0.5); glVertex3f(-0.5,-0.5,0.5); glVertex3f(-0.5,0.5,0.5); glVertex3f(-0.5,0.5,-0.5); glEnd(); glBegin(GL_QUADS); glColor4f(1.0,0.0,1.0,1.0); glVertex3f(-0.5,-0.5,-0.5); glVertex3f(0.5,-0.5,-0.5); glVertex3f(0.5,-0.5,0.5); glVertex3f(-0.5,-0.5,0.5); glEnd(); glBegin(GL_QUADS); glColor4f(0.0,1.0,1.0,1.0); glVertex3f(-0.5,0.5,0.5); glVertex3f(0.5,0.5,0.5); glVertex3f(0.5,0.5,-0.5); glVertex3f(-0.5,0.5,-0.5); glEnd(); } GLvoid display(GLvoid) { glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glLoadIdentity(); /* Translate our origin to midway between the near and far clipping plane */ glTranslatef(0, 0, -(mNearClip+mFarClip)/2); glRotatef(mRotX, 1, 0, 0); glRotatef(mRotY, 0, 1, 0); drawModel (); glFlush(); } GLvoid init(GLvoid) { glClearColor(0.0, 0.0, 0.0, 0.0); glShadeModel(GL_FLAT); glEnable(GL_DEPTH_TEST); glEnable (GL_CULL_FACE); glCullFace(GL_BACK); mRotX = 0; mRotY = 0; } int main(int argc, char** argv) { glutInit (&argc, argv) ; glutInitDisplayMode ( GLUT_RGB | GLUT_DEPTH ) ; glutCreateWindow (argv[0]) ; init(); glutDisplayFunc (display) ; glutKeyboardFunc(keyboard) ; glutSpecialFunc (specialkeys); glutReshapeFunc (reshape); glutMouseFunc (mouse); glutMotionFunc (motion); printHelp( ); glutMainLoop() ; return 0 ; }