Archived

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

The_Fallen

OpenGL Small OpenGL matrix problem

Recommended Posts

Hi, I''ve got some trouble with the ogl matrices. The whole static scene renders perfectly, now I wanna implement some dynamic things like models etc. My drawing code looks like this:
  
glMatrixmode(GL_MODELVIEW); 
glPushMatrix(); 
glLoadIdentity(); 
glTranslate(...); 
Draw model...
glPopMatrix(); 
glMatrixMode(GL_PROJECTION);
  
The problem is, that glLoadIdentity sets the matrix to the camera pos and not to the center of the scene. So everything is rendered relatively to the camera pos and not relatively to the center of the scene... Any ideas, what I''m doing wrong? thx, fallen

Share this post


Link to post
Share on other sites
There''s no such thing as the camera pos. The camera pos, if you like, is always in the center of your scene. You create the illusion of moving the camera by moving the drawing position for all the objects in your scene, by translating or rotating the modelview matrix (which is why you have to do everything in reverse).

When you call glLoadIdentity, your drawing position is set to the center of the scene. If you then call glTranslate (1,0,0) its like moving the camera (-1,0,0) OR moving all objects (1,0,0).

____________________________________________________________
www.elf-stone.com

Share this post


Link to post
Share on other sites
Yes, that''s it!
But why in the name of hell is ogl drawing the models relatively to the camera position?
I move my camera not by translating the modelview matrix, but with glLookAt. Does that make any difference?
My whole rendering code looks like this:

  
Move camera with glLookAt
Draw the static scene (no translation or rotating in this code!)
Process the code from the last code

What''s the fault?

Share this post


Link to post
Share on other sites
When you call glLoadIdentity, you''re resetting the modelview matrix. You should put the gluLookAt call after glLoadIdentity. Also, your first call to glPushMatrix serves no purpose. It should be after the the gluLookAt call. That way, you save your camera matrix on the stack.

Also, I suspect you''re gluLookAt call was in the projection matrix. It should be done on the modelview matrix. The projection matrix is only for setting up your projection, not for positioning the camera. You should leave the matrix mode on gl_modelview, and only change it to gl_projection on the rare occasions when you want to change the projection.

Like this:

  
glMatrixmode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(...);
glPushMatrix();
glTranslate(...);
Draw model...
glPopMatrix();


____________________________________________________________
www.elf-stone.com

Share this post


Link to post
Share on other sites