Jump to content
  • Advertisement
Sign in to follow this  
Alex_hbg

Viewing Frustum Culling

This topic is 4107 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

public void UpdateViewingFrustum()
        {
            // Update viewing frustum

            Matrix viewProj = viewMatrix * projMatrix;

            // Left plane
            viewingFrustum[0].A = viewProj.M14 + viewProj.M11;
            viewingFrustum[0].B = viewProj.M24 + viewProj.M21;
            viewingFrustum[0].C = viewProj.M34 + viewProj.M31;
            viewingFrustum[0].D = viewProj.M44 + viewProj.M41;

            // Right plane
            viewingFrustum[1].A = viewProj.M14 - viewProj.M11;
            viewingFrustum[1].B = viewProj.M24 - viewProj.M21;
            viewingFrustum[1].C = viewProj.M34 - viewProj.M31;
            viewingFrustum[1].D = viewProj.M44 - viewProj.M41;

            // Top plane
            viewingFrustum[2].A = viewProj.M14 - viewProj.M12;
            viewingFrustum[2].B = viewProj.M24 - viewProj.M22;
            viewingFrustum[2].C = viewProj.M34 - viewProj.M32;
            viewingFrustum[2].D = viewProj.M44 - viewProj.M42;

            // Bottom plane
            viewingFrustum[3].A = viewProj.M14 + viewProj.M12;
            viewingFrustum[3].B = viewProj.M24 + viewProj.M22;
            viewingFrustum[3].C = viewProj.M34 + viewProj.M32;
            viewingFrustum[3].D = viewProj.M44 + viewProj.M42;

            // Near plane
            viewingFrustum[4].A = viewProj.M13;
            viewingFrustum[4].B = viewProj.M23;
            viewingFrustum[4].C = viewProj.M33;
            viewingFrustum[4].D = viewProj.M43;

            // Far plane
            viewingFrustum[5].A = viewProj.M14 - viewProj.M13;
            viewingFrustum[5].B = viewProj.M24 - viewProj.M23;
            viewingFrustum[5].C = viewProj.M34 - viewProj.M33;
            viewingFrustum[5].D = viewProj.M44 - viewProj.M43;

            // Normalize planes
            for (int i = 0; i < 6; i++)
            {
                viewingFrustum.Normalize();
            }

            // Update visible geometry

            Vector4 vec4 = new Vector4(0, 0, 0, 1);

            for (int x = 0; x < MeshClusters.Length; x++)
            {
                MeshClusters[x].InFrustum = false;

                for (int i = 0; i < 6; i++)
                {
                    float temp = viewingFrustum.Dot(vec4) + MeshClusters[x].boundingRadius;
                    if (temp >= 0)
                    {
                        MeshClusters[x].InFrustum = true;
                        break;
                    }
                }
            }
        }

public void GetBoundingRadius(ExMesh exMesh)
        {
            //calculate bounding spheres
            GraphicsStream gs = exMesh.LockVertexBuffer(LockFlags.None);

            Vector3 uselessVector;
            exMesh.boundingRadius = Geometry.ComputeBoundingSphere(gs, exMesh.VerticeCount, CVertexFormat.Format, out uselessVector);

            exMesh.UnlockVertexBuffer();
        }
Well theres my viewing frustum culling method, but it doesn't seem to be working. I've got a counter going in the render call that'll record how many meshes are drawn and then output that to the screen, so far every mesh has been rendered every single time :-/ If I switch the last part to this:
// Update visible geometry

            Vector4 vec4 = new Vector4(0, 0, 0, 1);

            for (int x = 0; x < MeshClusters.Length; x++)
            {
                MeshClusters[x].InFrustum = true;

                for (int i = 0; i < 6; i++)
                {
                    float temp = viewingFrustum.Dot(vec4) + MeshClusters[x].boundingRadius;
                    if (temp < 0)
                    {
                        MeshClusters[x].InFrustum = false;
                        break;
                    }
                }
            }
        }
Then it does start to work, but the problem I'm having then is that meshes that are right in front of me sometimes disappear too >_> I am using a right handed viewing system, as well as clockwise culling, my upVector is also 0, 0, 1. Great if someone could help me out on this :)

Share this post


Link to post
Share on other sites
Advertisement
You're testing the spheres incorrectly. The following code is from here, see how yours differs (note that they use OpenGL so their matrices are different, but the testing is the same (though that can be different too, depending on what plane equation you're using...)).


int FrustumG::sphereInFrustum(Vec3 &p, float radius) {

float distance;
int result = INSIDE;

for(int i=0; i < 6; i++) {
distance = pl.distance(p);
if (distance < -radius)
return OUTSIDE;
else if (distance < radius)
result = INTERSECT;
}
return(result);
}

Share this post


Link to post
Share on other sites
I tried and the same thing happened as I described at the end of the first post; some meshes that are in my full view are being culled :-/

Anyone know why?

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!