• 10
• 10
• 12
• 12
• 14

# Perspective and Orthographic Projections

This topic is 3672 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi, I need to switch between perspective and orthographic projections during runtime. I have tried with glOrtho and gluPerspective but have nt succeeded. can anyone help me with this. My code is if(perspective_active == 1) { glMatrixMode(GL_PROJECTION); glPushMatrix(); glLoadIdentity(); gluPerspective(30,1.2,0.0,10.0); glMatrixMode(GL_MODELVIEW); } else { glMatrixMode(GL_PROJECTION); glPopMatrix(); glLoadIdentity(); glOrtho(-1.0,1.0,-1.0,1.0,1.0,-1.0); glMatrixMode(GL_MODELVIEW); } where perspective_active is the flag set when perspective projection is chosen. Thank you.

##### Share on other sites
I'd expect something like
glMatrixMode(GL_PROJECTION);glLoadIdentity();if(perspective_active == 1){   gluPerspective(30,1.2,0.0,10.0);}else{   glOrtho(-1.0,1.0,-1.0,1.0,1.0,-1.0);}glMatrixMode(GL_MODELVIEW);
to work. No need to push/pop anything here, as long as you don't want to do something special not mentioned in the OP. If it doesn't work, please tell us what happens instead.

##### Share on other sites
Why are you pushing and popping the matrices on and off the matrix stack directly before clearing them?
Also, you can use gluOrtho2D instead of glOrtho (if you are doing 2D drawing like GUI for example).

if(perspective_active == 1){	glMatrixMode(GL_PROJECTION);	glLoadIdentity();	gluPerspective(30,1.2,0.0,10.0);	glMatrixMode(GL_MODELVIEW);}else{	glMatrixMode(GL_PROJECTION);	glLoadIdentity();	gluOrtho2D(0, width_of_backbuffer, 0, height_of_backbuffer);	glMatrixMode(GL_MODELVIEW);}

##### Share on other sites
glOrtho should get higher values for left/right, top/bottom and near/far since with the setup you have, the frustum is just a cube with edge lengths of 2.0 centered at the camera.

I guess you are calling the code every frame, so why do you push the matrix with perspective and pop it with otho mode? glPushMatrix is used to save the current matrix for the current matrix mode whereas glPopMatrix is used to load the last saved matrix for current matrix mode. So unless you don't want to save the old projection matrix you don't need those calls.

Additionally, since the projection matrix seldom changes, why don't you use a function that sets the projection matrix only when changed? (Or do you do this already?)

Another important point: The near parameter for gluPerspective should never be 0! Otherwise you'll suffer severe precision problems in you depth buffer. Set it to 1.0 or at least to 0.1 instead.

##### Share on other sites
hi,
I tried using both glOrtho and glOrtho2D without pushing and popping the current matrix. My initial projection type is orthographic but when i try to switch to perspective, its not working. There is no change in the display. but when i switch back to orthographic projection from perspective the object moves to the bottom left corner of the screen if i use gluOrtho2D(0, 2.0, 0, 2.0).

Is there any other way to change to perspective projection apart from gluPerspective..?

Thanks a lot..

regards,
AM

##### Share on other sites
Quote:
 Original post by opendepIs there any other way to change to perspective projection apart from gluPerspective..?
glFrustum is OpenGL's own routine for perspective projection.

Have you considered Lord_Evil's hint? See
Quote:
 man page of glFrustumDepth buffer precision is affected by the values specified for near_val and far_val. The greater the ratio of far_val to near_val is, the less effective the depth buffer will be at distinguishing between surfaces that are near each other. If r=near_val roughly log2(r) bits of depth buffer precision are lost. Because r approaches infinity as near_val approaches 0, near_val must never be set to 0.

and
Quote:
 man page of gluPerspectiveDepth buffer precision is affected by the values specified for zNear and zFar. The greater the ratio of zFar to zNear is, the less effective the depth buffer will be at distinguishing between surfaces that are near each other. If r = zFar/zNearroughly log2(r) bits of depth buffer precision are lost. Because r approaches infinity as zNear approaches 0, zNear must never be set to 0.

Perhaps invoking gluPerspective (or glFrustum) fails since you specify zNear=0, and hence OpenGL does not change the projection!?

##### Share on other sites
hi,
Yea i did that and its working with gluPerspective now. The problem was with a glui live variable whose value did not change on switching to prespective projection.

Thanks a lot guys.. :)

regards,
AM