What's up with glFrustum?

Started by
2 comments, last by AnalGoat 23 years, 6 months ago
i''ve been playing with OpenGL for a while now, but I can''t get glFrustum to work properly. What i mean is, i want to get rid of the ''glu'' commands in my programs (which includes gluPerspective). So i got a piece of paper and started to figure out how ''gluPerspective(60.0f,640.0f/480.0f,1.0f,100.0f)'' translates into a glFrustum call. glFrustum takes left right, bottom, top, near and far as parameters. So here is what i figured using my highschool junior level math: top=near * tan(FOV/2); bottom=-top; left=bottom/Aspect; right=top/Aspect; and then I call ''glFrustum(left,right,bottom,top,near,far)'', but it doesn''t look right. Are my calculations wrong? Thanks!
Advertisement
I suspect your problem is that tan() works with radians, not degrees, so your code that does this:
top=near * tan(FOV/2);
...should do this:
top=near * tan(FOV * PI / 360.0f);
Normally PI is defined as M_PI in math.h, but not always.

Here''s what my code looks like:

GLvoid ReSizeGLScene(GLsizei w, GLsizei h) // Resize And Initialize The GL Window
{
GLfloat nearp = 0.1;
GLfloat farp = 100.0;
GLfloat fov = 60;
GLfloat left, right, top, bottom, aspect;


if (h==0) // Prevent A Divide By Zero By
{
h=1; // Making Height Equal One
}

glViewport(0,0,w,h); // Reset The Current Viewport

glMatrixMode(GL_PROJECTION); // Select The Projection Matrix
glLoadIdentity(); // Reset The Projection Matrix

if(w>h)
{
aspect = w/h;
// fov*0.5 = arctan ((top-bottom)*0.5 / near) for glFrustrum
// Since bottom == -top for a symmetrical projection:
top = tan(fov*M_PI / 360.0f) * nearp;
bottom = -top;
// The left and right parameters are simply functions of the top, bottom, and aspect:
left = aspect * bottom;
right = aspect * top;
}
else
{
aspect = h/w;
right = tan(fov*0.5) * nearp;
left = -right;
bottom = aspect * left;
top = aspect * right;
}

glFrustum(left, right, bottom, top, nearp, farp);

glMatrixMode(GL_MODELVIEW); // Select The Modelview Matrix
glLoadIdentity(); // Reset The Modelview Matrix
}
i have a question
why do u wanna get rid of the glu commands?

http://members.xoom.com/myBollux
Here is some code specific to SpAsm but it might help ya:

[glFrustum_zfar:   R§  4.0 glFrustum_znear:  R§  1.0 glFrustum_top:    R§  0.6 glFrustum_bottom: R§ -0.6 glFrustum_right:  R§  0.6 glFrustum_left:   R§ -0.6]        mov ebx glFrustum_zfar          ; distances far depth clipping planes        push D§ebx+4        push D§ebx        mov ebx glFrustum_znear         ; distances near depth clipping planes        push D§ebx+4        push D§ebx        mov ebx glFrustum_top           ; coord. top horizontal clipping planes        push D§ebx+4        push D§ebx        mov ebx glFrustum_bottom        ; coord. bottom horiz. clipping planes        push D§ebx+4        push D§ebx        mov ebx glFrustum_right         ; coord. right vertical clipping planes        push D§ebx+4        push D§ebx        mov ebx glFrustum_left          ; coord. left vertical clipping planes        push D§ebx+4        push D§ebx        api 'OPENGL32.glFrustum'  




This code is a bit hard to understand. If you have any questions I can most likely answer them

Edited by - real_man on October 20, 2000 4:39:53 PM

This topic is closed to new replies.

Advertisement