PDA

View Full Version : Change the rotation of a 3D sphere to the exact opposite direction with GLUT



argamanza
04-07-2016, 01:14 AM
I have a program i created with a rotating sphere (glutWireSphere) and i like to change the rotation to the exact opposite direction.

For example if it rotates slowly CW on the Y axis i want it to rotate CCW.

Here is the code i have so far:


float angle = 0.;

void display() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glPushMatrix();
glTranslatef(0., -1.5, 0.);
glRotatef(angle, 0.5, 1., 0.5);
glColor3d(0, 0, 1);
glutWireSphere(2, 25, 15);
glPopMatrix();
glutSwapBuffers();
glFlush();
}

void idle() {
angle += 0.2;
if (angle > 360.) angle -= 360.;
glutPostRedisplay();
}

void myinit() {
glClearColor(1.0, 1.0, 1.0, 1.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-6., 6., -6., 6., -6., 6.);
glMatrixMode(GL_MODELVIEW);
}

void main(int argc, char** argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH | GLUT_RGB);
glutInitWindowSize(400, 400);
glutInitWindowPosition(10, 10);
glutCreateWindow("3d objects");
glutDisplayFunc(display);
glutIdleFunc(idle);
glEnable(GL_DEPTH_TEST);
myinit();
glutMainLoop();
}

To do what i just wrote i changed the values of the X, Y & Z arguments inside the glRotatef function of the display function to -0.5,-1.,-0.5 and i still get the exact same rotation.

I also tried to change the idle function to this (with the original glRotatef arguments):


void idle() {
angle -= 0.2;
if (angle < 0.) angle += 360.;
glutPostRedisplay();
}

And still, same rotation.

Am i missing something in my understanding here about the glRotatef function?

reaktor24
04-07-2016, 04:37 AM
Hve you tried: glRotatef(-angle, 0.5f, 1.0f, 0.5f);

argamanza
04-07-2016, 04:42 AM
Hve you tried: glRotatef(-angle, 0.5f, 1.0f, 0.5f);

Yeah, I did...

GClements
04-07-2016, 04:00 PM
How can you tell which direction the sphere is rotating? For a wireframe sphere with an orthographic projection and no lighting, the front and back will be indistinguishable. The movement of the vertices at the back will be a mirror image of those at the front, and you can't tell which is which. So changing the rotation direction will have no visible effect.

If you try it with a solid, texture-mapped sphere with depth tests enabled, you'll see that you are actually changing the direction of rotation. The difference should also be noticeable with a perspective projection provided that the viewpoint is significantly closer to the front of the sphere than to the back.