Getting the right perspective

I am working with GLUT, simply trying to draw polygons in 3D and rotate the camera. I have drawn a triangle near the origin, and I can get it to display at first, without trying to implement perspective. However, as soon as I implement perspective, nothing appears on the screen.
At first I thought that the problem is the parameters of gluPerspective were incorrect, but they seem right.

In my GLUT display function, I have:

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glTranslatef(0.0, 0.0, 0.0);
glBegin(GL_TRIANGLES);
glColor3f(1.0f,0.0f,0.0f); // Red
glVertex3f( 0.0f, 0.5f, 0.0f);
glVertex3f(-0.5f,-0.5f, 0.5f);
glVertex3f( 0.5f,-0.5f, 0.5f);
glEnd();

And in my window reshape function, I have:

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glViewport(0, 0, width, height);
gluPerspective(30,1,1,100); /* this line is the problem */
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(0,0,5, 0,0,0, 0,1,0);

When I comment out the call to gluPerspective, I see my triangle fine; but when gluPerspective is called (as above), I get a blank screen.

This problem has been agonizing! Thanks in advance.

P-Sz

Yur triangle is not appearing because you are drawing it at the origin, but that is not visible given your frustum.

Solution: give your glTranslatef() call a Z value < -1.

Also, your gluLookAt() call is not doing anything for you, because you’re loading the identity matrix right on top of it. You could also fix the problem by (re)moving the glLoadIdentity() call at the beginning of your redraw code.

Thank you. When I translate the triangle to z=-2, I now can view my triangle.

This draws the same triangle around the point (0,0,-2), correct? Now if I execute the same gluLookAt call above, shouldn’t my camera move to (0,0,5) and look at (0,0,2) (along the z axis), thus getting everything near the z-axis between z = 4 and z = -95? Thus my triangle should be visible?

Why do I not want to load the identity matrix each time before I call gluLookAt()? I want my parameters in gluLookAt to be relative to the origin (a “fresh” matrix operation), so therefore I should load the identity matrix into GL_MODELVIEW, right?

Thanks again,

P-Sz

This draws the same triangle around the point (0,0,-2), correct?

yes, that’s new new origin.

Now if I execute the same gluLookAt call above, shouldn’t my camera move to (0,0,5) and look at (0,0,2) (along the z axis), thus getting everything near the z-axis between z = 4 and z = -95? Thus my triangle should be visible?

If you did call gluLookAt(), it would be just like calling
glTranslatef(0.0, 0.0, -5.0);
for this case. So that means (-2.0)+(-5.0)=-7.0
and that’s like calling a single
glTranslatef(0.0, 0.0, -7.0);

Why do I not want to load the identity matrix each time before I call gluLookAt()? I want my parameters in gluLookAt to be relative to the origin (a “fresh” matrix operation), so therefore I should load the identity matrix into GL_MODELVIEW, right?

gluLookAt is a waste because you are calling glLoadIdentity in you drawing code. You should do this instead

glPushMatrix(); //saves the current matrix
glTranslatef(0.0,0.0,-2.0);
//draw stuff
glPopMatrix(); //restores the old matrix

V-man