Small OpenGL matrix problem

Started by
5 comments, last by The_Fallen 21 years, 11 months ago
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
Advertisement
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

____________________________________________________________www.elf-stone.com | Automated GL Extension Loading: GLee 5.00 for Win32 and Linux

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 glLookAtDraw the static scene (no translation or rotating in this code!)Process the code from the last code  

What''s the fault?
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

____________________________________________________________www.elf-stone.com | Automated GL Extension Loading: GLee 5.00 for Win32 and Linux

Yeah, sounds good! Sometimes I''m really stupid... :/
Glad to help. Everyone learns somehow BTW.

____________________________________________________________
www.elf-stone.com

____________________________________________________________www.elf-stone.com | Automated GL Extension Loading: GLee 5.00 for Win32 and Linux

Yeah, I forgot to say thx, so here it is: Thank you very much!

This topic is closed to new replies.

Advertisement