// OpenGL Tutorial // Events.c - TJ VERSION // http://www.eecs.tulane.edu/www/Terry/OpenGL/Introduction.html /************************************************************************* This example extends Hello_World.c to accept and handle events. The user should try produce each of the following events: key press, mouse movement, mouse button press, mouse button release, reshape window, and expose window. *************************************************************************/ // gcc events.c -lm -L/usr/lib -lGLU -lGL -lglut -L/usr/X11R6/lib -lX11 -lXext -lXi -lXmu #include #include #include #include void init() { // Clear the window glClear(GL_COLOR_BUFFER_BIT); } void reshape(int width, int height) { printf("Got a Reshape Event"); printf(":\t width = %d height = %d\n", width, height); // Set the new viewport size glViewport(0, 0, (GLint)width, (GLint)height); // Clear the window glClear(GL_COLOR_BUFFER_BIT); } void keypressed(unsigned char key, int x, int y) { printf("Got a Key Pressed Event "); printf("at:\t x = %d, y = %d\n", x, y); if (key == (unsigned char) 27) exit(0); } void mouseButtonPress(int button, int state, int x, int y) { printf("Got a Mouse Keypressed Event"); printf(":\t x = %d, y = %d, button = ", x, y); switch (state) { case GLUT_UP : printf("UP\t"); break; case GLUT_DOWN : printf("DOWN\t"); break; } switch (button) { case GLUT_LEFT_BUTTON : printf("left\n"); break; case GLUT_RIGHT_BUTTON : printf("right\n"); break; case GLUT_MIDDLE_BUTTON : printf("middle\n"); break; default : printf("none\n"); break; } } void mouseMoveNoButtonPress(int x, int y) { printf("Got a Mouse Move Event"); printf(":\t x = %d, y = %d, no button pressed\n", x, y); } void mouseMoveWithButtonPress(int x, int y) { printf("Got a Mouse Move Event"); printf(":\t x = %d, y = %d, with button pressed\n", x, y); } void specialKeyPress(int key, int x, int y) { printf("Got a Special Keypressed Event"); printf(":\t x = %d, y = %d ", x, y); switch (key) { case GLUT_KEY_LEFT : printf("LEFT ARROW\n"); break; case GLUT_KEY_UP : printf("UP ARROW\n"); break; case GLUT_KEY_RIGHT : printf("RIGHT ARROW\n"); break; case GLUT_KEY_DOWN : printf("DOWN ARROW\n"); break; case GLUT_KEY_INSERT : printf("INSERT\n"); break; case GLUT_KEY_HOME : printf("HOME\n"); break; case GLUT_KEY_F1 : printf("F1\n"); break; } // THE ESCAPE, BACKSPACE, and DELETE keys are generated as an ASCII // character } void draw(void) { printf("Got a Display Event\n"); // Set the drawing color glColor3f(1.0, 1.0, 1.0); // Specify which primitive type is to be drawn glBegin(GL_POLYGON); // Specify verticies in quad glVertex2f(-0.5, -0.5); glVertex2f(-0.5, 0.5); glVertex2f(0.5, 0.5); glVertex2f(0.5, -0.5); glEnd(); // Flush the buffer to force drawing of all objects thus far glFlush(); } int main(int argc, char **argv) { // Open a window and name it // Open a window, name it "Events" glutInit(&argc, argv); glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB); glutInitWindowSize(400, 400); glutInitWindowPosition(200,200); glutCreateWindow("Events"); // Set the clear color to black glClearColor(0.0, 0.0, 0.0, 0.0); // Assign expose() to be the function called whenever // an expose event occurs init(); // Assign reshape() to be the function called whenever // a reshape event occurs glutReshapeFunc(reshape); // Assign key_down() to be the function called whenever // a key is pressed glutKeyboardFunc(keypressed); // Assign mouse_down() to be the function called whenever // a mouse button is pressed glutMouseFunc(mouseButtonPress); // Assign mouse_move() to be the function called whenever // the mouse is moved glutMotionFunc(mouseMoveWithButtonPress); glutPassiveMotionFunc(mouseMoveNoButtonPress); glutSpecialFunc(specialKeyPress); // Assign draw() to be the function called whenever a display // event occurs, generally after a resize or expose event glutDisplayFunc(draw); // Assign idle() to be the function called whenever there // are no events, NOT USED IN THIS EXAMPLE // tkIdleFunc(idle); // Pass program control to tk's event handling code // In other words, loop forever glutMainLoop(); return 0; }