Page 79 - Computer Graphics Handout
P. 79
functionality as having left, right, and middle buttons as we have assumed in this chapter. More information about these functions
is in the Suggested Readings section at the end of the chapter.
2.11.4 The Idle Callback
The idle callback is invoked when there are no other events. Its default is the null function pointer. A typical use of the idle callback
is to continue to generate graphical primitives through a display function while nothing else is happening. Another is to produce an
animated display.
Let’s do a simple extension to our triangle programthat rotates the triangle about the center of the window. Consider the two-
dimensional rotation in Figure 2.45. A point at (x, y) when rotated by φ degrees about the origin moves to a point (x_ , y_). We obtain
the equations of rotation by expressing both points in polar coordinates. If the original point is at x = r cos(θ),
y = r sin(θ),
then the rotated point is at
x_ = r cos(θ + φ) = r(cos(θ) cos(φ) − sin(θ) sin(φ)),
y_ = r sin(θ + φ) = r(cos(θ) sin(φ) + sin(θ) cos(φ)),
or
x_ = x cos(φ) − y sin(φ),
y_ = x sin(φ) + y cos(φ).
Instead of displaying a triangle using the entered vertex positions, first we will rotate the positions by a small angle each time. The
idle callback need only post a redisplay. In main, we specify an idle callback,
glutIdleFunc(idle);
The display callback not only changes the vertex positions butmust also send the new vertex data to the GPU as in the code for
which the angle is 1/1000 of a degree:
const float DegreesToRadians = M_PI / 180.0;
float angle = 0.001*DegreesToRadians; // small angle in radians
void display()
{
for( int i = 0; i < 3; i++)
{
float x = cos(angle)*points[i].x - sin(angle)*points[i].y;
float y = sin(angle)*points[i].x + cos(angle)*points[i].y;
points[i].x = x;
points[i].y = y;
}
glBufferData(GL_ARRAY_BUFFER, sizeof(points), points,
GL_STATIC_DRAW);
glClear(GL_COLOR_BUFFER_BIT); // clear the window
glDrawArrays(GL_TRIANGLES, 0, 3);
glFlush();
}
The idle function is just
void idle()
79

