PDA

View Full Version : glLoadIdentity not working w/ perspective project



renderz
01-27-2009, 06:45 PM
Hi,
I am playing around with the sample code from red book now and having difficulty understanding the behavior of glLoadIdentity. It's suppose to clear the transformation on the current matrix, right? Well, when the it gets called when the projection is ortho it works like a charm but when the project is using perspective(via gluPerspective) nothing gets drawn.

I am posting a code here which has two functions display() and reshape(). This code will show blank screen but if the commented section for glOrtho is uncommented and you comment gluPerspective call you can actually see triangles being drawn.

I don't think this is how glLoadIdentity is suppose to work. Can someone tell me why this is happening? Thanks!



void display(void)
{
glClear (GL_COLOR_BUFFER_BIT);
glColor3f (1.0, 1.0, 1.0);

glTranslatef(0.0f, 0.0f, -100.0f);
glPushMatrix();

glLoadIdentity ();
glColor3f (1.0, 1.0, 1.0);
draw_triangle ();

glEnable (GL_LINE_STIPPLE);
glLineStipple (1, 0xF0F0);
glLoadIdentity ();
glTranslatef (-20.0, 0.0, 0.0);
draw_triangle ();

glLineStipple (1, 0xF00F);
glLoadIdentity ();
glScalef (1.5, 0.5, 1.0);
draw_triangle ();

glLineStipple (1, 0x8888);
glLoadIdentity ();
glRotatef (90.0, 0.0, 0.0, 1.0);
draw_triangle ();
glDisable (GL_LINE_STIPPLE);

glPopMatrix();

glFlush ();
}

void reshape (int w, int h)
{
glViewport (0, 0, (GLsizei) w, (GLsizei) h);
glMatrixMode (GL_PROJECTION);
glLoadIdentity ();

//Get aspect ratio.
GLdouble dAspectRatio = w/h;
GLdouble dPerspectiveAngle = 60.0f;
//Set perspective frustum.
gluPerspective(dPerspectiveAngle, dAspectRatio, 1, 150);

//if (w <= h)
// glOrtho (-50.0, 50.0, -50.0*(GLfloat)h/(GLfloat)w,
// 50.0*(GLfloat)h/(GLfloat)w, -1.0, 1.0);
//else
// glOrtho (-50.0*(GLfloat)w/(GLfloat)h,
// 50.0*(GLfloat)w/(GLfloat)h, -50.0, 50.0, -1.0, 1.0);
glMatrixMode(GL_MODELVIEW);
}

Xmas
01-28-2009, 03:25 AM
You're using a znear, zfar range of [-1, 1] when calling glOrtho but [1, 150] for gluPerspective. So it's quite likely your triangles are only in the [-1, 1] range in Z direction.

You probably want the line glTranslatef(0.0f, 0.0f, -100.0f) to have an effect, but since you call glLoadIdentity before drawing anything that translation gets cleared as well.