/* File: step2.c Author: Katia Oleinik This example demonstrates - optional call-back routines - adjusting the viewport based on window size Solution to the assignment: 1. Enable a Keyboard callback routine that prints the pressed key in the command window 2. Make program exit when "q" or "Q" is pressed 3. Enable a Mouse callback routine that prints on the screen the information about which mouse button was pressed */ #include #include #include /* user defined functions declarations */ void keypress( unsigned char key, int x, int y); void specialkeys(int key, int x, int y); void mousepress( int button, int state, int x, int y); void resize(int width, int height); void mydraw(void); void display(void); void init(void); /* keyboard callback routine */ void keypress( unsigned char key, int x, int y) { // output the key printf(" The ASCII code for \'%c\' is %i\n",key, key); if (key == 'q' || key =='Q' || key ==27)exit(0); // exit } /* handle special keys */ void specialkeys(int key, int x, int y) { switch (key) { case GLUT_KEY_LEFT: printf(" Left\n"); break; case GLUT_KEY_RIGHT: printf(" Right\n"); break; } } /* mouse click callback routine */ void mousepress( int button, int state, int x, int y) { if (state != GLUT_DOWN)return; switch (button) { case GLUT_LEFT_BUTTON: printf("Left mouse button pressed\n"); break; case GLUT_MIDDLE_BUTTON: printf("Middle mouse button pressed\n"); break; case GLUT_RIGHT_BUTTON: printf("Right mouse button pressed\n"); break; } printf( " Mouse coordinates: %d %d\n",x,y); } /* callback routine to respond on resize of the window */ void resize(int width, int height) { double aspect; /* Reset the viewport */ glViewport(0,0,width,height); /* compute aspect */ aspect = (double)width / (double)height; /* Set up the perspective matrix accordingly */ glMatrixMode(GL_PROJECTION); glLoadIdentity(); //reset projection matrix /* make sure that the clipping planes are set to the original values in the smallest direction */ if (aspect < 1.0) glOrtho(-4., 4., -4./aspect, 4./aspect, 1., 10.); else glOrtho(-4.*aspect, 4.*aspect, -4., 4., 1., 10.); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); /* Set Camera Position */ gluLookAt(0., 0., 5., 0., 0., 0., 0., 1., 0.); } /* drawing routine, called by display every animated frame */ void mydraw(void) { glColor3f( 1.0, 0.5, 0.0); // orange color glutSolidSphere(1., 24, 24); //draw sphere } /* display is called by the glut main loop once for every animated frame */ void display(void) { /* initialize color and depth buffers */ glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); /* call the routine that actually draws what you want */ mydraw(); /* show the just-filled frame buffer */ glutSwapBuffers(); } /* called once to set up basic opengl state */ void init(void) { /* Use depth buffering for hidden surface elimination. */ glEnable(GL_DEPTH_TEST); /* Set up the perspective matrix */ glMatrixMode(GL_PROJECTION); glLoadIdentity(); /* Define perspective matrix */ glOrtho(-4., 4., -4., 4., 1, 10.); /* Set up the model view matrix */ glMatrixMode(GL_MODELVIEW); glLoadIdentity(); /* Define Camera Position */ gluLookAt(0., 0., 5., 0., 0., 0., 0., 1., 0.); } int main(int argc, char **argv) { /* GLUT Configuration */ glutInit(&argc, argv); glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH); glutInitWindowSize(500,500); glutInitWindowPosition(100,100); /* Create Window */ glutCreateWindow("Orange Ball"); /* Set display as a callback for the current window */ glutDisplayFunc(display); /* Set callback function that respond to resizing the window */ glutReshapeFunc(resize); /* Set callback function that responds on keyboard pressing */ glutKeyboardFunc (keypress); /* Set callback function to respond to special keys pressing */ glutSpecialFunc(specialkeys); /* Set callback function that responds on the mouse click */ glutMouseFunc(mousepress); /* Set basic openGL states */ init(); /* Enter GLUT event processing loop */ glutMainLoop(); /* Exit the program */ return 0; }