180 likes | 285 Views
Interactive Input & Animation. Last Updated : 24-03-2009. HB11 HB13. Interactive Input Methods. GLUT Mouse Functions glutMouseFunc ( mouseFcn ) Void mouseFcn ( GLint button, Glint action, Glint xMouse , Glint yMouse ) button: GLUT_LEFT_BUTTON GLUT_MIDDLE_BUTTON
E N D
Interactive Input & Animation Last Updated: 24-03-2009 HB11 HB13
Interactive Input Methods • GLUT Mouse Functions • glutMouseFunc (mouseFcn) • Void mouseFcn (GLint button, Glint action, Glint xMouse, Glint yMouse) button: GLUT_LEFT_BUTTON GLUT_MIDDLE_BUTTON GLUT_RIGHT_BUTTON action: GLUT_DOWN GLUT_UP
Interactive Input Methods • GLUT Keyboard Functions • glut Keyboard Func (keyFcn) • Void keyFcn (GLubyte key, Glint xMouse, Glint yMouse)
Animation • To give life: virtual reality • We use parameterised transformations that change over time t • Thus for each frame the object moves a little further, just like a movie
Animation • Initialization : • Initialize OpenGL & GLUT • Load models. • Load animation file. Initialization Set Timer function • Set Timer function : • Use glutTimerFunc() to set Timer function. Timer function • Timer function : • Increase time counter. • load animation data of this time. • if there is another frame, • use glutTimerFunc() again
Animation • glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB); • Activate double-buffering operations • glutSwapBuffers(); • Interchange front & back refresh buffers • glutIdleFunc(animationFcn); • Specify a function for incrementing animation parameters • glutTimerFunc(msecs, func, value); // instead of glutIdleFunc • registers a timer callback to be triggered in a specified number of milliseconds. • glutPostRedisplay(); • Can be the last statement of animationFcn, marks the current window as needing to be redisplayed
Example 1/4 #include <GL/glut.h> #include <stdlib.h> static GLfloat spin = 0.0; void init(void) { glClearColor (0.0, 0.0, 0.0, 0.0); } void display(void) { glClear(GL_COLOR_BUFFER_BIT); glPushMatrix(); glRotatef(spin, 0.0, 0.0, 1.0); glColor3f(1.0, 0.0, 0.0); glRectf(-25.0, -25.0, 25.0, 25.0); glPopMatrix(); glutSwapBuffers(); // instead of glFlush() }
Example 2/4 void animationFcn(void) // for use in glutIdleFunc(animationFcn); { spin = spin + 0.01; if (spin > 360.0) spin = 0.0; glutPostRedisplay(); } void reshape(int w, int h) { glViewport (0, 0, (GLsizei) w, (GLsizei) h); glMatrixMode(GL_PROJECTION); glLoadIdentity(); glOrtho(-50.0, 50.0, -50.0, 50.0, -1.0, 1.0); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); }
Example 3/4 void mouse(int button, int state, int x, int y) { switch (button) { case GLUT_LEFT_BUTTON: if (state == GLUT_DOWN) glutIdleFunc(animationFcn); break; case GLUT_RIGHT_BUTTON: if (state == GLUT_DOWN) glutIdleFunc(NULL); break; default: break; } }
Example 4/4 int main(intargc, char** argv) { glutInit(&argc, argv); glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB); glutInitWindowSize (250, 250); glutInitWindowPosition (100, 100); glutCreateWindow (argv[0]); init (); glutDisplayFunc(display); glutReshapeFunc(reshape); glutMouseFunc(mouse); glutMainLoop(); return 0; }
What is next? • Use glutTimerFunc instead of glutIdleFunc in previous example • Exit from program with Esc key
glutTimerFunction • Arguments • msecs : ms. • Func : Timer function pointer. • Value : argument pass to Timer funciton. • Ex. • glutTimerFunc(5000, TimerFunc, 0); call TimerFunc(0) 5000 ms later.
Example 1/4 #include <GL/glut.h> #include <stdlib.h> static GLfloat spin = 0.0; inttimer_milliseconds = 34; // 34 milliseconds are approx. fps = 29.97 void init(void) { glClearColor (0.0, 0.0, 0.0, 0.0); } void display(void) { glClear(GL_COLOR_BUFFER_BIT); glPushMatrix(); glRotatef(spin, 0.0, 0.0, 1.0); glColor3f(1.0, 0.0, 0.0); glRectf(-25.0, -25.0, 25.0, 25.0); glPopMatrix(); glutSwapBuffers(); }
Example 2/4 void animationFcn(int value) { glutTimerFunc(timer_milliseconds, animationFcn, 0); spin = spin + 1; if (spin > 360.0) spin = 0.0; glutPostRedisplay(); } void reshape(int w, int h) { glViewport (0, 0, (GLsizei) w, (GLsizei) h); glMatrixMode(GL_PROJECTION); glLoadIdentity(); glOrtho(-50.0, 50.0, -50.0, 50.0, -1.0, 1.0); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); }
Example 3/4 void keyboard(unsigned char key, int x, int y) { if (key=27) { exit(0); } }
Example 4/4 int main(intargc, char** argv) { glutInit(&argc, argv); glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB); glutInitWindowSize (250, 250); glutInitWindowPosition (100, 100); glutCreateWindow (argv[0]); init (); glutDisplayFunc(display); glutReshapeFunc(reshape); glutKeyboardFunc(keyboard); glutTimerFunc(timer_milliseconds, animationFcn, 0); glutMainLoop(); return 0; }
Exercise • Add in previous program, start and stop animation with left and right buttons of mouse, resp. Slow and Fast movement with S & F keyboard shortcuts. • Learn the use of 3rd parameter of glutTimerFunc from [3]
References • Donald Hearn, M. Pauline Baker, Computer Graphics with OpenGL, Third Edition, Prentice Hall, 2004. • Hill, F. S., Kelly S. M., Computer Graphics Using OpenGL, Third Edition, Pearson Education, 2007, http://www.4twk.com/shill/ • http://ironbark.bendigo.latrobe.edu.au/~fran/int32gp/2006/wk04/tt_02.html