Jump to content
  • Advertisement
Sign in to follow this  
opendep

Perspective and Orthographic Projections

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
Share on other sites
Quote:
Original post by opendep
Is 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 glFrustum
Depth 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 gluPerspective
Depth 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/zNear
roughly 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 this post


Link to post
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

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!