Jump to content
  • Advertisement
Sign in to follow this  
andreahmed

Frustum culling

This topic is 786 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 All,

 

I'm trying to do frustum culling by extracting the planes from the view projection matrix but while the camera is not facing the sphere, it returns true, and it says that the sphere is inside the frustum however its not facing the camera. here is a video of what am talking about

 

 

 

here is the full code, can somebody spot a math error here ?

 

  1. std::vector<XMFLOAT4> cCamera::getFrustumPlanes(XMMATRIX& viewProj)
  2. {
  3.     // x, y, z, and w represent A, B, C and D in the plane equation
  4.     // where ABC are the xyz of the planes normal, and D is the plane constant
  5.     std::vector<XMFLOAT4> tempFrustumPlane(6);
  6.     XMFLOAT4X4 fView;
  7.     XMStoreFloat4x4(&fView, viewProj);
  8.    
  9.     // Left Frustum Plane
  10.     // Add first column of the matrix to the fourth column
  11.     tempFrustumPlane[0].x = fView._14 + fView._11;
  12.     tempFrustumPlane[0].y = fView._24 + fView._21;
  13.     tempFrustumPlane[0].z = fView._34 + fView._31;
  14.     tempFrustumPlane[0].w = fView._44 + fView._41;
  15.  
  16.     // Right Frustum Plane
  17.     // Subtract first column of matrix from the fourth column
  18.     tempFrustumPlane[1].x = fView._14 - fView._11;
  19.     tempFrustumPlane[1].y = fView._24 - fView._21;
  20.     tempFrustumPlane[1].z = fView._34 - fView._31;
  21.     tempFrustumPlane[1].w = fView._44 - fView._41;
  22.  
  23.     // Top Frustum Plane
  24.     // Subtract second column of matrix from the fourth column
  25.     tempFrustumPlane[2].x = fView._14 - fView._12;
  26.     tempFrustumPlane[2].y = fView._24 - fView._22;
  27.     tempFrustumPlane[2].z = fView._34 - fView._32;
  28.     tempFrustumPlane[2].w = fView._44 - fView._42;
  29.  
  30.     // Bottom Frustum Plane
  31.     // Add second column of the matrix to the fourth column
  32.     tempFrustumPlane[3].x = fView._14 + fView._12;
  33.     tempFrustumPlane[3].y = fView._24 + fView._22;
  34.     tempFrustumPlane[3].z = fView._34 + fView._32;
  35.     tempFrustumPlane[3].w = fView._44 + fView._42;
  36.  
  37.     // Near Frustum Plane
  38.     // We could add the third column to the fourth column to get the near plane,
  39.     // but we don't have to do this because the third column IS the near plane
  40.     tempFrustumPlane[4].x = fView._13;
  41.     tempFrustumPlane[4].y = fView._23;
  42.     tempFrustumPlane[4].z = fView._33;
  43.     tempFrustumPlane[4].w = fView._43;
  44.  
  45.     // Far Frustum Plane
  46.     // Subtract third column of matrix from the fourth column
  47.     tempFrustumPlane[5].x = fView._14 - fView._13;
  48.     tempFrustumPlane[5].y = fView._24 - fView._23;
  49.     tempFrustumPlane[5].z = fView._34 - fView._33;
  50.     tempFrustumPlane[5].w = fView._44 - fView._43;
  51.  
  52.     // Normalize plane normals (A, B and C (xyz))
  53.     // Also take note that planes face inward
  54.     for (int i = 0; i < 6; ++i)
  55.     {
  56.         float length = sqrt((tempFrustumPlane.x * tempFrustumPlane.x) + (tempFrustumPlane.y * tempFrustumPlane.y) + (tempFrustumPlane.z * tempFrustumPlane.z));
  57.         tempFrustumPlane.x /= length;
  58.         tempFrustumPlane.y /= length;
  59.         tempFrustumPlane.z /= length;
  60.         tempFrustumPlane.w /= length;
  61.     }
  62.  
  63.     return tempFrustumPlane;
  64. }
  65.  
  66. bool cCamera::CheckSphere(FLOAT xCenter, FLOAT yCenter, FLOAT zCenter, FLOAT radius, std::vector<XMFLOAT4> &frustumPlanes)
  67. {
  68.     // Check if the radius of the sphere is inside the view frustum.
  69.     for (INT i = 0; i < 6; i++)
  70.     {
  71.         XMVECTOR dotproduct = XMPlaneDotCoord(XMLoadFloat4(&frustumPlanes), XMLoadFloat4(&XMFLOAT4(xCenter, yCenter, zCenter, 1.0f)));
  72.         XMFLOAT4 fPlaneDotProduct;
  73.         XMStoreFloat4(&fPlaneDotProduct, dotproduct);
  74.         if (fPlaneDotProduct.x < -radius)
  75.         {
  76.             return false;
  77.         }
  78.     }
  79.  
  80.     return true;
  81. }

