OpenGL Event Driven Programming
-
Upload
herrod-garza -
Category
Documents
-
view
213 -
download
5
description
Transcript of OpenGL Event Driven Programming
![Page 1: OpenGL Event Driven Programming](https://reader035.fdocuments.us/reader035/viewer/2022082200/56813022550346895d95a41f/html5/thumbnails/1.jpg)
OpenGLEvent Driven Programming
Program responds to events Events are handled by user defined callback
functions Callbacks must know context and event type
(passed through variables)
![Page 2: OpenGL Event Driven Programming](https://reader035.fdocuments.us/reader035/viewer/2022082200/56813022550346895d95a41f/html5/thumbnails/2.jpg)
Event Driven Programming
MainEventLoop
DisplayHandler
KeyboardHandler
MouseHandler
![Page 3: OpenGL Event Driven Programming](https://reader035.fdocuments.us/reader035/viewer/2022082200/56813022550346895d95a41f/html5/thumbnails/3.jpg)
Simple Example
Displaying a squareint main (int argc, char *argv[]){ glutInit(&argc, argv); glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE); int windowHandle = glutCreateWindow("Simple GLUT App");
glutDisplayFunc(redraw);
glutMainLoop();
return 0;}
![Page 4: OpenGL Event Driven Programming](https://reader035.fdocuments.us/reader035/viewer/2022082200/56813022550346895d95a41f/html5/thumbnails/4.jpg)
Display Callback
Called when window is redrawnvoid redraw(){ glClear(GL_COLOR_BUFFER_BIT); glBegin(GL_QUADS); glColor3f(1, 0, 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(); // GL_QUADS glutSwapBuffers();}
![Page 5: OpenGL Event Driven Programming](https://reader035.fdocuments.us/reader035/viewer/2022082200/56813022550346895d95a41f/html5/thumbnails/5.jpg)
More GLUT
Additional GLUT functions glutPositionWindow(int x,int y); glutReshapeWindow(int w, int h);
Additional callback functions glutReshapeFunction(reshape); glutMouseFunction(mousebutton); glutMotionFunction(motion); glutKeyboardFunction(keyboardCB); glutSpecialFunction(special); glutIdleFunction(animate);
![Page 6: OpenGL Event Driven Programming](https://reader035.fdocuments.us/reader035/viewer/2022082200/56813022550346895d95a41f/html5/thumbnails/6.jpg)
Reshape Callback
Called when the window is resizedvoid reshape(int w, int h){ glViewport(0.0,0.0,w,h);
glMatrixMode(GL_PROJECTION); glLoadIdentity(); glOrtho(0.0,w,0.0,h, -1.0, 1.0);
glMatrixMode(GL_MODELVIEW); glLoadIdentity(); }
![Page 7: OpenGL Event Driven Programming](https://reader035.fdocuments.us/reader035/viewer/2022082200/56813022550346895d95a41f/html5/thumbnails/7.jpg)
Mouse Callbacks
Called when the mouse button is pressedvoid mousebutton(int button, int state, int x, int y){ if (button==GLUT_LEFT_BUTTON && state==GLUT_DOWN) { rx = x; ry = winHeight - y; }}
Called when the mouse is moved with button downvoid motion(int x, int y){ rx = x; ry = winHeight - y;}
![Page 8: OpenGL Event Driven Programming](https://reader035.fdocuments.us/reader035/viewer/2022082200/56813022550346895d95a41f/html5/thumbnails/8.jpg)
Keyboard Callbacks
Called when a button is pressedvoid keyboardCB(unsigned char key, int x, int y){ switch(key) { case 'a': cout<<"a Pressed"<<endl; break; }}
Called when a special button is pressedvoid special(int key, int x, int y){ switch(key) { case GLUT_F1_KEY:
cout<<“F1 Pressed"<<endl; break; }}
![Page 9: OpenGL Event Driven Programming](https://reader035.fdocuments.us/reader035/viewer/2022082200/56813022550346895d95a41f/html5/thumbnails/9.jpg)
Animation Callbacks
Called when the system is idlevoid animationCB(){ float time = glutGet(GLUT_ELAPSED_TIME); glRotated(time,0.0,1.0,0.0); glutPostRedisplay();}
![Page 10: OpenGL Event Driven Programming](https://reader035.fdocuments.us/reader035/viewer/2022082200/56813022550346895d95a41f/html5/thumbnails/10.jpg)
Menu Callbacks
Creating a menu enum { M_NONE, M_OPEN, M_SAVE, M_EXIT }; glutCreateMenu(menuFunc); glutAddMenuEntry("Open", M_OPEN); glutAddMenuEntry("-----------------------", M_NONE); glutAddMenuEntry("Exit", M_EXIT); glutAttachMenu(GLUT_RIGHT_BUTTON);
Called when a menu button is pressedvoid menuFunc(int value){ switch(value) { case M_EXIT: exit(0); break; }}