xiewneqi

11-18-2013, 07:54 PM

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;

}

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;

}