1 / 18

Interactive Input & Animation

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

miron
Download Presentation

Interactive Input & Animation

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Interactive Input & Animation Last Updated: 24-03-2009 HB11 HB13

  2. 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

  3. Interactive Input Methods • GLUT Keyboard Functions • glut Keyboard Func (keyFcn) • Void keyFcn (GLubyte key, Glint xMouse, Glint yMouse)

  4. 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

  5. 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

  6. 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

  7. 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() }

  8. 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(); }

  9. 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; } }

  10. 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; }

  11. What is next? • Use glutTimerFunc instead of glutIdleFunc in previous example • Exit from program with Esc key

  12. 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.

  13. 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(); }

  14. 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(); }

  15. Example 3/4 void keyboard(unsigned char key, int x, int y) { if (key=27) { exit(0); } }

  16. 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; }

  17. 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]

  18. 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

More Related