[solved] Some help with frustum culling in directX

Started by
0 comments, last by Zaph-0 15 years, 9 months ago
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]
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...

This topic is closed to new replies.

Advertisement