interactive input & animation

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

Upload: miron

Post on 24-Feb-2016

31 views

Category:

Documents


0 download

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 Presentation

TRANSCRIPT

Page 1: Interactive Input & Animation

1Interactive Input & AnimationLast Updated: 24-03-2009

HB11HB13

Page 2: Interactive Input & Animation

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

Page 3: Interactive Input & Animation

3Interactive Input Methods GLUT Keyboard Functions

• glut Keyboard Func (keyFcn)• Void keyFcn (GLubyte key, Glint xMouse, Glint yMouse)

Page 4: Interactive Input & Animation

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

Page 5: Interactive Input & Animation

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

Page 6: Interactive Input & Animation

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

Page 7: Interactive Input & Animation

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

}

Page 8: Interactive Input & Animation

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();

}

Page 9: Interactive Input & Animation

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

}

Page 10: Interactive Input & Animation

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;

}

Page 11: Interactive Input & Animation

11What is next?1. Use glutTimerFunc instead of glutIdleFunc in

previous example2. Exit from program with Esc key

Page 12: Interactive Input & Animation

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.

Page 13: Interactive Input & Animation

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();

}

Page 14: Interactive Input & Animation

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();

}

Page 15: Interactive Input & Animation

15Example 3/4void keyboard(unsigned char key, int x, int y){

if (key=27) {exit(0);

}}

Page 16: Interactive Input & Animation

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;

}

Page 17: Interactive Input & Animation

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]

Page 18: Interactive Input & Animation

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