# rotating spheres

I want to continuously rotate 2 spheres, however the rotation does not seem to work.

Here is my code:

float angle = 0.0f;

void light(){
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glEnable(GL_LIGHT1);

// Create light components
GLfloat positionlight1[] = { 9.0, 5.0, 1.0, 0.0 };
GLfloat positionlight2[] = {0.2,2.5,1.3,0.0};
GLfloat light_ambient1[] = { 0.0, 0.0, 1.0, 1.0};
GLfloat light_diffuse[] = { 1.0, 1.0, 1.0, 1.0 };

glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient1);
glLightfv(GL_LIGHT1, GL_DIFFUSE, light_diffuse);
glLightfv(GL_LIGHT0, GL_POSITION, positionlight1);
glLightfv(GL_LIGHT1, GL_POSITION, positionlight2);

}

void changeSize(int w, int h) {

if (h==0) // Prevent A Divide By Zero By
{
h=1; // Making Height Equal One
}

glMatrixMode(GL_PROJECTION); // Select The Projection Matrix
glLoadIdentity(); // Reset The Projection Matrix
glViewport(0,0,w,h);// Reset The Current Viewport
// Calculate The Aspect Ratio Of The Window
gluPerspective(45.0f,(GLfloat)w/(GLfloat)h,0.1f,100.0f);

glMatrixMode(GL_MODELVIEW); // Select The Modelview Matrix // Reset The Modelview Matrix
}

void renderScene(void) {

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glPushMatrix(); //set where to start the current object

glTranslatef(0.0,1.2,-6);
glRotatef(angle,0,1.2,-6);
glutSolidSphere(1,50,50);

glPopMatrix(); //end the current object transformations
glPushMatrix(); //set where to start the current object

glTranslatef(0.0,-2,-6);
glRotatef(angle,0,-2,-6);
glutSolidSphere(0.5,50,50);

glPopMatrix(); //end the current object transformations

angle=+0.1;
glutSwapBuffers();
}

int main(int argc, char **argv) {

// init GLUT and create window
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
glutInitWindowPosition(100,100);
glutInitWindowSize(500,500);
glutCreateWindow("Hello World");
// register callbacks
light();
glutDisplayFunc(renderScene);
glutReshapeFunc(changeSize);
glutIdleFunc(renderScene);

// enter GLUT event processing loop
glutMainLoop();

return 1;
}

Graphicstest::Graphicstest(void)
{
}


In the renderscene where i draw,translate and rotate my 2 spheres. It does not seem to rotate the spheres continuously. What am i doing wrong?

• Can you provide more details for what is happening, and what is supposed to happen? Is it supposed to rotate at a certain rate in a certain direction? Please see the FAQ for what is and isn't appropriate for gamedev. Your question as it stands is just "I wrote code, it doesn't work, here is all the code, what is wrong"? Without details helping is a lot harder. Nov 8, 2012 at 21:33
• you might want to try something that is not a sphere. It's kind of hard to see the rotation. just try using a cube instead (it's nice and pointy) Nov 8, 2012 at 22:22

The problem is this:

angle=+0.1;


You have your addition assignment the wrong way around. It should be:

angle += 0.1f;


When you fix that, it might rotate weirdly. That'll probably be because you're setting it to rotate around two axes (rather than one) in your glRotatef() function:

glRotatef(angle,0,-2,-6);


The last three digits correspond to the axis you want it to rotate around. So if you want to rotate just around the x-axis, you'd do:

glRotatef(angle, 1.0f, 0.0f, 0.0f);


Hope this helps (and is correct, heh). It might be an idea to look into the maths library glm (if you don't already know about it). Helps a lot with stuff like this.

• Nice work picking that up. Nov 8, 2012 at 22:39