PDA

View Full Version : Moving object along z axis



yakovm
04-09-2010, 12:09 PM
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);

FrimaMichael
04-09-2010, 12:19 PM
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

yakovm
04-09-2010, 12:20 PM
well I did it I changed the first post

Aeluned
04-09-2010, 12:26 PM
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(...);

yakovm
04-09-2010, 12:36 PM
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);
}

FrimaMichael
04-09-2010, 12:51 PM
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

yakovm
04-09-2010, 01:00 PM
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?

FrimaMichael
04-09-2010, 01:10 PM
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