gluLookAt and ModelView matrix

Hi all, I have some problem with opengl’s modelview matrix. Say I change camera’s transform using:

glMatrixMode (GL_MODELVIEW);
glLoadIdentity ();
gluLookAt (20, 0, 0,
               20, 0, -10,
               0, 100, 0);

so that camera is now located at (20, 0, 0) in world-space and point to the negative z-axis, with my orthographic projection:

glMatrixMode (GL_PROJECTION);
glLoadIdentity ();
glOthro (-50, 50, -50, 50, -50, 50);

Then in my display function, there is a triangle need to be rendered, whose vertex is (0, 20, 0) (-10, 0, 0) (10, 0, 0) in world-space:

glMatrixMode (GL_MODELVIEW);
glLoadIdentity ();
glBegin(GL_TRIANGLES);
glColor3f (1, 0, 0);
glVertex3f (0, 20, 0);
glVertex3f (-10, 0, 0);
glVertex3f (10, 0, 0);
glEnd();

I reset modelview matrix to I-Matrix to make vertices of triangle in proper position in world-space. But OpenGL shows me a very different result, the triangle is still in the middle of my view-space, not offset to left by 20 units as I assumed. So, what’s wrong with this?

Here is the complete code:

#include <stdio.h>
#include <GLUT/GLUT.h>

void myDisplay ()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    
    // draw a triangle
    glMatrixMode (GL_MODELVIEW);
    glLoadIdentity ();
    glBegin(GL_TRIANGLES);
    glColor3f (1, 0, 0);
    glVertex3f (0, 20, 0);
    glVertex3f (-10, 0, 0);
    glVertex3f (10, 0, 0);
    glEnd();
    
    glFlush ();
}

int main (int argc, char** argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH);
    glutInitWindowPosition(0, 0);
    glutInitWindowSize(500, 500);
    glutCreateWindow ("Hello openGL");
    glutDisplayFunc(myDisplay);
    glClearColor(1, 1, 1, 1);
    
    glMatrixMode (GL_MODELVIEW);
    glLoadIdentity();
    gluLookAt (20, 0, 0,   // eye point
               20, 0, -10, // reference point
               0, 100, 0); // up vector
    
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(-50, 50, -50, 50, -50, 50);
    
    glutMainLoop();
    return 0;
}

glLoadIdentity() will reset the camera so you have to push and pop the ModelView matrix around the draw function

Thanks for reply, I added push/pop but not working anyway:

void myDisplay ()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    
    // draw a triangle
    glMatrixMode (GL_MODELVIEW);
    glPushMatrix();
    glLoadIdentity ();
    glBegin(GL_TRIANGLES);
    glColor3f (1, 0, 0);
    glVertex3f (0, 20, 0);
    glVertex3f (-10, 0, 0);
    glVertex3f (10, 0, 0);
    glEnd();
    glPopMatrix();
    glFlush ();
}

Well, it seems that gluLookAt just changes the View-Matrix, not model matrix. So, If I remove glLoadIdentity() in display callback, every thing works fine now. However, is there anyway in OpenGL to reset current Model-Matrix, not together with View-Matrix?

No not without using shaders but you can manager them separately and combine them yourself and load the combined matrix into the ModelView matrix.