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.

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.

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.

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

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

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

redraw() {
glMatrixMode( GL_PROJECTION );
glLoadMatrixd( perspective_proj );
glMatrixMode( GL_MODELVIEW );
glLoadIdentity();

(perspective mode instructions)

glMatrixMode( GL_PROJECTION );
glLoadMatrixd( ortho_proj );
glMatrixMode( GL_MODELVIEW );
glLoadIdentity();

(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

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=-1r and b=-1t

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.