Sign in to follow this  
sirob

(solved) Frustum Culling problem

Recommended Posts

Hi again, I've been having this problem with frustum culling for over a week now. I've looked at the code over and over and over and over and over again, and theres absolutly nothing wrong with it as far as I can see. The problem is that it returns incorrect true/falses. It just culls the wrong things. heres some code:
bool cFrustum::Create(D3DXMATRIX mat)
{
	m_Planes[0].a = mat._13; // Near
	m_Planes[0].b = mat._23;
	m_Planes[0].c = mat._33;
	m_Planes[0].d = mat._43;
	D3DXPlaneNormalize(&m_Planes[0], &m_Planes[0]);

	m_Planes[1].a = mat._14 - mat._13; // Far
	m_Planes[1].b = mat._24 - mat._23;
	m_Planes[1].c = mat._34 - mat._33;
	m_Planes[1].d = mat._44 - mat._43;
	D3DXPlaneNormalize(&m_Planes[1], &m_Planes[1]);

	m_Planes[2].a = mat._14 + mat._11; // Left
	m_Planes[2].b = mat._24 + mat._21;
	m_Planes[2].c = mat._34 + mat._31;
	m_Planes[2].d = mat._44 + mat._41;
	D3DXPlaneNormalize(&m_Planes[2], &m_Planes[2]);

	m_Planes[3].a = mat._14 - mat._11; // Right
	m_Planes[3].b = mat._24 - mat._21;
	m_Planes[3].c = mat._34 - mat._31;
	m_Planes[3].d = mat._44 - mat._41;
	D3DXPlaneNormalize(&m_Planes[3], &m_Planes[3]);

	m_Planes[4].a = mat._14 - mat._12; // Top
	m_Planes[4].b = mat._24 - mat._22;
	m_Planes[4].c = mat._34 - mat._32;
	m_Planes[4].d = mat._44 - mat._42;
	D3DXPlaneNormalize(&m_Planes[4], &m_Planes[4]);

	m_Planes[5].a = mat._14 + mat._12; // Bottom
	m_Planes[5].b = mat._24 + mat._22;
	m_Planes[5].c = mat._34 + mat._32;
	m_Planes[5].d = mat._44 + mat._42;
	D3DXPlaneNormalize(&m_Planes[5], &m_Planes[5]);

	return true;
}



this is the frustum plane calculation, mat is the view * proj matrices. heres that part:
	D3DXMATRIX matTmp;
	D3DXMatrixMultiply(&matTmp, &m_Mat, &m_MatProj);
	m_Frustum.Create(matTmp);



and heres the function for checking a sphere:
bool cFrustum::CheckSphere(float x, float y, float z, float Radius)
{
	for (short i=0;i<6;i++)
		if(D3DXPlaneDotCoord(&m_Planes[i], &D3DXVECTOR3(x, y, z)) < -Radius)
				return false;
	return true;
}



and finally, what calls the checkSphere:
bool cTerrain::Render(LPDIRECT3DDEVICE9 d3dDevice, cCamera *Camera)
{
	if (Camera->GetFrustum()->CheckSphere(m_GX, m_GY, m_GZ, 2.5f))
	{



Thanks for any help - going nuts here, quite close to ripping out hair... [EDIT] Forgot to say, that the culling does seem to change somehow when I change the View Matrice, but it doesn't seem to have anything to do with what should be displayed, and what shouldn't. [Edited by - sirob on October 1, 2004 10:55:10 AM]

Share this post


Link to post
Share on other sites
Your near plane calculation is off.


m_Planes[0].a = mat._14 + mat._13;
m_Planes[0].b = mat._24 + mat._23;
m_Planes[0].c = mat._34 + mat._33;
m_Planes[0].d = mat._44 + mat._43;

Share this post


Link to post
Share on other sites
Hi,

First off, thanks for answering.

Unfortunatly, I was aware of that line. I wasn't too sure what the value was supposed to be, as I've seen it in both forms in different places.

Luckily, I tried both, and both didn't work right, so I'm sorry, but thats not it.

If you have any other ideas I'd love to hear them :).

Thanks.

Share this post


Link to post
Share on other sites
Cant really see anything wrong with the code presented, but just incase:

Are you sure that
- m_Mat is the current view matrix
- m_GX, m_GY, and m_GZ are the correct sphere center coords

If you want, you can download the DX utility engine in my sig and compare your frustum class with mine and see if you find something.

Share this post


Link to post
Share on other sites
Thanks for having a look.

I too can't find anything wrong with the class.

As for the m_Mat question, yes I'm positive - the only time I call SetTransform with the View matrix is right before I call frustum::Create with the exact same matrix multiplied by the proj matrix. So it's got to be right.

As for the GX GY and GZ, I just don't think it quite matters. I'm getting results that are WAY off, and I'm positive my GX GY and GZ are one of the vertices actually used in the quad I'm culling. So, even if they are off (which I can't see any reason they should be), it would only skew my results, and make them a bit inaccurate, by my results are WAY off.

Anywho, I'm downloading your classes now, and I'll have a look.

Share this post


Link to post
Share on other sites
Well, Don't I feel stupid now :)

I solved it, finally.

It was actually your question about m_Mat being the view matrix that got me thinking, and I figured out I wasn't using the world matrix in the calculation. I added it in before the view matrix and all is well now.

Thanks a bunch for the help, :-).

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