Jump to content

  • Log In with Google      Sign In   
  • Create Account


Frustum Culling


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
6 replies to this topic

#1 _Slin_   Members   -  Reputation: 202

Like
0Likes
Like

Posted 13 May 2013 - 03:09 AM

I want to do some basic view frustum culling, using bounding spheres for my objects.

The spheres are positioned in world space and I am constructing planes for my frustum.

I then calculate the distance between my spheres centers and each plane and if it is bigger than the radius and on the wrong side, the object is culled. This can be optimized by first culling with a sphere around the frustum and maybe some caching for the plane that culled an object in the previous frame and generally works great. Something like this can be found easily online and is most probably good enough for most cases.

 

A problematic case looks like this:

CullingProblem.png

where "View" is the visible area, "PL" the left plane, "PB" the bottom plane and "P" the center of the sphere to cull.

It is obviously inside the top and right plane (where ever those are) and while the point is clearly on the wrong side of PL and PB, the distance to each minus the radius is inside the both planes, but still the sphere would not be visible.

This usually shouldn´t be a problem, but I am using it to cull lights for tiled shading, so I´ve got many small frustums and many in comparison big spheres, so this is a big issue.

 

The solution I came up with is simple, all I do is (additional to the culling above) storing the distances to the planes and using pythagoras to get the squared distance to the frustums corner which means for example for the case above: distToPL*distToPL+distToPB*distToPB

For visibility, the result can be compared to the squared radius.

 

Now I am wondering if this makes sense (I am not completely sure if maybe the perspective frustum breaks it? From my understanding it shuldn´t, but that doesn´t have to mean much...) and if there is any better alternative?



Sponsor:

#2 IggyZuk   Members   -  Reputation: 896

Like
0Likes
Like

Posted 13 May 2013 - 06:40 AM

I'm confused with the problem you're facing, all I can offer is some code I use for Frustum Culling. Hope it helps in any way.

 

/* Before rendering the object check if it is visible */
Object::IsVisible() {
	Frustum frustum = scene->GetCamera()->GetFrustum();
	frustum.Inside(this.position, this.radius);
}

/*
   Create: Plane planes[6]
   Iterate through all 6 planes, if the point is outside ANY plane;
   it's not visible
*/
bool Frustum::Inside(Vec3 point, float radius) {
	for (int i=0; i<6; ++i) {
		if (!planes[i].Inside(point, radius)) return false;
	}
	return true; // Point is inside all planes
}

/* Up to you to find out how PointToPlaneDistance function works */
bool Plane::Inside(Vec3 point, float radius) {
	float distance = Math::PointToPlaneDistance(this, point);
	return (distance >= -radius); //We are out of visible area
}

Start by doing what is necessary; then do what is possible; and suddenly you are doing the impossible.


#3 TMarques   Members   -  Reputation: 189

Like
1Likes
Like

Posted 13 May 2013 - 07:25 AM

Assuming you know the point of intersection (the corner) between PB and PL (let's call it PI), you can calculate a vector V = (P - PI). If (length(V) < sphereRadius) your point is inside, otherwise, check the dot product of normalized(V) with PB and PL plane normals, if and only if both values are negative, point is outside, if the comparison fails, it's inside.

 

Take this with a grain of salt since I've never tested or even considered this case when doing frustum culling, but this would be my first guess to solve this issue.

 

Hope it helped.


Tiago.MWeb Developer - Aspiring CG Programmer

#4 _Slin_   Members   -  Reputation: 202

Like
0Likes
Like

Posted 13 May 2013 - 08:22 AM

IgnatusZuk, thanks, but your code has the exact same problem I am trying to solve: There are some cases, where the function return visibility, while the object is actually outside the view frustum. In most cases I expect this to be just very few objects, so anything more complicated for culling is not worth it.

 

TMarques, I may should have mentioned that I am culling spheres in a 3D frustum, so those corners are actually lines and not points, but my approach to get that V as the distance to the planes should be fine, except the thing you mentioned, where I have to check which side of the planes the center is on, which explains why now in my implementation too many lights are culled.

Thank you very much for that hint :)

 

