View Full Version : Questions reguarding some code from OpenGL SuperBible...

07-14-2000, 06:29 AM
hi i have a couple questions about some of this code which i got from the OpenGL SuperBible... first, why do they put the"int value" int the function "void TimerFunction(int value)" since it never seems to be used.... and what does the function "glutTimerFunc(33, TimerFunction, 1) actually do? Thanx,

// Bounce.c
// Demonstrates a simple animated rectangle program with GLUT
// OpenGL SuperBible, 2nd Edition
// Richard S. Wright Jr.

#include <windows.h>
#include <gl/glut.h>

// Initial square position and size
GLfloat x1 = 100.0f;
GLfloat y1 = 150.0f;
GLsizei rsize = 50;

// Step size in x and y directions
// (number of pixels to move each time)
GLfloat xstep = 1.0f;
GLfloat ystep = 1.0f;

// Keep track of windows changing width and height
GLfloat windowWidth;
GLfloat windowHeight;

// Called to draw scene
void RenderScene(void)
// Clear the window with current clearing color

// Set current drawing color to red
// R G B
glColor3f(1.0f, 0.0f, 0.0f);

// Draw a filled rectangle with current color
glRectf(x1, y1, x1+rsize, y1+rsize);

// Flush drawing commands

// Called by GLUT library when idle (window not being
// resized or moved)
void TimerFunction(int value)
// Reverse direction when you reach left or right edge
if(x1 > windowWidth-rsize &#0124; &#0124; x1 < 0)
xstep = -xstep;

// Reverse direction when you reach top or bottom edge
if(y1 > windowHeight-rsize &#0124; &#0124; y1 < 0)
ystep = -ystep;

// Check bounds. This is incase the window is made
// smaller and the rectangle is outside the new
// clipping volume
if(x1 > windowWidth-rsize)
x1 = windowWidth-rsize-1;

if(y1 > windowHeight-rsize)
y1 = windowHeight-rsize-1;

// Actually move the square
x1 += xstep;
y1 += ystep;

// Redraw the scene with new coordinates
glutTimerFunc(33,TimerFunction, 1);

// Setup the rendering state
void SetupRC(void)
// Set clear color to blue
glClearColor(0.0f, 0.0f, 1.0f, 1.0f);

// Called by GLUT library when the window has chanaged size
void ChangeSize(GLsizei w, GLsizei h)
// Prevent a divide by zero
if(h == 0)
h = 1;

// Set Viewport to window dimensions
glViewport(0, 0, w, h);

// Reset coordinate system

// Keep the square square, this time, save calculated
// width and height for later use
if (w <= h)
windowHeight = 250.0f*h/w;
windowWidth = 250.0f;
windowWidth = 250.0f*w/h;
windowHeight = 250.0f;

// Set the clipping volume
glOrtho(0.0f, windowWidth, 0.0f, windowHeight, 1.0f, -1.0f);


// Main program entry point
void main(void)
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
glutTimerFunc(33, TimerFunction, 1);



08-02-2000, 06:49 PM
I'm new to OpenGL but I can try to take a stab at your question.

The "int value" is in the function definition for the timer function because the timer function must be of the form void Something(int); The program never actually calls TimerFunction anywhere. It just passes the address of the function to GLUT (using glutTumerFunc(int wait, TIMERFUNC timerFunction, int parameter));

In the bounce.c example, the author calls:
glutTimerFunc(33, TimerFunction, 1);
Somewhere deep inside the GLUT libraries, something starts counting down 33 milliseconds (the first parameter). When 33 milliseconds are up, GLUT calls:
using the second parameter (the name of the function to call - which must be of the form void X(int); or else the call above is invalid) and the third parameter, 1. At the end of the timer function, notice that it starts the timer again.

Bounce.c doesn't use the parameter in the timer function, but it could. Imagine you wanted to render a looping animation that has 10 frames. Your timer function would be
void TimerFunction(int frame) {
DrawFrame(frame); // Draw the current frame
frame++; // Go to the next frame
if(frame = 10) // If we've drawn 10
frame = 0; // Start over

// Start timer for next frame in 30ms
glutTimerFunc(30, TimerFunction, frame)