glFrustum and glPerspective difference?

Started by
6 comments, last by GekkoCube 20 years, 7 months ago
what is the difference? i tried both, not at the same time, and opengl appears to work. is glFrustum basically a more advanced viewing system over glPerspective?
Advertisement
i had the same question a few weeks ago...
gluPerspective calls glFrustum...
i made my own version of this function and some others to
eliminate dependency on the GLU library and shave a few kilobytes
off the EXE size... although i don''t remember exactly how i wrote
it (i''ve replaced it with a customized version), this should
work:

const double pi180 = 0.017453292519943295769236907684886;
// pi/180

// replacement for gluPerspective
void setPerspective(GLdouble fovy, GLdouble aspect, GLdouble zNear, GLdouble zFar){
GLdouble top, bottom, left, right;
top = zNear * tan(pi180*fovy/2);
bottom = -top;
right = aspect*top;
left = -right;
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glFrustum(left, right, bottom, top, zNear, zFar);
glMatrixMode(GL_MODELVIEW);
}

note: this function isn''t exactly like gluPerspective... the difference is the call the glLoadIdentity, and the glMatrixMode calls which i put there for convenience

this may or may not answer your question... but knowing how gluPerspective works enables you to use the more powerful glFrustum to customize the camera''s view.
I''d be very surprised if gluPerspective calls glFrustrum. That would create unnessicary overhead. Both functions perform the exact same operation, which is to generate a 4x4 perspective matrix. The only difference is in the values you pass them. glFrustrum requires that you specify the values for the upper, lower, left, and right clipping planes, while gluPerspective derives these values from the fov angle and the aspect ratio. It''s one of the simplest uses of matrix math there is. You could easily build your own function that incorporates the functionality of both, or any other system that you may prefer, without relying on either of them.

oh okay.
im sticking with glperspective.
thanks.
quote:Original post by Nemesis2k2
I''d be very surprised if gluPerspective calls glFrustrum. That would create unnessicary overhead.


overhead that probably nobody cares about because youre not going to call this function a million times per second. i dont know about glu, but for gl nobody is telling you how your implementation has to look like. you can always look at the mesa source to see how they are doing things.

f@dzhttp://festini.device-zero.de
quote:Original post by Nemesis2k2
I'd be very surprised if gluPerspective calls glFrustrum.


I agree with Trienco. I would be very surprised if gluPerspective does not call glFrustrum. "Both functions perform the exact same operation" is precisely the reason. No sense in duplicating code. Here's how I would implement gluPerspective:
void gluPerspective( GLdouble fovy, GLdouble aspect, GLdouble near, GLdouble far ){    GLdouble half_height = near * tan( fovy * 0.5 * TWOPI_OVER_360 );    GLdouble half_width = half_height * aspect;    glFrustum( -half_width, half_width, -half_height, half_height, near, far );}   
The only "unnecessary" overhead is the glFrustum call overhead and two negate operations. Who cares??? It is better than unnecessary duplication of code.

[edited by - JohnBolton on September 16, 2003 8:11:23 PM]
John BoltonLocomotive Games (THQ)Current Project: Destroy All Humans (Wii). IN STORES NOW!
I agree that the overhead is nothing, but still, it's not duplication of code. If you had a decent matrix class, You could implement glFrustrum in one line of code. I'm dead serious. One line. All the examples you've posted are doing is putting the values that were passed to gluPerspective in the form that glFrustrum wants them. That's pointless, especially when you can implement gluPerspective doing less operations than are done in glFrustrum.



[edited by - Nemesis2k2 on September 16, 2003 10:33:37 PM]
guess i can't disagree there. i'm setting up the "view" matrix from my "camera matrix" by doing the inversion myself. not because its crucial to optimize, simply because i dont like converting the vectors to something glulookat likes and have it reverse the conversion, doing unnecessary cross products and then invert. but here the frustrating part is that lookat internally builds exactly the matrix i already have before inverting it.

but boltons code just brought to my attention, that i shouldnt use perspective in my camera class, as it's using width/2 and height/2 anyway.

though still: there are no rules how an implementation of gluperspective and glfrustum should look like. just what goes in and what comes out.

just had a look and for a typical setup it can be simplified a little:
yFac = tanf(FOV * 3.14f/360);xFac = yFac*Aspect;	glMatrixMode(GL_PROJECTION);float PMtr[16]={1/xFac, 0, 0, 0,	                0, 1/yFac, 0, 0,		0, 0, -(far+near)/(far-near), -1,		0, 0, -(2*far*near)/(far-near), 0};	glLoadMatrixf(PMtr);glMatrixMode(GL_MODELVIEW);


(2*near)/(top-bottom):
top=near*tan(..), bottom=-near*tan(..)
extracting near leaves 2/(top-bottom)=2/(2*top)=1/top.
term simplified and calculating yFac for 1 instead of near.

having a nice symmetry the terms right+left and top+bottom evaluate to 0, removing the terms at (3,1) and (3,2).

guess it would really be wasteful to call the much more flexible glfrustum.. but i still dont think anybody cares ,-)

[edited by - Trienco on September 17, 2003 7:09:27 AM]
f@dzhttp://festini.device-zero.de

This topic is closed to new replies.

Advertisement