Here's my specific problem:
I wrote a little "free camera" application in C++, using OpenGL and SDL.
Its camera was controlled with keyboard & mouse, like in a first-person shooter.
To see where I was going I had it draw a 50x50x50 cube of octahedra:
Expand|Select|Wrap|Line Numbers
- int i, j, k, l = 500, m = 80; // l is the radius, m is the side length of the volume
- glBegin (GL_QUADS);
- for (i=0; i<m; i++)
- {
- for (j=0; j<m; j++)
- {
- for (k=0; k<m; k++)
- {
- glColor3f (1, 0.5, 0); // Draw a quad on the YZ plane
- glVertex3f (i*10000, j*10000+l, k*10000);
- glVertex3f (i*10000, j*10000, k*10000+l);
- glVertex3f (i*10000, j*10000-l, k*10000);
- glVertex3f (i*10000, j*10000, k*10000-l);
- glColor3f (1, 0.6, 0); // Draw a quad on the XY plane
- glVertex3f (i*10000, j*10000+l, k*10000);
- glVertex3f (i*10000+l, j*10000, k*10000);
- glVertex3f (i*10000, j*10000-l, k*10000);
- glVertex3f (i*10000-l, j*10000, k*10000);
- glColor3f (1, 0.7, 0); // Draw a quad on the XZ plane. Together they look look an octahedron
- glVertex3f (i*10000+l, j*10000, k*10000);
- glVertex3f (i*10000, j*10000, k*10000+l);
- glVertex3f (i*10000-l, j*10000, k*10000);
- glVertex3f (i*10000, j*10000, k*10000-l);
- }
- }
- }
- glEnd();
To make the camera move at the same speed regardless of framerate I figured this out: At the end of each frame I take the number of seconds since the last frame frame:
Expand|Select|Wrap|Line Numbers
- // double CT is the Current Time
- // double LT is the Last frame's Time
- // double DT is the Difference in Time between this frame and the last
- LT = CT;
- CT = clock();
- if (LT) DT = CT - LT; // LT is assigned zero at the beginning of the program. This way there's no time for the first frame.
- DT /= CLOCKS_PER_SEC;
- cout << CT << ' ' << DT << '\n';
Expand|Select|Wrap|Line Numbers
- // MFore = 1 means the "foreward" key is down
- // Player.Pos is the player's location
- // Player.AimX and AimY indicate where the player is aiming
- // MSpeed is the movement speed in OpenGL units/second
- if (MFore)
- {
- Player.Pos.X += (int) (sin(Player.AimX) * cos(Player.AimY) * MSpeed * DT);
- Player.Pos.Y += (int) (sin(Player.AimY) * MSpeed * DT);
- Player.Pos.Z += (int) (cos(Player.AimX) * cos(Player.AimY) * MSpeed * DT);
- }
Expand|Select|Wrap|Line Numbers
- 340000 0
- 590000 0.25
- 840000 0.25
- 1.09e+06 0.25
- 1.34e+06 0.25
When I run it using a display list to draw the octahedra it ends up looking like this:
Expand|Select|Wrap|Line Numbers
- 0 0
- 440000 0
- 440000 0
- 440000 0
- 440000 0
- 440000 0
- 440000 0
So I'm looking for a more precise, or just different way to get the framerate or clock time. Any ideas?
Written in C++ under Ubuntu 7.10 running on Intel Pentium 4 at 2.66GHz and ATI Radeon 9550SE (256MB).
Using OpenGL and SDL