# Thread: Perspective and Ortho switch

1. ## Perspective and Ortho switch

My question is:

Does anyone know where I could find the mathematics (or code) to switch a 3D scene from Ortho to Perspective (and reverse)? Nearest vertex should stay at the same raster position.

Thanx.

2. ## Re: Perspective and Ortho switch

As far as I know, calling glOrtho(l, r, b, t, n, f) and glFrustum(l, r, b, t, n, f), where l, b and n is the same in both cases, will always cause the point on the near clipping plane to be projected on the same pixels in the viewport. You can always calculate either l, b and n for glOrtho, or calculate fov and near for gluPerspective.

3. ## Re: Perspective and Ortho switch

Bob, mathematically I think you're right, but I can imagine that some drivers might use the glOrtho call to optimize away perspective divides etc, but wouldn't spot that they could do the same thing with the special-case glFrustrum.

4. ## Re: Perspective and Ortho switch

This is what I use to switch between ortho and perspective:

double ortho_proj[16];
double perspective_proj[16];

init() {
glMatrixMode( GL_PROJECTION );
glOrtho( 0, 640, 0, 480, 0, 1 );
glGetDoublev( GL_PROJECTION_MATRIX, ortho_proj );
gluPerspective( 40, 1, 1, 200 );
glGetDoublev( GL_PROJECTION_MATRIX, perspective_proj );
}

redraw() {
glMatrixMode( GL_PROJECTION );
glMatrixMode( GL_MODELVIEW );

(perspective mode instructions)

glMatrixMode( GL_PROJECTION );
glMatrixMode( GL_MODELVIEW );

(ortho mode instructions)
}

You will probablly have to play with the values for glOrtho and glFrustum as Bob suggested in order to get the Nearest Vertex effect you're looking for (I'm not sure what you mean).

-Marrcke

5. ## Re: Perspective and Ortho switch

When I read MikeC's post, something slipped into my mind. I'm partly correct in my post above. But only in one special case, when glOrtho's parameter fulfill the following requirements: l=-1*r and b=-1*t

Since you can call glOrtho with "non-uniform" (is that what it's called?) parameters where they don't pass the above requirement, when l=1 and r=2 for exmample, you must take some special action if this is the case.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•