Sign in to follow this  

Frustum culling problems

Recommended Posts

HendrixSweden    122
I've looked around for some nice frustum culling code in c++/d3d but the one I found don't work very well.. I've read threads here and in other places but couldn't find good code to extract the view frustum planes. I took code from and the code uses a combined matrix of the view and projection matrix. I'm a bit confused by the distance to plane variables which seemes to be 0.0 most of the time :) what happens is that almost everything get culled, but in certain angles of my camera some objects will be visible :) Any ideas on how to fix this? My projection matrix is set up like this D3DXMatrixPerspectiveFovLH( &matProj, D3DX_PI/4, fAspect, 0.5f, 1000.0f ); m_pd3dDevice->SetTransform( D3DTS_PROJECTION, &matProj ); and the view matrix updates with the inverse to world matrix of the camera. I'm using world space AABB to cull objects. Help plz :)

Share this post

Link to post
Share on other sites
jyk    2094
The plane distances should not all be zero, and if the camera is moving around rarely should any of them be zero. You might post your extraction code - it sounds like there's an error in there somewhere.

Share this post

Link to post
Share on other sites
HendrixSweden    122
Well.. whenever my camera moves I update the planes with the new view-matrix
and for some reason the distances becomes 0.0, at least on the left, right and
the bottom clip-planes.

void FrustumCuller::UpdatePlanes(const D3DXMATRIX &matView)
// Get combined matrix
D3DXMATRIXA16 matComb;
D3DXMatrixMultiply(&matComb, &matView, &m_matProj);

// Left clipping plane
m_frustumPlanes[0].m_normal.x = matComb._14 + matComb._11;
m_frustumPlanes[0].m_normal.y = matComb._24 + matComb._21;
m_frustumPlanes[0].m_normal.z = matComb._34 + matComb._31;
m_frustumPlanes[0].m_distance = matComb._44 + matComb._41;

// Right clipping plane
m_frustumPlanes[1].m_normal.x = matComb._14 - matComb._11;
m_frustumPlanes[1].m_normal.y = matComb._24 - matComb._21;
m_frustumPlanes[1].m_normal.z = matComb._34 - matComb._31;
m_frustumPlanes[1].m_distance = matComb._44 - matComb._41;

// Top clipping plane
m_frustumPlanes[2].m_normal.x = matComb._14 - matComb._12;
m_frustumPlanes[2].m_normal.y = matComb._24 - matComb._22;
m_frustumPlanes[2].m_normal.z = matComb._34 - matComb._32;
m_frustumPlanes[2].m_distance = matComb._44 - matComb._42;

// Bottom clipping plane
m_frustumPlanes[3].m_normal.x = matComb._14 + matComb._12;
m_frustumPlanes[3].m_normal.y = matComb._24 + matComb._22;
m_frustumPlanes[3].m_normal.z = matComb._34 + matComb._32;
m_frustumPlanes[3].m_distance = matComb._44 + matComb._42;

// Near clipping plane
m_frustumPlanes[4].m_normal.x = matComb._13;
m_frustumPlanes[4].m_normal.y = matComb._23;
m_frustumPlanes[4].m_normal.z = matComb._33;
m_frustumPlanes[4].m_distance = matComb._43;

// Far clipping plane
m_frustumPlanes[5].m_normal.x = matComb._14 - matComb._13;
m_frustumPlanes[5].m_normal.y = matComb._24 - matComb._23;
m_frustumPlanes[5].m_normal.z = matComb._34 - matComb._33;
m_frustumPlanes[5].m_distance = matComb._44 - matComb._43;

Share this post

Link to post
Share on other sites
jyk    2094
The extraction code looks correct. Distances of zero for the side, top and bottom planes suggests an identity view matrix. Are you sure the view and projection matrices are both correct?

Share this post

Link to post
Share on other sites
HendrixSweden    122
There was something very strange about the distance values.. but after adding and removing a variable to the FrustumPlane class and rebuilding the solution the problem was gone?! =)

While debugging, setting the distance to something like 8.3 - 7.9 would result in 0.0 ...

And found that the normals pointed in the wrong direction.. also, the distance had the wrong sign :)

that explains why it culled about every object! :)

// Left clipping plane
m_frustumPlanes[0].m_normal.x = -(matComb._14 + matComb._11);
m_frustumPlanes[0].m_normal.y = -(matComb._24 + matComb._21);
m_frustumPlanes[0].m_normal.z = -(matComb._34 + matComb._31);
m_frustumPlanes[0].m_distance = -(matComb._44 + matComb._41);

note the -( ... )


So it seemes to work!! .. great!..

Now I have to do some serious vertex shading transformations to speed up to drawing of up to 7000 objects per frame :9

Thx for your replies

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