If there are any more ideas, please post them, as I´d love to find a better way if there is one :)



#5 Norman Barrows   Crossbones+   -  Reputation: 1845

Like
-1Likes
Like

Posted 13 May 2013 - 11:03 AM

 
 
 
 
its not clear to me from your description whether this problem must be solved in 3d or can be solved in 2d.
 
if you just want to test if the lit area from a light is in view, you may be able to cull in 2d, perhaps with something as simple as bounding box distance.
 
if you're culling a large light sphere in 3d (hanging from a post, etc, FPS shooter view), you might try "cull if outside all 6 planes" vs "don't cull if within any of the 6 planes".
 
here's the code i use to cull large chunks to the frustum. the chunks are 300x300. the far plane is 1000. the frustum is NOT tilted up/down. chuncks are only clipped to the near, far, left, and right planes:
 
// calculate the planes 
 
 
void Zcalc_frustum_planes2()          // rotation only
{
D3DXMATRIX  projectionMatrix,       // ROCKLAND: copy of current projection matrix
            viewMatrix,             // ROCKLAND: copy of current view matrix
            matrix,m;                 // ROCKLAND: frustum matrix  ( view * modified proj )
projectionMatrix = Zprojection_matrix;
D3DXMatrixTranslation(&viewMatrix,-Zcamx,-Zcamy,-Zcamz);
D3DXMatrixRotationY(&m,-Zcamyr);
D3DXMatrixMultiply(&viewMatrix,&viewMatrix,&m);
D3DXMatrixMultiply(&matrix, &viewMatrix, &projectionMatrix);
// Calculate left plane of frustum.
m_planes2[2].a = matrix._14 + matrix._11; 
m_planes2[2].b = matrix._24 + matrix._21;
m_planes2[2].c = matrix._34 + matrix._31;
m_planes2[2].d = matrix._44 + matrix._41;
D3DXPlaneNormalize(&m_planes2[2], &m_planes2[2]);
// Calculate right plane of frustum.
m_planes2[3].a = matrix._14 - matrix._11; 
m_planes2[3].b = matrix._24 - matrix._21;
m_planes2[3].c = matrix._34 - matrix._31;
m_planes2[3].d = matrix._44 - matrix._41;
D3DXPlaneNormalize(&m_planes2[3], &m_planes2[3]);
// Calculate top plane of frustum.
m_planes2[4].a = matrix._14 - matrix._12; 
m_planes2[4].b = matrix._24 - matrix._22;
m_planes2[4].c = matrix._34 - matrix._32;
m_planes2[4].d = matrix._44 - matrix._42;
D3DXPlaneNormalize(&m_planes2[4], &m_planes2[4]);
// Calculate bottom plane of frustum.
m_planes2[5].a = matrix._14 + matrix._12;
m_planes2[5].b = matrix._24 + matrix._22;
m_planes2[5].c = matrix._34 + matrix._32;
m_planes2[5].d = matrix._44 + matrix._42;
D3DXPlaneNormalize(&m_planes2[5], &m_planes2[5]);
// Calculate near plane of frustum.
m_planes2[0].a = matrix._13;        
m_planes2[0].b = matrix._23;        
m_planes2[0].c = matrix._33;        
m_planes2[0].d = matrix._43;        
D3DXPlaneNormalize(&m_planes2[0], &m_planes2[0]);
// Calculate far plane of frustum.
m_planes2[1].a = matrix._14 - matrix._13; 
m_planes2[1].b = matrix._24 - matrix._23;
m_planes2[1].c = matrix._34 - matrix._33;
m_planes2[1].d = matrix._44 - matrix._43;
D3DXPlaneNormalize(&m_planes2[1], &m_planes2[1]);
}
 
 
 
 
// test corner vs plane.
 
