Sign in to follow this  

(solved) Frustum Culling problem

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

Hi, I posted this on the DX forum, but didn't get any replies, so I thought I might try here: 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:54:44 AM]

Share this post


Link to post
Share on other sites
if the 'Create' function takes in the clipping matrix then from just looking at that code your near plane calculation is wrong.

I haven't checked the matrix math over but just from your other code that you displayed the near matrix should be calculated as follows.


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;


I can check over the math later, if that doesn't do it

Hope that helped

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
checked over the math for 2 of the planes and it is correct.

for the spehere check i have mine set to less than or equal to the negative radius, might wanna try that but i doubt thats the problem.

Can't think of anything else atm, are u sure your radius for the terrain is 2.5?

Maybe you can provide a screenshot to show the error cause that code seems to be correct.

Share this post


Link to post
Share on other sites
Thanks nts,

but I just solved this problem, thanks to help I got on the DX forum.

Turns out I wasn't multiplying by the world matrix, when actually I should have been.

Anyhow, thanks for the help.

Share this post


Link to post
Share on other sites
Quote:
Original post by sirob
Thanks nts,

but I just solved this problem, thanks to help I got on the DX forum.

Turns out I wasn't multiplying by the world matrix, when actually I should have been.

Anyhow, thanks for the help.



Hey no problem, glad you fixed the problem (haven't used DX myself for a while now, mostly OGL these days:D)

Share this post


Link to post
Share on other sites

This topic is 4826 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.

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