Jump to content
  • Advertisement
Sign in to follow this  
Blue*Omega

Quick Trig Check (Frustum Culling)

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

Just want a confirmation that my math is correct (since I can't test it right now, I'm at work >_<) I'm working on an engine where the camera is always locked into looking down Z and the objects are always a certain distance from the camera (for the sake of simplicity just imagine that it's a sprite based engine.) Given these restrictions, I feel like I can safely reduce frustum culling down into a simple 2D bounding box test. I just need to calculate the "box" of the screen area and compare it against the "sprites" bounding box. Given a camera position (x, y, z) and a field of view (f) I think you would calculate the box as such: -z is effectively the distance from the drawing plane, so well call it d The frustum is, viewed from the side, an isometric triangle, so d splits it into two right triangles, with an angle of f adjacent to d. That would mean the side opposite of angle f is of a length d * tan(f), therefore the full legth of the visible plane is (d * tan(f)) * 2, with the other direction simply being that times the aspect ratio of the screen. Then of course you simply add x and y to those values to get the "real" box. This may be clearer: So let's say the camera is at (10, 5, -21) and the FOV is 25 degrees (yeah, that's an odd value, but 45 degrees isn't a good case test. I'll let you figure out why.) That means that half the screen width is 21*tan(25) = 9.79 Which means that half the screen height is 9.79 * 3/4 = 7.34 So our untranslated screen coords are (-9.79, -7.34) to (9.79, 7.34) Which translated would be (0.21, -2.34) to (19.79, 12.34) in "world space" Am I correct here? Just checking. (I'm really rusty on my trig) [Edited by - Blue*Omega on April 25, 2005 6:30:44 PM]

Share this post


Link to post
Share on other sites
Advertisement
Okay, so it looks like my equation was "close, but not quite". So, a couple of questions:

1) When setting up your projection matrix with gluPerspective(), is the value of the Field of View the "full" feild of view (from one edge of the screen to the next) or the "half" field of view? (For example, entering 45 would give an actual 90 degree FOV)

2) Is that FOV value Degrees or Raidians?

3) Does OpenGL do any other transforms to the view on it's own? It seems like the values I end up with are just a small percentage off all the time...

I'd appreciate any thoughts you might have! Thanks!

Share this post


Link to post
Share on other sites
You are correct, but just a minor point:

FOV usually means the angle from the bottom edge to the top edge. In that case, height = 2 * D * tan( fov/2 )

Share this post


Link to post
Share on other sites
Soooo..... after a good night of head bashing, punding the keyboard, and lots of muttered curses, I have discovered my problem!

When calling gluPerspective() I was calculating the aspect ratio as so:

float aspect = (float)( window.width / window.height );

This looked all well and good till I started stepping through the debug info and discovered... my aspect ration was coming out as 1.000000. Not good. So I shout some more obscenities and change the above code to:

float aspect = ( (float)window.width / (float)window.height );

and NOW we get our nice happy aspect of 1.333333 (or 1.77777 if your doing widescreen, but whatever), cubes actually look like cubes now (was wondering about that), AND the coordinates I'm calculating for my frustum culling actually make sense in screen space now! So many problems caused by bad casting...

Moral of the story: casting sucks.

Thanks for your help, though, JohnBolton!

[Edited by - Blue*Omega on April 26, 2005 9:30:58 AM]

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!