]> git.tdb.fi Git - libs/gl.git/commitdiff
Properly scale the the bounding sphere for frustum culling
authorMikko Rasa <tdb@tdb.fi>
Mon, 28 Nov 2016 00:20:20 +0000 (02:20 +0200)
committerMikko Rasa <tdb@tdb.fi>
Mon, 28 Nov 2016 00:20:20 +0000 (02:20 +0200)
The original radius must be squared too.  Also, negative distances mean
the sphere is inside the frustum and should not be culled.

source/scene.cpp

index 6c3d2910706db98d6f2ecd7ed67b3dd771c372e8..b8a74b59be7f3c6be74c85a179994c07345755ae 100644 (file)
@@ -66,13 +66,13 @@ bool Scene::frustum_cull(const Renderable &renderable) const
                return false;
 
        Vector4 center = culling_matrix*(*matrix*compose(bsphere->get_center(), 1.0f));
-       Vector4 x_axis = *matrix*Vector4(1.0f, 0.0f, 0.0f, 0.0f);
-       float radius_sq = bsphere->get_radius()*inner_product(x_axis, x_axis);
+       Vector4 x_axis = *matrix*Vector4(bsphere->get_radius(), 0.0f, 0.0f, 0.0f);
+       float radius_sq = inner_product(x_axis, x_axis);
 
        for(unsigned i=0; i<6; ++i)
        {
                float distance = inner_product(center, frustum_edges[i]);
-               if(distance*distance>radius_sq)
+               if(distance>0 && distance*distance>radius_sq)
                        return true;
        }