Lecture2 Continued
Lecture2 Continued
1
Logical Input Devices
These are input devices as viewed from inside the application program.
There are six main types.
2
Programming Event-Driven Input
We base the programming of input and interaction on events. For example,
a mouse event occurs when one of the mouse buttons is either depressed
or released. In GLUT, we can associate a function called a callback with
each specific event. The display function we wrote earlier was an example
of a callback.
A small square is drawn at the point (x, y) when the left mouse button
is depressed. The program exits when the right button is depressed. The
mouse callback must have the above form, and is specified, usually in main,
through the GLUT function
glutMouseFunc(mouse_callback)
3
Example 2. Reshape event
glViewport(0,0,w,h);
glClearColor(0.0, 0.0, 0.0, 0.0);
glClear(GL_COLOR_BUFFER_BIT);
glFlush();
}
This code resizes the clipping rectangle (in world coordinates!) to mimic
the new window size. We could instead keep the clipping rectangle fixed,
and adjust the viewport dimensions to keep a fixed aspect ratio.
4
Example 3. Idle callback
The idle callback is invoked when there are no other events. It can be
used to create animations. Consider a square rotating around the origin,
whose vertices lie on the unit circle:
(sin , cos )
(cos , sin )
( cos , sin )
(sin , cos )
5
The following code rotates the square:
void display()
{
glClear();
glBegin(GL_POLYGON);
thetar = theta/((2*3.14159)/360.0); /* radians */
glVertex2f(cos(thetar), sin(thetar));
glVertex2f(-sin(thetar), cos(thetar));
glVertex2f(-cos(thetar), -sin(thetar));
glVertex2f(sin(thetar), -cos(thetar));
glEnd();
}
void idle()
{
theta += 2.0; /* or some other amount */
if(theta >= 360.0) theta -= 360.0;
glutPostRedisplay();
}
In fact it doesnt work very well. Its better to use double buffering.