Jump to content
  • Advertisement
Sign in to follow this  
kosmon_x

Computing the bounding sphere of a viewing frustum

This topic is 4725 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

Hey all, I'm trying to compute the bounding sphere of my viewing frustum and am having some problems, namely that all objects appear to intersect the sphere no matter where they are. Here is the code I'm using to calculate the sphere:
void Frustum::CalcBoundSphere( const D3DXVECTOR3& Pos, const D3DXVECTOR3& Look, float FOV, float Aspect, float Near, float Far )
{
	float fLen = Far - Near;
	float fHeight = fLen * tan( FOV * 0.5f );
	float fWidth = Aspect * fHeight;

	D3DXVECTOR3 P( 0.0f, 0.0f, Near + (fLen * 0.5f) );
	D3DXVECTOR3 Q( fWidth, fHeight, fLen );
	D3DXVECTOR3 Diff( P - Q );

	m_BoundSphere.Radius = D3DXVec3Length( &Diff );
	m_BoundSphere.Center = Pos + (Look * ((fLen * 0.5f) + Near ));
}


It's the source off of an article on flipcode. Anyone know what I could be doing wrong, or know of a better way to calculate a sphere containing the frustum? Thanks :)

Share this post


Link to post
Share on other sites
Advertisement
I'm too tired to go through the maths properly right now, but:

1) the FOV passed into that function needs to be in radians, if you passed it in degrees, the magnitude of Q would end up quite big, and so would your bounding sphere.

2) I'm not sure which method (if any) that code is based on, but it feels like if anything it'd return a sphere smaller than the frustum rather than a perfect circumsphere.

3) A "look" vector that wasn't unit length (normalised), a "far" value less than the "near" value or a dodgy camera position wouldn't do good things there.

Share this post


Link to post
Share on other sites
Hm those are all good things to look out for. I'm using correct values for the Near/Far and my Look is normalized before the call. I'm also using the same FOV that I use for D3DXMatrixPerspectiveFovLH(), which is in radians.

Can anyone see what's wrong with this equation?

Share this post


Link to post
Share on other sites
Yeah, that's the formula I'm using to compute the sphere... Has anyone else used it can can confirm that it works correctly?

Share this post


Link to post
Share on other sites
I dunno, but you can always compute the bounding box (oriented or not) of the vertices of the frustum, and then the bounding sphere of that.

Share this post


Link to post
Share on other sites


I managed to implement this for my uni final year project. I had trouble with it, but it was a while ago, and have fogotten.

But here's some food for thought:

- Make sure your plane extraction is working properly.
- As you can see from his diagram, the sphere is pretty big,
if you have a long view distance, long far plane, that sphere
is gonna be pretty ******* big! In fact your encompassing at the minimum
twice what you need.
- Although sphere tests are fast, the recomputation each frame of the
position of the sphere might not be worth it, think this could be worked
around by some matrices though.

The results of my disertation indicated that the performance increase from this
technique was pretty much nothing, same as culling against the frustum planes.

The cone on the other hand is super quick, and very effective. If you can get your head round it, but eberly (god!) provides pseudocode to help you out.

These are just suggestions (brain dump), and i hope they help, but i think this test is just too much.

Share this post


Link to post
Share on other sites
Your code looks ok except that fWidth and fHeight should be calculated using Far instead of fLen. However, it won't help you because it makes the sphere slightly bigger.

Share this post


Link to post
Share on other sites
Wouldn't it just be as simple as using the two farthest points of the frustum? The two points on the frusum that are farthest from each other will never change. So find the center of those two points and use the distance to them as the radius. The distance to them should also never change unless you update your projection matrix. So your sphere size should normally remain constant. If you store the exact position the center point lies, you can transform the point into the camera state. That means your entire frustum bounding sphere can be computed with one matrix transform.

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.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!