Search code examples
c++openglglrotate

glRotatef() works weird in Opengl


I'm about to make a rotating sphere in C++ OpenGL, MFC Application. so I declared a variable that has a spinning velocity in demo.h :

GLfloat angle;
void Init();

Also, I initialized that variable and implemented one normal sphere at xyz(0,0,0) in demo.cpp :

Init();
angle += 1;
glRotatef(angle, 0, 1, 0);
GLUquadricObj* a = gluNewQuadric();
gluSphere(a, 10, 100, 100);  //radius = 10

Init() is user-defined function that initiates the value of angle variable :

void Init() = {
    angle = 1.0;
}

In this situation, the sphere spins well. But if I change angle += 1; to angle += angle; , then the Sphere does not rotate at the same speed and finally disappears :( I don't know what is the difference between these two. Is there a problem with applying "+=" operator to GLfloat type variables?


Solution

  • angle += angle doubles the rotation value every update. Depending on your update frequency, the spinning will almost immediately become completely erratic, and within a few seconds at most overflow the possible values for a float, thus becoming INFINITY, which OpenGL most likely errors out on.