setting the near clip with gluPerspective

I want to change the setting of the near clip when drawing different parts of the scene so that certain parts of the scene are clipped and other parts are not clipped.

I tried this…

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0, (GLfloat) 640 /(GLfloat) 480, nearPlaneDist, 100.0f);
glMatrixMode(GL_MODELVIEW);

but it isn’t quite working correctly. My question is: Can I just call gluPerspective anywhere in my code? Or do I have to always call gluPerspective first, before doing any transformations? I guess I might need to do something like this…

glGetFloatv(GL_MODELVIEW_MATRIX,savedMatrix);
glLoadIdentity();

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0, (GLfloat) 640 /(GLfloat) 480, nearPlaneDist, 100.0f);
glMatrixMode(GL_MODELVIEW);

glMultMatrixf(savedMatrix);

Is there an easier/better way to do this? Thanks in advance!

You can set the projetion matrix anywhere you like.

Will changing the near plane affect the depth values at all?

If the far plane is far out compared to the near plane, changing the near plane will have a great impact on the precision of the depth buffer. You should always have the far plane as far out as you can possibly accept for best precision.

Well, I have the near clip out at 13 and the far at 100 and render. Then I change the near to 0.1 and render again and it seems like the depths are not correct because the last and closer polys rendered are partially in back of the first. Maybe I should look into glDepthRange and partition it into two areas? Does that sound like a good way to go?

Used glDepthRange(0.1,1.0) and glDepthRange(0.0,0.09999)…that fixed it. Thanks!