Sign in to follow this  
Locutusborg

frustum culling behaving strange

Recommended Posts

Locutusborg    124
Hello everyone, I have the following problem: I use frustum culling together with object selection. when an object is selected, a square is placed around it. When I turn so the object is offscreen, the square should not be placed on screen. this works, but only partly. when the object is straight behind me, the culling thinks that the object is visible, and draws an empty square on screen. I use the following code for the culling:

D3DXMATRIX vp = matView * matProj;
D3DXPLANE frustum[6];

	//left plane
	frustum[0].a = vp._14 + vp._11;
	frustum[0].b = vp._24 + vp._21;
	frustum[0].c = vp._34 + vp._31;
	frustum[0].d = vp._44 + vp._41;

	//right plane
	frustum[1].a = vp._14 - vp._11;
	frustum[1].b = vp._24 - vp._21;
	frustum[1].c = vp._34 - vp._31;
	frustum[1].d = vp._44 - vp._41;

	//top plane
	frustum[2].a = vp._14 - vp._12;
	frustum[2].b = vp._24 - vp._22;
	frustum[2].c = vp._34 - vp._32;
	frustum[2].d = vp._44 - vp._42;

	//top plane
	frustum[3].a = vp._14 + vp._12;
	frustum[3].b = vp._24 + vp._22;
	frustum[3].c = vp._34 + vp._32;
	frustum[3].d = vp._44 + vp._42;

	//near plane
	frustum[4].a = vp._13;
	frustum[4].b = vp._23;
	frustum[4].c = vp._33;
	frustum[4].d = vp._43;

	//far plane
	frustum[5].a = vp._14 - vp._13;
	frustum[5].b = vp._24 - vp._23;
	frustum[5].c = vp._34 - vp._33;
	frustum[5].d = vp._44 - vp._43;

	for(int i = 0; i <6; i++)
	{
		D3DXPlaneNormalize(&frustum[i],&frustum[i]);
	}

	float fDistance; // Calculated distance

// Test against a plane
for(int i=0; i<6; ++i)
{
	fDistance=D3DXPlaneDotCoord(&frustum[i], &pos);
			
   if(fDistance<-radius)
   {
	   return 0; // not visible
   }
}
return 1; // visible

If anyone has an idea as to what is wrong here? THANKS

Share this post


Link to post
Share on other sites
Lord_Evil    680
I don't know D3DXPlaneDotCoord very well, but it seems that since the near and far planes' normals point in the same direction, the distance to the near plane might have the wrong sign if the object is right behind the camera.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this