Programming

Notes on programming for the labs and assignments.

Avoiding flashes/synch problems

Simple OpenGL programs can update the display hundreds of times per second in the lab because the PCs have fast nVidia graphics cards and because the update rate is not limited to that at which the monitor is actually redrawing. You can force 3D buffer swaps to occur only during the vertical retrace interval by setting an environment variable in the terminal window before running the program. Use
$ setenv __GL_SYNC_TO_VBLANK 1
or
$ export __GL_SYNC_TO_VBLANK=1

for tcsh or bash respectively. (Two underscores at the start of the name.)

System clock

To get the number of seconds as a floating point value since the program began, the Java code could look like:
import java.util.*;
// Initialise
static long Start;
Date now = new Date();
Start = now.getTime();
...
// Current time
Date now = new Date();
float seconds;
seconds = (float)(now.getTime() - Start) / 1000.0f;
and the C code:
#include <sys/time.h>
...
/* Initialise */
static long Start;
struct timeval now;
gettimeofday(&now, NULL);
Start = now.tv_sec;
...
/* Current time */
struct timeval now;
float  seconds;
gettimeofday(&now, NULL);
seconds = (float)(now.tv_sec - Start) + (float)now.tv_usec / 1000000.0;

Possible loss of precision

The Java compiler will complain about a "loss of precision" in
float something = 1.5;
To shut it up, put an f on the end of the number, eg 1.5f

Key/character codes

In Java and C, ASCII characters are also integers. In an event handler you can write
void keyboard (int key, int x, int y)
{
if (key == 'r') ...
For special keys such as escape or arrow you should use CONSTANT names rather than writing the numbers directly. They can change on different windowing systems.