Problem with OpenGL rotate

Hi,

I’m implementing a project in OpenGL and have a few questions in line with that.

My question-

I have a cube with some texture bound to its top face: I would like to rotate the cube about its Y-axis. I’m aware that this would need some kind of a fixed point rotation i.e. I translate the center of the cube to origin, then rotate the cube at origin and then translate it back. But my cube is of unit length dimensions, so its center comes out as (0,0,0). The cube is drawn at (1.1,0.8,-2.0). I’m confused with this. I tried the following as well-
glTranslatef(1.1,0.8,-2.0);
glRotatef(theta,0.0,1.0,0.0);
glTranslatef(-1.1,-0.8,2.0);
The cube seems to rotate about its Y-axis but its shifting its position slowly.

Any idea why this is happening ?

Thanks in advance.

I have the same problem.
I was going to write a post about it.

glTranslatef(1.1,0.8,-2.0);
glRotatef(theta,0.0,1.0,0.0);
glTranslatef(-1.1,-0.8,-2.0);

I think that’s bcuz you forgot the - before 2.0.

The cube seems to rotate about its Y-axis but its shifting its position slowly.
Any idea why this is happening ?

Calling glRotate multiple times in sequences is dangerous due to floating point precision error accumulation. When you rotate an object in an animation, use the “absolute” angle with a “fresh” matrix. Calling glRotatef(delta_angle,…) multiple times is not good in an animation.

Try this:


glLoadIdentity(); // call just after glClear(...)
...
theta += delta_theta; // limit this in the range [0 360]
glPushMatrix();
glTranslatef(1.1,0.8,-2.0);
glRotatef(theta,0.0,1.0,0.0);
glTranslatef(-1.1,-0.8,2.0);
draw_cube();
glPopMatrix();

This should work;

trinitrotoluene> Where does Andriyev calls glRotatef in sequence? You are right but I think that it is not the problem here.

my cube is of unit length dimensions, so its center comes out as (0,0,0). The cube is drawn at (1.1,0.8,-2.0)

As far as I understand, the cube is already centered at the origin in object space. So I think that there is a useless translation in your code, you should do:


glTranslatef(1.1,0.8,-2.0);
glRotatef(theta,0.0,1.0,0.0);

Remember that transformations are done in the reverse order because you use matrices. So here the rotation R is done before the translation T and lets say that v is a cube vertex, it will be transformed like this (in matrix operations terms):

v’ = T * R * v

After rereading the post of Andriyev,of course, dletozeun you give the right solution. Apparently I have misunderstood the problem. I have originally thought that the problem was an animation one.

Thanks a lot for all the replies…

I tried most of the stuff mentioned but the cube still doesn’t rotate about its y-axis (passes through the center of the top and bottom face), rather it rotates about some other axis which passes through some other points (both collinear in Y direction) on top and bottom face.

Another point (sorry for not mentioning it earlier) here is that, I’m also translating the cube along negative z-axis when the user presses Up/Down arrow key. This kind of gives zoom in/zoom out effect. I guess this affects my rotation as well as the fixed point about which I’m trying to rotate the cube changes every time I translate along z-axis.

Do I need to do some kind of book keeping to keep track of all my z motions and then adjust while I rotate ?

With the above piece of code I have given, I assumed that the current modelview matrix is identity and it looks like it is not because of your translation along Z.

If I understand correctly your problem, you don’t need to keep track of all your translation along Z axis. Just store the translation value along z (modified with the up/down arrow keys), and do something like this:



// your draw function
draw()
{
    ...
    glLoadIdentity();
    // Set up camera
    ...
    glPushMatrix();
    glLoadIdentity();
    
    // Translate the cube to the wanted position, z is the
    // translation value along z-axis. It is modfied by arrow keys
    glTranslatef(1.1,0.8, [b]z[/b]);
    // rotate around the y-axis
    glRotatef(theta,0.0,1.0,0.0);

    draw_cube();
    glPopMatrix();

    ...
}

Don’t forget to remove all glTranslate calls other than in the above piece of code. Or if it need to be applied to the whole scene like the camera transformation, use the glPushMatrix/glPopMatrix pair.

I’ve posted some code that does what I think the original poster is trying to do. It is a self-contained GLUT application. The crucial line is 107. This is an easy problem if you order the transformations properly. You can find it at:

   [http://pastebin.com/m5e9925d9](http://pastebin.com/m5e9925d9)

Hope this helps. I’m not doing someone’s homework for them am I?

Thanks a bunch for all the help…
Thanks to Max for the code snippet…(no it is not my home work, but I was just stuck at that point in my project…) :slight_smile: