Sign in to follow this  
dxdotnet1

ANother frustum culling question

Recommended Posts

Whats wrong with my frustum cullinh code? Ive used the code bellow for my frustum culling test and it doesnt work
Matrix proj = new Matrix( );
Matrix view = new Matrix( );
		
proj = device.GetTransform( TransformType.Projection );
			view = device.GetTransform( TransformType.View );

Matrix comboMatrix = Matrix.Multiply( proj, view );
            
#region Plane extraction from combo matrix

//extract left plane
frustum[ 0 ].A = comboMatrix.M14 + comboMatrix.M11;
frustum[ 0 ].B = comboMatrix.M24 + comboMatrix.M21;
frustum[ 0 ].C = comboMatrix.M34 + comboMatrix.M31;
frustum[ 0 ].D = comboMatrix.M44 + comboMatrix.M41;
frustum[ 0 ].Normalize( );

//extract right plane
frustum[ 1 ].A = comboMatrix.M14 - comboMatrix.M11;
frustum[ 1 ].B = comboMatrix.M24 - comboMatrix.M21;
frustum[ 1 ].C = comboMatrix.M34 - comboMatrix.M31;
frustum[ 1 ].D = comboMatrix.M44 - comboMatrix.M41;
frustum[ 1 ].Normalize( );
			
//top clipping plane
frustum[ 2 ].A = comboMatrix.M14 - comboMatrix.M12;
frustum[ 2 ].B = comboMatrix.M24 - comboMatrix.M22;
frustum[ 2 ].C = comboMatrix.M34 - comboMatrix.M32;
frustum[ 2 ].D = comboMatrix.M44 - comboMatrix.M42;
frustum[ 2 ].Normalize( );
	
//bottom clipping plane
frustum[ 3 ].A = comboMatrix.M14 + comboMatrix.M12;
frustum[ 3 ].B = comboMatrix.M24 + comboMatrix.M22;
frustum[ 3 ].C = comboMatrix.M34 + comboMatrix.M32;
frustum[ 3 ].D = comboMatrix.M44 + comboMatrix.M42;
frustum[ 3 ].Normalize( );

//near clipping plane
frustum[ 4 ].A = comboMatrix.M14 + comboMatrix.M13;
frustum[ 4 ].B = comboMatrix.M24 + comboMatrix.M23;
frustum[ 4 ].C = comboMatrix.M34 + comboMatrix.M33;
frustum[ 4 ].D = comboMatrix.M44 + comboMatrix.M43;
frustum[ 4 ].Normalize( );

//Far clipping plane
frustum[ 5 ].A = comboMatrix.M14 - comboMatrix.M13;
frustum[ 5 ].B = comboMatrix.M24 - comboMatrix.M23;
frustum[ 5 ].C = comboMatrix.M34 - comboMatrix.M33;
frustum[ 5 ].D = comboMatrix.M44 - comboMatrix.M43;
frustum[ 5 ].Normalize( );

and i test my a certain Point using this code:
public static bool CheckPoint( float x,float y,float z )
{
	for(int i= 0; i < 6 ; i++ ) 
	{ 
          float d = frustum[i].A* x + frustum[i].B*y +  
                    frustum[i].C* z + frustum[i].D;
	  if( d <= 0 )						  	    return false;
	}
        return true;	
}

whats wrong with my code? [Edited by - Coder on November 9, 2004 2:44:27 AM]

Share this post


Link to post
Share on other sites
My code for extract planes


g_pd3dDevice->GetTransform(D3DTS_PROJECTION, &proj);
g_pd3dDevice->GetTransform(D3DTS_WORLD, &world);
g_pd3dDevice->GetTransform(D3DTS_VIEW, &view);

// Combine the three matrices
clip = world * view * proj;

// Extract the numbers for the RIGHT plane
frustum[0][0] = clip[ 3] - clip[ 0];
frustum[0][1] = clip[ 7] - clip[ 4];
frustum[0][2] = clip[11] - clip[ 8];
frustum[0][3] = clip[15] - clip[12];

//Normalize the result
t = sqrt( frustum[0][0] * frustum[0][0] + frustum[0][1] * frustum[0][1] + frustum[0][2] * frustum[0][2] );
frustum[0][0] /= t;
frustum[0][1] /= t;
frustum[0][2] /= t;
frustum[0][3] /= t;

// Extract the numbers for the LEFT plane
frustum[1][0] = clip[ 3] + clip[ 0];
frustum[1][1] = clip[ 7] + clip[ 4];
frustum[1][2] = clip[11] + clip[ 8];
frustum[1][3] = clip[15] + clip[12];

