interactive input & animation
DESCRIPTION
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 - PowerPoint PPT PresentationTRANSCRIPT
1Interactive Input & AnimationLast Updated: 24-03-2009
HB11HB13
2Interactive 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_DOWNGLUT_UP
3Interactive Input Methods GLUT Keyboard Functions
• glut Keyboard Func (keyFcn)• Void keyFcn (GLubyte key, Glint xMouse, Glint yMouse)
4AnimationTo give life: virtual realityWe use parameterised transformations that change over time tThus for each frame the object moves a little further, just like a movie
5Animation
Initialization
Timer function
Initialization : 1. Initialize OpenGL & GLUT2. Load models.3. Load animation file. Set Timer function :
1. Use glutTimerFunc() to set Timer function.
Set Timer function
Timer function : 1. Increase time counter.2. load animation data of this time.3. if there is another frame, use glutTimerFunc() again
6Animation 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
7Example 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()
}
8Example 2/4void 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();
}
9Example 3/4void 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;}
}
10Example 4/4int main(int argc, 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;
}
11What is next?1. Use glutTimerFunc instead of glutIdleFunc in
previous example2. Exit from program with Esc key
12glutTimerFunction
Arguments• msecs : ms.• Func : Timer function pointer.• Value : argument pass to Timer funciton.
Ex.• glutTimerFunc(5000, TimerFunc, 0); call TimerFunc(0) 5000 ms later.
13Example 1/4#include <GL/glut.h>#include <stdlib.h>static GLfloat spin = 0.0;int timer_milliseconds = 34; // 34 milliseconds are approx. fps = 29.97void 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();
}
14Example 2/4void 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();
}
15Example 3/4void keyboard(unsigned char key, int x, int y){
if (key=27) {exit(0);
}}
16Example 4/4int main(int argc, 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;
}
17Exercise1. 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.
2. Learn the use of 3rd parameter of glutTimerFunc from [3]
18References1. Donald Hearn, M. Pauline Baker, Computer Graphics with OpenGL, Third
Edition, Prentice Hall, 2004.2. Hill, F. S., Kelly S. M., Computer Graphics Using OpenGL, Third Edition,
Pearson Education, 2007, http://www.4twk.com/shill/ 3. http://ironbark.bendigo.latrobe.edu.au/~fran/int32gp/2006/wk04/tt_02.html