Archived

This topic is now archived and is closed to further replies.

nemezjah

Im stuck with glTranslatef()

Recommended Posts

Im using glTranslatef function to move some object (md2) somewhere into the space, for example 0,0,0. Next im setting up a camera look gluLookAt() on those coordinates. After this i have implemented the keys moving camera, and here is the problem. When i move my camera for example forward, the object moves also ! why?? please help... how to set something in his place, maybe there is other function than translatef...?

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
there is no glLookup, it''s glLookAt()

try this:
glLookAt(whatever);
glPushMatrix();
glLoadIdentity();
glTranslatef(whatever the object coord);
DrawObject();
glPopMatrix();

see if that works

Share this post


Link to post
Share on other sites
there is no glLookup, it''s glLookAt()

try this:
glLookAt(whatever);
glPushMatrix();
glLoadIdentity();
glTranslatef(whatever the object coord);
DrawObject();
glPopMatrix();

see if that works

Share this post


Link to post
Share on other sites
damn, now the object dissapeared somewhere in the "space". look this is my code for this:

inline int DrawGLScene(GLvoid){
glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT); glLoadIdentity(); gluLookAt(-20,40,-60,
-20,0,20,
-20,0,20);

// below, we''v got a map

glDisable(GL_BLEND);
glDisable(GL_LIGHTING);
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, texture[1]);
glBegin(GL_QUADS);
glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f, -2.0f, 0.0f);
glTexCoord2f(0.0f, 1.0f); glVertex3f( 40.0f, -2.0f, 0.0f);
glTexCoord2f(0.0f, 0.0f); glVertex3f( 40.0f, -2.0f, 40.0f);
glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -2.0f, 40.0f);
glEnd();


//and here we''v got a model

glPushMatrix();
glLoadIdentity();
glTranslatef(UP,1,5);
glShadeModel(GL_SMOOTH);
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, Md2_Texture_Modele.ID);

Md2_Modele.Animate(Md2_Modele.stateStart, Md2_Modele.stateEnd, .2f,20);

glPopMatrix();



and what''s goin'' wrong?

Share this post


Link to post
Share on other sites
quote:

there is no glLookup, it's glLookAt()

That's what I meant. For some reason I wrote gluLookup instead. Must be something about posting while half asleep.

DJ_GL's suggestion is completely wrong BTW.

quote:

glLookAt(whatever);
glPushMatrix();
glLoadIdentity();
glTranslatef(whatever the object coord);
DrawObject();
glPopMatrix();


The glLoadIdentity call would clear the camera transform before you draw the object.

The correct order is:

glLoadIdentity(); //clear the modelview matrix
glLookAt(whatever); //do the camera transform
glPushMatrix(); //store the camera transform
glTranslatef(whatever the object coord); //transform the object (multiply the camera transform by the object's transform)
DrawObject();
glPopMatrix(); //pop the matrix stack back to the camera transform


____________________________________________________________
www.elf-stone.com

[edited by - benjamin bunny on August 11, 2002 2:23:30 PM]

Share this post


Link to post
Share on other sites
yea, sorry about that, take out the glLoadIdentity(). hehe, it's from a habit of writing. BTW, i wasn't completely wrong obviously if some of it was right. NEways, hehe, after work I stayed up til 9am and so I was really tired when writing that. I just got my 3D selection program selecting correctly using gluProject and gluUnProect.

(not that i need to show this any). it's all win32 coding so it's not any fancy MFC code (as I hate MFC code) and I'm so glad I get the correct 2D to 3D coordinates.



really beautiful now tho...

[edited by - DJ_GL on August 11, 2002 6:06:53 PM]

Share this post


Link to post
Share on other sites
What will happen if i call gluLoadIdentity() before every movement of some object like:

gluLoadIdentity();
glTranslatef(...);
some_object_here();

gluLoadIdentity();
glTranslatef(...);
some_object_here2();

? Will it work, or the gluLoadIdentity(); should be called once ?

Share this post


Link to post
Share on other sites
I don''t think I''ve ever heard of gluLoadIdentity, but your object would stay in the same spot on screen regardless of the camera''s orientation and position. This kind of thing is good for text or....hmmmm...a cheap ghost effect?

_____________________________

And the Phoenix shall rise from the ashes...

--Thunder_Hawk -- ¦þ
______________________________

Share this post


Link to post
Share on other sites
I think he meant glLoadIdentity() Your object would stay in the same spot but the matrix OpenGL is using would be different, so your object wouldn't appear since the matrix reset itself using glLoadIdentity(). I could be wrong, this is only from my understanding.

[edited by - DJ_GL on August 11, 2002 7:50:40 PM]

Share this post


Link to post
Share on other sites
quote:
Original post by nemezjah
What will happen if i call gluLoadIdentity() before every movement of some object like:

gluLoadIdentity();
glTranslatef(...);
some_object_here();

gluLoadIdentity();
glTranslatef(...);
some_object_here2();

? Will it work, or the gluLoadIdentity(); should be called once ?



By calling glLoadIdentity, you're clearing camera transform before you draw the object, which means the camera's position and orientation will make no difference to where the object is drawn on the screen (if it's drawn on the screen at all). Your gluLookAt call is effectively useless.

As already stated, your draw scene method should look something like this:

glLoadIdentity(); //clear the modelview matrix
glLookAt(whatever); //do the camera transform
glPushMatrix(); //store the camera transform

glTranslatef(whatever the object coord); //transform the object (multiply the camera transform by the object's transform)
DrawObject();
glPopMatrix(); //pop the matrix stack back to the camera transform


I'll elaborate. At the start of your frame, you want the modelview matrix to be an identity matrix. That is

1,0,0,0
0,1,0,0
0,0,1,0
0,0,0,1

This matrix specifies a camera at position 0,0,0, pointing down the Z axis.

Then, you call gluLookAt, which multiplies the modelview matrix by a camera transform you specify. This changes the matrix to specify the camera transform you want.

Now, to draw an object at 10,10,10 in your world, you call


glPushMatrix();
glTranslatef(10,10,10);
drawObject();
glPopMatrix();


*500 error*

____________________________________________________________
www.elf-stone.com

[edited by - benjamin bunny on August 11, 2002 10:31:02 PM]

Share this post


Link to post
Share on other sites