The User Interface Lecture 2 Mon, Aug 27, 2007
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.
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);
Callback Functions Then when the action is initiated by the user (mouse click, keystroke, etc.), the function name() is called to handle it.
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.
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.).
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.
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.
OpenGL Callback Functions glutIdleFunc(idle) Called whenever nothing else is happening. See the online reference manual.
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.
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.
The Main Loop main() display() Process keyboard and mouse events reshape() keyboard() special() mouse() idle() motion() passiveMotion()
The Main Loop main() display() Process keyboard and mouse events reshape() keyboard() special() mouse() idle() motion() passiveMotion()
The Main Loop main() display() Process keyboard and mouse events glutMainLoop() display() Process keyboard and mouse events reshape() keyboard() special() mouse() idle() motion() passiveMotion()
The Main Loop main() display() Process keyboard and mouse events reshape() keyboard() special() mouse() idle() motion() passiveMotion()
The Main Loop main() display() Process keyboard and mouse events glutPostRedisplay() display() Process keyboard and mouse events reshape() keyboard() special() mouse() idle() motion() passiveMotion()
The Main Loop main() display() Process keyboard and mouse events Window resize reshape() keyboard() special() mouse() idle() motion() passiveMotion()
The Main Loop main() display() Process keyboard and mouse events reshape() ASCII keystroke keyboard() special() mouse() idle() motion() passiveMotion()
The Main Loop main() display() Process keyboard and mouse events reshape() keyboard() Arrow/F key special() mouse() idle() motion() passiveMotion()
The Main Loop main() display() Process keyboard and mouse events reshape() keyboard() special() Mouse down/up mouse() idle() motion() passiveMotion()
The Main Loop main() display() Process keyboard and mouse events reshape() keyboard() special() mouse() idle() Mouse-down movement motion() passiveMotion()
The Main Loop main() display() Process keyboard and mouse events reshape() keyboard() Mouse-up movement special() mouse() idle() motion() passiveMotion()
The Main Loop main() display() Process keyboard and mouse events reshape() keyboard() special() No activity mouse() idle() motion() passiveMotion()
Example: Callback Functions Read Run
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; }
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.
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;
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.
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;
Callback Functions in Action Read Run