• 10
• 9
• 13
• 10
• 18

# glRotate misunderstanding

This topic is 4033 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

there's a certain phenomena i don't understand let's say i want to move a cube across the negative z axis in code, this looks something like that
vector3 position(0, 0,  0);
velocity(0, 0, -1);

void Update(double dt)
{
position += velocity * dt;
}

void Display(void)
{
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glPushMatrix();

// move the camera up a bit
glTranslatef(0.0f, -1.0f, 0.0f);

glTranslatef(pos.x, pos.y, pos.z);
glutSolidCube(1.0f);
glPopMatrix();
glFlush();
}

void Idle()
{
// update the frame time
SortFrameTimer();
// update the game, use time amount since last query
Update(FrameTime());
// redraw the screen
glutPostRedisplay();
}


the rest of the functions are normal. this works and looks well. now i want to do something a bit different. i want to spin the cube abit on some axis (lets say the x axis), and move it again along the negative z axis. in code the only change is
void Display(void)
{
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glPushMatrix();

glRotatef(45.0f, 1.0f, 0.0f, 0.0f)  // NEW
// move the camera up a bit
glTranslatef(0.0f, -1.0f, 0.0f);

glTranslatef(pos.x, pos.y, pos.z);
glutSolidCube(1.0f);
glPopMatrix();
glFlush();
}


however i dont get the effect i want - instead of going along the negative z axis, it goes along some other line (looks like (1, 1, 0)), even though i rotated before translating. what is the proper way of doing this?

##### Share on other sites
If I get you, you have to put glRotate after grTranslate,because you have to think the multiplications as occuring in the opposite order(in a grand coordinate system) thus in order to rotate the object relative to an axis and then move it away you have to put glRotate after glTranslate in your code.

[Edited by - Lord_Vader on March 5, 2007 4:15:17 AM]

##### Share on other sites
well that's very weird but apparently it works. i don't remember ever doing things the opposite order, i thought opengl took care of it for me

##### Share on other sites
In OpenGL, the transformations happen in object space, not in the world space. Thus, if you rotate first and then translate, that translation happens along the new local axes that differ from the global coordinate axes after the rotation. It's completely intuitive if you think about it that way. And, as has been said, if you want to think in world coordinates, just reverse the order of transformations.

##### Share on other sites
Think of it this way:

On OpenGL, each operation pre-pends to the previous operation. The vertex follows the operation from last to first; it flows "backwards" through your code.

One reason for this is so that you can set up the camera transform once, and then render each object using glPushMatrix()/glPopMatrix() go get back to the camera transform (without object-specific transform).