PDA

View Full Version : Problem with PROJECTION / MODELVIEW matrices



12-05-2002, 01:21 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;
m_f32CameraAngleY -= 0.5F;
}
else if ((m_bMoveCameraLeft == false) && (m_bMoveCameraRight == true))
{
//m_f32CameraPositionX -= 0.1F;
m_f32CameraAngleY += 0.5F;
}

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();

Mazy
12-05-2002, 02:42 AM
It may look the same, but it isnt..

You should NEVER ( unless you have very specific reasons) do anything else than projection in the Projection matrix..

reason : A number of operations like fog, lighting and texture coordinate calculations are made on then vertices after the modelview tranformation and before projection.. so screing up the projection matrix makes those calculations go wild.

12-05-2002, 03:34 AM
What I wanna see is like it's written in the red book:
glMatrix(GL_PROJECTION)
glTranslatef(0.0F, 0.0F, 5.0F);
EQUAL
glMatrix(GL_MODELVIEW)
glTranslatef(0.0F, 0.0F, -5.0F);
Am I wrong ?

zhhui
12-05-2002, 07:59 AM
Basically, if you want to use glTranslatef(), it always uses openGL like this
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(0.0F, 0.0F, 5.0F);

So, the answer to your question is NOT EQULE.


Originally posted by Matthieu DUFOURNEAUD:
What I wanna see is like it's written in the red book:
glMatrix(GL_PROJECTION)
glTranslatef(0.0F, 0.0F, 5.0F);
EQUAL
glMatrix(GL_MODELVIEW)
glTranslatef(0.0F, 0.0F, -5.0F);
Am I wrong ?

12-05-2002, 08:29 AM
On which matrix does process glTranslate ?
The one choose by glMatrixMode or always GL_MODELVIEW ?

kaysoft
12-05-2002, 10:54 AM
The one selected with glMatrixMode(...)