• ### What is your GameDev Story?

#### Archived

This topic is now archived and is closed to further replies.

# What's up with glFrustum?

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

## Recommended Posts

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!

##### Share on other sites
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
}

##### Share on other sites
i have a question
why do u wanna get rid of the glu commands?

http://members.xoom.com/myBollux

##### Share on other sites
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

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 10
• 11
• 13
• 9
• 11
• ### Forum Statistics

• Total Topics
634092
• Total Posts
3015442
×