int Zpoint_inside_frustum_plane(int x,int y,int z,int plane_index)
{
D3DXVECTOR3 v;
v.x=(float)x;
v.y=(float)y;
v.z=(float)z;
if ( D3DXPlaneDotCoord(&m_planes2[plane_index],&v) <  0.0f )  
    {
return(0);
}
return(1);
}
 
 
 
// test area vs plane.
 
int Zarea_inside_frustum_plane(int x1,int z1,int x2,int z2,int plane_index)
{
if ( Zpoint_inside_frustum_plane(x1,0,z1,plane_index) ||
     Zpoint_inside_frustum_plane(x1,0,z2,plane_index) ||
     Zpoint_inside_frustum_plane(x2,0,z1,plane_index) ||
     Zpoint_inside_frustum_plane(x2,0,z2,plane_index) )   return(1);
return(0);
}
 
 
 
 
// test area vs near,far,left,and right planes...
 
int Zarea_in_frustum(int x1,int z1,int x2,int z2)
{
if (! Zarea_inside_frustum_plane(x1,z1,x2,z2,2)) return(0); // left
if (! Zarea_inside_frustum_plane(x1,z1,x2,z2,3)) return(0); // right
if (! Zarea_inside_frustum_plane(x1,z1,x2,z2,0)) return(0); // near
if (! Zarea_inside_frustum_plane(x1,z1,x2,z2,1)) return(0); // far
return(1);
}

Norm Barrows

Rockland Software Productions

"Building PC games since 1988"

 

rocklandsoftware.net

 


#6 Norman Barrows   Crossbones+   -  Reputation: 1845

Like
1Likes
Like

Posted 13 May 2013 - 11:34 AM

I am using it to cull lights for tiled shading

 

this is the killer here. any unculled light will be a performance hit. if you were merely culling a mesh, it wouldn't matter, as the poly engine would eventually clip it with relatively little overhead, so it would be ok to let it "slip through" your frustum cull. i assume you have some sort of list of lights you go through and cull to see which you should activate when setting lights for the scene. have you considered a change of data structure? i've had a lot of luck recently using that approach to speed up drawing stuff. for example, instead of a list of lights, a 2d array map of lights and you simply activate all within some bounding box distance of the camera, no culling required. i've had a lot of luck copying data from my existing data structures into new ones that are in a format which can be quickly processed for drawing.


Norm Barrows

Rockland Software Productions

"Building PC games since 1988"

 

rocklandsoftware.net

 


#7 _Slin_   Members   -  Reputation: 202

Like
0Likes
Like

Posted 13 May 2013 - 12:43 PM

For now it is just a list, later they will probably be sorted into an octree or something similar, which should of course be an improvement but for now it is good enough to optimize the culling itself a little (for less than 1000 lights, it performs very well, using multithreading and some basic code optimizations).

 

My code seems to work well and looks like this now:

#define Distance(plane, op, r) { \
	float dot = (position.x * plane.normal.x + position.y * plane.normal.y + position.z * plane.normal.z);\
	distance = dot - plane.d; \
	if(distance op r) \
		continue; \
	}


const Vector3& position = light->_worldPosition;
const float range = light->_range;
float distance, dr, dl, dt, db;
Distance(plright, >, range);
dr = distance;
Distance(plleft, <, -range);
dl = distance;
Distance(plbottom, <, -range);
db = distance;
Distance(pltop, >, range);
dt = distance;

float sqrange = range*range;
if(dr > 0.0f && db < 0.0f && dr*dr+db*db > sqrange)
	continue;
if(dr > 0.0f && dt > 0.0f && dr*dr+dt*dt > sqrange)
	continue;
if(dl < 0.0f && db < 0.0f && dl*dl+db*db > sqrange)
	continue;
if(dl < 0.0f && dt > 0.0f && dl*dl+dt*dt > sqrange)
	continue;

 

It´s not pretty, but I have to fix some other things before cleaning up and while my planes have a function to get it´s distance to a point, at 32*24*1024*4 calls, the function call overhead was the biggest performance hit ;)


Edited by _Slin_, 13 May 2013 - 12:44 PM.





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS