View Full Version : Questions on some program code from the OpenGL SuperBible...

07-10-2000, 12:05 PM
i was wondering what the
"GLsizei rsize = 50" and the
"glutTimerFunc(33,TimerFunction, 1);"
are used for. 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);



07-10-2000, 05:28 PM
Originally posted by Y-T:
i was wondering what the
"GLsizei rsize = 50" and the
"glutTimerFunc(33,TimerFunction, 1);"
are used for. Thanx =)

rsize just stands for rectangle size... or size of the square... used for the collision detection and drawing.. that's all just a variable...

the TimerFunc.. I plan to go look at that when I am finished with the book, I am not sure if it's like Sleep() or it gives the code a certain amount of time to execute... good luck man

07-10-2000, 08:48 PM
or woman

07-11-2000, 05:03 AM
glutTimerFunc(33,TimerFunction, 1);

installs a timer callback.
TimerFunction will be called after 33 milliseconds. the last parameter (1 in this case) will be passed to TimerFunction.

TimerFunction must look somehow like this :

void TimerFunction(int val) {
//do something

note that the timer function is called
only once, so if you want that it will be
called again, you have to call glutTimerFunc again, at the end of your timer function.

imo this is a silly way to measure time
or synchronize graphics. the timer doesn't seem to be terribly accurate, and if youe want as interval 5 milliseconds or so...well, forget it. it's too slow.

personally i prefer timeGetTime() (i write win32 programs)
i use then glutIdleFunc to install a function
that simply calls glutPostRedisplay().
in my display function i get the time using
timeGetTime() and voilą.


07-11-2000, 06:06 AM
Thanx for all of ur replies! =)

Y-T(I'm a guy...)