Back and forth rotation on Y-axis I want to write code for a cube rotating 10 degrees back and forth on its y-axis in an OpenGL application. Right now I have this written in my display method, however it depends on processor speed, and it may be more complicated than it needs to be. I would like to make the code independent of processor speed.

GLfloat rot = 10;
GLfloat angle = 0, angle2 = 0, angle3 = 0;

if (angle &lt; rot){
   glRotated(angle, 0, 1,0);                                  
   angle++;
}
else{
   if (angle2 &lt; rot * 2 ){
     glRotated(rot - angle2, 0, 1, 0);      
     angle2++;
    }

else{
    if (angle3 &lt; rot ){
     glRotated(-rot + angle3, 0, 1, 0);     
     angle3++;
    }

else{ angle = 0, angle2 = 0, angle3 = 0; }
In order to dissociate the rotation from the CPU speed, you need to use some sort of timing in your function. I recommend passing in a time value of some sort. The easiest thing is to pass in the number of seconds since the app started. How you do that depends on your particular OS, which you didn't specify. 

Once you have a timing constant, you can generate a triangle wave to control the rotation. This formula will oscillate between 0 and 1 over time:

double rotation = 2.0 * fabs(fmod(time, 1.0) - 0.5);


You can make it go from 0 to 10 by multiplying the result by 10:

rotation *= 10.0;


Then you plug that into your glRotated() call:

glRotated(rotation, 0, 1, 0);


If you want to make the motion a little smoother, you could replace the triangle wave with a sine wave, like this:

double rotation = 0.5 * (sin(time * 2.0 * M_PI + M_PI / 2.0) + 1.0);