Archived

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

a1studmuffin

Simple object-level frustum culling

Recommended Posts

Hi all, Everyone seems to have these cumbersome methods for getting the 6 clipping planes of the view frustum, but for basic object-level culling, what's wrong with just doing this:
  
// Assumptions:

// matView + matProj are our view and projection matrices

// vecTest is a D3DXVECTOR3 point in world space


// Initialize the frustum culling matrix:

D3DXMATRIX matCull = matView * matProj;

// Transform our world space point into a screen space point:

D3DXVECTOR3 vecTemp;
D3DXVec3TransformCoord(&vecTemp, &vecTest, &matCull);

// Test the point for visibility:

if (vecTemp.z > 0.0f &&
    // if object is in front of screen

    vecTemp.x > -1.0f && vecTemp.x < 1.0f &&
    // if object is within X range of frustum

    vecTemp.y > -1.0f && vecTemp.y < 1.0f)
    // if object is within Y range of frustum

    printf("Visible!\n");
else
    printf("Not visible!\n");
  
I'm currently using this in my Asteroids game and it seems to work fine, with a few minor drawbacks (as I can see it): 1) We aren't doing any Z-distance checking other than seeing if an object is behind us. This just means there is no far clip plane, which is fine for my app anyway because if an object is further than 256 units away it will wrap around behind you (pacman style). 2) Because we're only checking for a single point in space, objects will "pop" in and out on the edges of the screen. Problem 1 doesn't bother me as it's not an issue for my game, but number 2 is a bit annoying. Does anyone know how I would go about testing a sphere bounding volume instead of a single point? I'm guessing I just need to add the radius of the sphere into the equation somewhere, but where, and how? Also, can anyone see a reason why this method is bad? To me it seems a hell of a lot simpler than the 6-clipping plane method. Surely it wouldn't be slower? And it's certainly a lot leaner than those methods Perhaps the 6-clipping plane method is more designed for testing heaps of polys, whereas this is a more broad object-level cull? [edited by - a1studmuffin on April 30, 2002 12:33:41 AM]

Share this post


Link to post
Share on other sites
That works fine for points (in fact, that''s how they clip primitives like lines and triangles), but as you discovered, not so well for spheres. The problem is that if you transform a sphere into projection space coordinates, it''s no longer a sphere--I think it would be kind of egg shaped. Because of the weird shape, you wouldn''t be able to do a simple test similar to a sphere - axis aligned box test. Probably you''d have to test the volume against the 6 frustum planes, and then you''re better off doing that in world space because it would be a sphere!

Share this post


Link to post
Share on other sites
Ahh yes, a very valid point about the sphere/egg problem

I''ve been trying to work out some kind of relationship between the projected coordinate''s Z distance and the object''s radius and its size on screen.

Also, couldn''t I just make the sphere a little larger than it needs to be to compensate for the "egg" factor? I know this isn''t a perfect method but it would certainly suffice for what I''m trying to do

Cheers
- Michael

Share this post


Link to post
Share on other sites