Presentation is loading. Please wait.

Presentation is loading. Please wait.

The User Interface Lecture 2 Mon, Aug 27, 2007.

Similar presentations


Presentation on theme: "The User Interface Lecture 2 Mon, Aug 27, 2007."— Presentation transcript:

1 The User Interface Lecture 2 Mon, Aug 27, 2007

2 Callback Functions A callback function is a programmer-specified function that the library will call whenever necessary. Each callback function must be registered with glut. glut provides over 20 callbacks.

3 Callback Functions The glut library contains functions with names of the form glutNameFunc(name), where Name stands for some form of windows interaction (mouse, keyboard, etc.). The parameter is a user-defined function name(). For example, glutMouseFunc(mouse);

4 Callback Functions Then when the action is initiated by the user (mouse click, keystroke, etc.), the function name() is called to handle it.

5 OpenGL Callback Functions
glutDisplayFunc(display); Called whenever the scene needs to be redrawn. Activated by calls to glutPostRedisplay(). glutReshapeFunc(reshape); Called whenever the window is resized. Activated by resizing the window.

6 OpenGL Callback Functions
glutKeyboardFunc(keyboard) Called whenever an ASCII key is pressed. Activated by keystrokes (down only) of an ASCII key (letters, digits, punctuation). glutSpecialFunc(special) Called whenever a non-ASCII key is pressed. Activated by keystrokes (down only) of a non-ASCII key (function keys, arrow keys, etc.).

7 OpenGL Callback Functions
glutMouseFunc(mouse) Called whenever the mouse is clicked (up or down). Activated by mouse clicks. Left or right button, button up or button down.

8 OpenGL Callback Functions
glutMotionFunc(motion) Called whenever the mouse is moved while the mouse button is down. glutPassiveMotionFunc(passiveMotion) Called whenever the mouse is moved while the mouse button is up.

9 OpenGL Callback Functions
glutIdleFunc(idle) Called whenever nothing else is happening. See the online reference manual.

10 OpenGL Callback Functions
Typically, the callback functions end with a call to glutPostRedisplay() so that the scene will be redrawn. To force the scene to be redrawn continuously, include glutPostRedisplay() in the display() function. If the idle() function is used, and it calls glutPostRedisplay(), then this is not necessary.

11 The Main Loop Typically main() ends by calling
glutMainLoop() This function runs “forever,” or until we exit the program. It calls the callback functions as necessary. It handles all drawing commands as they are generated.

12 The Main Loop main() display() Process keyboard and mouse events
reshape() keyboard() special() mouse() idle() motion() passiveMotion()

13 The Main Loop main() display() Process keyboard and mouse events
reshape() keyboard() special() mouse() idle() motion() passiveMotion()

14 The Main Loop main() display() Process keyboard and mouse events
glutMainLoop() display() Process keyboard and mouse events reshape() keyboard() special() mouse() idle() motion() passiveMotion()

15 The Main Loop main() display() Process keyboard and mouse events
reshape() keyboard() special() mouse() idle() motion() passiveMotion()

16 The Main Loop main() display() Process keyboard and mouse events
glutPostRedisplay() display() Process keyboard and mouse events reshape() keyboard() special() mouse() idle() motion() passiveMotion()

17 The Main Loop main() display() Process keyboard and mouse events
Window resize reshape() keyboard() special() mouse() idle() motion() passiveMotion()

18 The Main Loop main() display() Process keyboard and mouse events
reshape() ASCII keystroke keyboard() special() mouse() idle() motion() passiveMotion()

19 The Main Loop main() display() Process keyboard and mouse events
reshape() keyboard() Arrow/F key special() mouse() idle() motion() passiveMotion()

20 The Main Loop main() display() Process keyboard and mouse events
reshape() keyboard() special() Mouse down/up mouse() idle() motion() passiveMotion()

21 The Main Loop main() display() Process keyboard and mouse events
reshape() keyboard() special() mouse() idle() Mouse-down movement motion() passiveMotion()

22 The Main Loop main() display() Process keyboard and mouse events
reshape() keyboard() Mouse-up movement special() mouse() idle() motion() passiveMotion()

23 The Main Loop main() display() Process keyboard and mouse events
reshape() keyboard() special() No activity mouse() idle() motion() passiveMotion()

24 Example: Callback Functions
Read Run

25 Other Initializations
void init() { glClearColor(0.8, 0.8, 0.8, 0.0); // Light gray glEnable(GL_DEPTH_TEST); // Enable depth testing glDepthFunc(GL_LEQUAL); // Type of depth testing glClearDepth(1.0); // Depth buffer setup printInstructions(); : return; }

26 The keyboard() Function
The prototype of the keyboard() function is void keyboard(unsigned char key, int x, int y); key – ASCII value of the key pressed. x, y – x and y window coordinates of the mouse. Caution – y is measured from the top down.

27 The keyboard() Function
Typical structure of the keyboard() function: void keyboard(unsigned char key, int x, int y) { y = screenHeight – y; // y measured from bottom up switch (key) case '+': case '=': // Code to zoom in break; : case ESC: exit(0); } glPostRedisplay(); return;

28 void mouse(int button, int state, int x, int y);
The mouse() Function The prototype of the mouse() function is void mouse(int button, int state, int x, int y); button – GLUT_LEFT_BUTTON or GLUT_RIGHT_BUTTON. state – GLUT_UP or GLUT_DOWN. x, y – x and y window coordinates of mouse. Caution – y is measured from the top down.

29 The mouse() Function Typical structure of the mouse() function:
void mouse(int button, int state, int x, int y) { y = screenHeight – y; // y measured from bottom up if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN) // Perform left-button-down action } else : // Other actions glPostRedisplay(); return;

30 Callback Functions in Action
Read Run


Download ppt "The User Interface Lecture 2 Mon, Aug 27, 2007."

Similar presentations


Ads by Google