PDA

View Full Version : Problem with GL_PROJECTION / GL_MODELVIEW matrices



Marmotte
12-05-2002, 11:17 AM
Does anyone know why this code works the same way when bProjectionNotModeview == TRUE or FALSE:
glClearColor(0.0, 0.0, 0.0, 1.0);
glClearDepth(1.0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0, 640.0 / 480.0, 1.0, 1000.0);

if ((m_bMoveCameraForward == true) && (m_bMoveCameraBackward == false))
{
m_f32CameraPositionZ += 0.1F;
}
else if ((m_bMoveCameraForward == false) && (m_bMoveCameraBackward == true))
{
m_f32CameraPositionZ -= 0.1F;
}

if ((m_bMoveCameraLeft == true) && (m_bMoveCameraRight == false))
{
m_f32CameraPositionX += 0.1F;
}
else if ((m_bMoveCameraLeft == false) && (m_bMoveCameraRight == true))
{
m_f32CameraPositionX -= 0.1F;
}

// !!! HERE BEGINS THE PROBLEM !!!
if (bProjectionNotModeview == true)
{
glMatrixMode(GL_PROJECTION);

glTranslatef(m_f32CameraPositionX, 0.0F, m_f32CameraPositionZ);
glRotatef(m_f32CameraAngleY, 0.0F, 1.0F, 0.0F);
glRotatef(m_f32CameraAngleX, 1.0F, 0.0F, 0.0F);

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
else
{
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

glTranslatef(m_f32CameraPositionX, 0.0F, m_f32CameraPositionZ);
glRotatef(m_f32CameraAngleY, 0.0F, 1.0F, 0.0F);
glRotatef(m_f32CameraAngleX, 1.0F, 0.0F, 0.0F);
}

glShadeModel(GL_SMOOTH);

glEnable(GL_DEPTH_TEST);

glBegin(GL_QUADS);
glColor3ub(255, 0, 0);
glVertex3f( 10.0F, 10.0F, 10.0F);
glColor3ub( 0, 255, 0);
glVertex3f(-10.0F, 10.0F, 10.0F);
glColor3ub( 0, 0, 255);
glVertex3f(-10.0F, -10.0F, 10.0F);
glColor3ub(255, 255, 0);
glVertex3f( 10.0F, -10.0F, 10.0F);

glColor3ub(255, 0, 0);
glVertex3f( 10.0F, 10.0F, -10.0F);
glColor3ub(255, 0, 0);
glVertex3f( 10.0F, 10.0F, 10.0F);
glColor3ub(255, 255, 0);
glVertex3f( 10.0F, -10.0F, 10.0F);
glColor3ub(255, 255, 0);
glVertex3f( 10.0F, -10.0F, -10.0F);

glColor3ub( 0, 255, 0);
glVertex3f(-10.0F, 10.0F, 10.0F);
glColor3ub( 0, 255, 0);
glVertex3f(-10.0F, 10.0F, -10.0F);
glColor3ub( 0, 0, 255);
glVertex3f(-10.0F, -10.0F, -10.0F);
glColor3ub( 0, 0, 255);
glVertex3f(-10.0F, -10.0F, 10.0F);

glColor3ub( 0, 255, 0);
glVertex3f(-10.0F, 10.0F, -10.0F);
glColor3ub(255, 0, 0);
glVertex3f( 10.0F, 10.0F, -10.0F);
glColor3ub(255, 255, 0);
glVertex3f( 10.0F, -10.0F, -10.0F);
glColor3ub( 0, 0, 255);
glVertex3f(-10.0F, -10.0F, -10.0F);
glEnd();

Coriolis
12-05-2002, 03:26 PM
Yeah... it's because it is supposed to. Each vertex gets transformed by the modelview and the projection matrices, so you could technically do any transform in one or the other of the two matrices and get the same result. Most implementations will pre-multiply the modelview and projection matrices together, though OpenGL does not require this.

Marmotte
12-09-2002, 05:31 AM
In the OpenGL Programming Guide it's written (page 117) that:

glMatrixMode(GL_PROJECTION);
glTranslatef(0.0, 0.0, 5.0);

as the same effect than:

glMatrixMode(GL_MODELVIEW);
glTranslatef(0.0, 0.0, -5.0);

So why my code works the same ?


Originally posted by Coriolis:
Yeah... it's because it is supposed to. Each vertex gets transformed by the modelview and the projection matrices, so you could technically do any transform in one or the other of the two matrices and get the same result. Most implementations will pre-multiply the modelview and projection matrices together, though OpenGL does not require this.

Korval
12-09-2002, 11:22 AM
Because the OpenGL programming guide is probably assuming that you're slipping a camera matrix or something of that nature in somewhere.

Now, it is important to note that your code is not 100% the same. For vertex position transforms, it is the same. But, in terms of lighting, no. Normals are only transformed by the modelview matrix; not the projection matrix. As such, you really do want to use the modelview matrix for what it says: Model-to-view transform. And the projection matrix should, idealy, only have the projection matrix (matrices output by glFustrum, gluProjection, glOrtho, and the rest of the gl/glu projection functions).

Obviously, if you aren't using lighting, you won't notice a difference.

zed
12-09-2002, 01:28 PM
btw the only thing that goes in the projection part is eg gluPerspective(...)
NO translating etc it screws up a few things
check the faq

vincoof
12-10-2002, 01:31 AM
yes it screws many things (especially lighting as Korval said), though for very special camera effects you could want to use it. Anyway don't do it unless you REALLY know what you're doing, c'est clair ? http://www.opengl.org/discussion_boards/ubb/wink.gif

[This message has been edited by vincoof (edited 12-10-2002).]