1

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?

qkekehrtnfl97
  • 49
  • 1
  • 5
  • Angle += angle is multiplying by 2. Pretty quickly you'll hit the limit of what's representable as a float that way. – Flexo Nov 04 '20 at 10:04
  • `angle += angle;` is similar to what `angle *= 2;` would achieve. I.e. at each execution of statement the value of `angle` is doubled. If you try this on paper you will note how fast the value of `angle` will increase. Rather sooner than later you get giant values and finally an overflow of your `GLfloat`. (It's really worth to be aware of this for every programmer.) – Scheff's Cat Nov 04 '20 at 10:04

1 Answers1

3

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.

Sebastian Redl
  • 69,373
  • 8
  • 123
  • 157