Share this post


Link to post
Share on other sites
Advertisement

Please use the formatting tools provided to put your code in a readable form.  The little <>'s on the interface do it.

Share this post


Link to post
Share on other sites
// const float SCREEN_DEPTH = 1000.0f;           // ROCKLAND: the far clip plane distance
 
D3DXPLANE m_planes[6];        // ROCKLAND: the frustum planes
D3DXPLANE m_planes2[6];        // ROCKLAND: the frustum planes.  frustum rotated only.
 
 
// ROCKLAND: screenDepth = the far clip plane distance
// ROCKLAND: projectionMatrix = the current projection matrix 
// ROCKLAND: viewMatrix = the current view matrix 
 
// ROCKLAND: Z3D equivalents:
// ROCKLAND: screenDepth = (float)Zfarplane
// ROCKLAND: projectionMatrix = Zprojection_matrix
// ROCKLAND: viewMatrix = ZmView
 
void Zcalc_frustum_planes()
{
//float   //screenDepth,                // ROCKLAND:  far plane
        //zMinimum,                   // ROCKLAND: near plane
        //r;                          // ROCKLAND:    far / far-near
D3DXMATRIX  projectionMatrix,       // ROCKLAND: copy of current projection matrix
            viewMatrix,             // ROCKLAND: copy of current view matrix
            matrix;                 // ROCKLAND: frustum matrix  ( view * modified proj )
 
// ROCKLAND: copy Z3D values into the local vars used by this code.
//screenDepth=(float)Zfarplane;
projectionMatrix = Zprojection_matrix;
viewMatrix = ZmView;
 
/*
// Calculate the minimum Z distance in the frustum.
zMinimum = -projectionMatrix._43 / projectionMatrix._33;
r = screenDepth / (screenDepth - zMinimum);
projectionMatrix._33 = r;
projectionMatrix._43 = -r * zMinimum;
// Create the frustum matrix from the view matrix and updated projection matrix.
*/
 
 
D3DXMatrixMultiply(&matrix, &viewMatrix, &projectionMatrix);
 
 
// Calculate left plane of frustum.
m_planes[2].a = matrix._14 + matrix._11; 
m_planes[2].b = matrix._24 + matrix._21;
m_planes[2].c = matrix._34 + matrix._31;
m_planes[2].d = matrix._44 + matrix._41;
D3DXPlaneNormalize(&m_planes[2], &m_planes[2]);
 
// Calculate right plane of frustum.
m_planes[3].a = matrix._14 - matrix._11; 
m_planes[3].b = matrix._24 - matrix._21;
m_planes[3].c = matrix._34 - matrix._31;
m_planes[3].d = matrix._44 - matrix._41;
D3DXPlaneNormalize(&m_planes[3], &m_planes[3]);
 
 
// Calculate top plane of frustum.
m_planes[4].a = matrix._14 - matrix._12; 
m_planes[4].b = matrix._24 - matrix._22;
m_planes[4].c = matrix._34 - matrix._32;
m_planes[4].d = matrix._44 - matrix._42;
D3DXPlaneNormalize(&m_planes[4], &m_planes[4]);
 
// Calculate bottom plane of frustum.
m_planes[5].a = matrix._14 + matrix._12;
m_planes[5].b = matrix._24 + matrix._22;
m_planes[5].c = matrix._34 + matrix._32;
m_planes[5].d = matrix._44 + matrix._42;
D3DXPlaneNormalize(&m_planes[5], &m_planes[5]);
 
 
// Calculate near plane of frustum.
m_planes[0].a = matrix._13;        // matrix._14 + matrix._13;
m_planes[0].b = matrix._23;        // matrix._24 + matrix._23;
m_planes[0].c = matrix._33;        // matrix._34 + matrix._33;
m_planes[0].d = matrix._43;        // matrix._44 + matrix._43;
D3DXPlaneNormalize(&m_planes[0], &m_planes[0]);
 
// Calculate far plane of frustum.
m_planes[1].a = matrix._14 - matrix._13; 
m_planes[1].b = matrix._24 - matrix._23;
m_planes[1].c = matrix._34 - matrix._33;
m_planes[1].d = matrix._44 - matrix._43;
D3DXPlaneNormalize(&m_planes[1], &m_planes[1]);
}

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.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!