// Normalize the result
t = sqrt( frustum[1][0] * frustum[1][0] + frustum[1][1] * frustum[1][1] + frustum[1][2] * frustum[1][2] );
frustum[1][0] /= t;
frustum[1][1] /= t;
frustum[1][2] /= t;
frustum[1][3] /= t;

// Extract the BOTTOM plane
frustum[2][0] = clip[ 3] + clip[ 1];
frustum[2][1] = clip[ 7] + clip[ 5];
frustum[2][2] = clip[11] + clip[ 9];
frustum[2][3] = clip[15] + clip[13];

// Normalize the result
t = sqrt( frustum[2][0] * frustum[2][0] + frustum[2][1] * frustum[2][1] + frustum[2][2] * frustum[2][2] );
frustum[2][0] /= t;
frustum[2][1] /= t;
frustum[2][2] /= t;
frustum[2][3] /= t;

//Extract the TOP plane
frustum[3][0] = clip[ 3] - clip[ 1];
frustum[3][1] = clip[ 7] - clip[ 5];
frustum[3][2] = clip[11] - clip[ 9];
frustum[3][3] = clip[15] - clip[13];

// Normalize the result
t = sqrt( frustum[3][0] * frustum[3][0] + frustum[3][1] * frustum[3][1] + frustum[3][2] * frustum[3][2] );
frustum[3][0] /= t;
frustum[3][1] /= t;
frustum[3][2] /= t;
frustum[3][3] /= t;

// Extract the FAR plane
frustum[4][0] = clip[ 3] - clip[ 2];
frustum[4][1] = clip[ 7] - clip[ 6];
frustum[4][2] = clip[11] - clip[10];
frustum[4][3] = clip[15] - clip[14];

// Normalize the result
t = sqrt( frustum[4][0] * frustum[4][0] + frustum[4][1] * frustum[4][1] + frustum[4][2] * frustum[4][2] );
frustum[4][0] /= t;
frustum[4][1] /= t;
frustum[4][2] /= t;
frustum[4][3] /= t;

// Extract the NEAR plane
frustum[5][0] = clip[ 3] + clip[ 2];
frustum[5][1] = clip[ 7] + clip[ 6];
frustum[5][2] = clip[11] + clip[10];
frustum[5][3] = clip[15] + clip[14];

// Normalize the result
t = sqrt( frustum[5][0] * frustum[5][0] + frustum[5][1] * frustum[5][1] + frustum[5][2] * frustum[5][2] );
frustum[5][0] /= t;
frustum[5][1] /= t;
frustum[5][2] /= t;
frustum[5][3] /= t;



And my code to test a point:

bool Clipper::pointInFrustum(D3DVECTOR point)
{
for (int i=0;i<6;i++)
{
if (frustum[i][0]*point.x+frustum[i][1]*point.y+frustum[i][2]*point.z+frustum[i][3]<= 0 )
return false;
}
return true;
}



Hope it will be useful to you.

VinCenT.

Share this post


Link to post
Share on other sites
thanks. a couple of things i get confused. do i need to include the world matrix when getting the clip or its just the vew and projection?

lastly? DO i need to test the point againts 6 planes or stop when i alreadu encounter a <=0 value against a plane?
Example when i test the point against the first plane and got a negative result, do i still need to test the point against the rest of the plane or not? pls explain :)

Share this post


Link to post
Share on other sites
If a point is behind any one plane, you can stop at that point. Only if the point is inside all 6 planes is it potentially visible.

Does your normalize function also rescale the D component?

Here is mine for what it's worth...


// Extract the numbers for the Far plane
mFrustum.mPlanes[ Shapes::Far ].mPlane.a = mComposite._14 - mComposite._13;
mFrustum.mPlanes[ Shapes::Far ].mPlane.b = mComposite._24 - mComposite._23;
mFrustum.mPlanes[ Shapes::Far ].mPlane.c = mComposite._34 - mComposite._33;
mFrustum.mPlanes[ Shapes::Far ].mPlane.d = mComposite._44 - mComposite._43;
D3DXPlaneNormalize( &mFrustum.mPlanes[ Shapes::Far ].mPlane, &mFrustum.mPlanes[ Shapes::Far ].mPlane );

mFrustum.mPlanes[ Shapes::Far ].mNormal = D3DXVECTOR3( mFrustum.mPlanes[ Shapes::Far ].mPlane.a,
mFrustum.mPlanes[ Shapes::Far ].mPlane.b,
mFrustum.mPlanes[ Shapes::Far ].mPlane.c );
mFrustum.mPlanes[ Shapes::Far ].mDistance = mFrustum.mPlanes[ Shapes::Far ].mPlane.d;

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