Advertisement Jump to content
Sign in to follow this  

[solved] Some help with frustum culling in directX

This topic is 3832 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:

	//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;
	{	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

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, you agree to our community Guidelines, Terms of Use, and Privacy Policy. is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!