Moving object along z axis

Hello,I want to to transform the object (mesh ) :translate it along z axis (in the global coordinate system).
The problem is that it moves not along the z axis but diagonally (according to position /slope angle of the object) here is a snippet from the code.What do you think the problem is?


glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(...);
glTranslatef(0,0,n);

Maybe you should do a glLoadIdentity(); before applying transformation so the Modelview matrix is not containing data from previous operation…

Your code snippet is not very telling but that’s what I’m seeing.

Hope it helps.

Mick

well I did it I changed the first post

FYI: gluLookAt modifies the modelview matrix. You’re transforming your world coordinate system then translating along the z-axis.

Do it the other way around:

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(0,0,n);
gluLookAt(…);

the problem is that I want to translate only one object of many objects That I want to render on the scene
So the original code was:


glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(...);
for(all objects)
{
    if(object.mustTranslate)
       glTranslatef(0,0,n);
}

Like Aeluned said, the gluLookAt modify your coordinate system so applying a translate after it will translate according to the new system.

Also, doing this loop make every objects in your array of object (or whatever) translate from n. So your fifth object will actually translate by (0,0,5n). But maybe that is what you wanted ? If not your need to reset the modelview matrix after each object translation;

So something like thios :


glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
for(all objects)
{
    if(object.mustTranslate)
       glTranslatef(0,0,n);

   glLoadIdentity();
}
gluLookAt(...);


Mick

Thanks,
The problem is I have a lot of transformation to do
Translate1 Rotate1 Rotate2 Translate2 …
(some permutation of them)…
Because the order is important,how can I code this?

I suggest you read this section of the red book:

http://www.glprogramming.com/red/chapter03.html#name2

I’m also fairly new to OpenGL programming but reading this book is helping A LOT.

This section cover what you are trying to do, hope it helps.

Mick