Jump to content
  • Advertisement
Sign in to follow this  
Zaph-0

[solved] Some help with frustum culling in directX

This topic is 3617 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 guys, I am having huge difficulties implementing working frustum culling. I have following code every frame:

CalcFrustum()
{
	//matview = view matrix
	//	vEyePt		- position where viewer stands (camera)
	//	vLookatPt 	- position to look at
	//	vUpVec 	- vector (0,-1,0) for orientation
D3DXMatrixLookAtLH( &matView, &vEyePt, &vLookatPt, &vUpVec );

	//matProj = projection matrix
	//	fovy 		- D3DX_PI/4
//	aspectxy 	- screenx / screeny
//	zmin 		- 1.0f
//	zmax 		- 100.0f
D3DXMatrixPerspectiveFovLH(&matProj, fovy, aspectxy, zmin, zmax);


	//		
D3DXMATRIX ViewProjection;
	D3DXMatrixMultiply( &viewProjection, &matView, &matProj );

	// Left plane
	frustumplane[0].a = viewProjection._14 + viewProjection._11;
	frustumplane[0].b = viewProjection._24 + viewProjection._21;
	frustumplane[0].c = viewProjection._34 + viewProjection._31;
	frustumplane[0].d = viewProjection._44 + viewProjection._41;

	// Right plane
	frustumplane[1].a = viewProjection._14 - viewProjection._11;
	frustumplane[1].b = viewProjection._24 - viewProjection._21;
	frustumplane[1].c = viewProjection._34 - viewProjection._31;
	frustumplane[1].d = viewProjection._44 - viewProjection._41;

	// Top plane
	frustumplane[2].a = viewProjection._14 - viewProjection._12;
	frustumplane[2].b = viewProjection._24 - viewProjection._22;
	frustumplane[2].c = viewProjection._34 - viewProjection._32;
	frustumplane[2].d = viewProjection._44 - viewProjection._42;

	// Bottom plane
	frustumplane[3].a = viewProjection._14 + viewProjection._12;
	frustumplane[3].b = viewProjection._24 + viewProjection._22;
	frustumplane[3].c = viewProjection._34 + viewProjection._32;
	frustumplane[3].d = viewProjection._44 + viewProjection._42;

	// Near plane
	frustumplane[4].a = viewProjection._13;
	frustumplane[4].b = viewProjection._23;
	frustumplane[4].c = viewProjection._33;
	frustumplane[4].d = viewProjection._43;

	// Far plane
	frustumplane[5].a = viewProjection._14 - viewProjection._13;
	frustumplane[5].b = viewProjection._24 - viewProjection._23;
	frustumplane[5].c = viewProjection._34 - viewProjection._33;
	frustumplane[5].d = viewProjection._44 - viewProjection._43; 

	// Normalize planes
	for(int i=0;i<6;i++)
	{	D3DXPlaneNormalize(&frustumplane, &frustumplane);
	}
}


and then I have following code for checking if vertices are within the frustum:

bool CheckFrustum(float x, float y, float z)
{	D3DXVECTOR3	vector;
	int			i;
	//
	vector.x = x;
	vector.y = y;
	vector.z = z;
	//
	for(i=0;i<6;i++)
	{	if(D3DXPlaneDotCoord(&frustumplane, &vector) < 0 )
		{	return FALSE;
		}
	} 
	return true;
}


But the problem is, it doesn't really work. The vertices that are being culled are wrong: sometimes too early, sometimes too late. Could anybody please help me with this ? [Edited by - Zaph-0 on July 23, 2008 7:55:02 AM]

Share this post


Link to post
Share on other sites
Advertisement

It seems to be working now.

I forgot that I have to calculate any modifiers for scaling/translation/rotation *before* I do the culling check.

Damn, that was annoying to find out.

Anyway, I hope someone else might benefit from my code snippet...and the mistake...

^_^

PS: Should there still be a mistake somewhere (doesn't appear that way) please let me know...

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!