]> git.tdb.fi Git - libs/gl.git/commitdiff
Further tweak frustum culling to be more efficient
authorMikko Rasa <tdb@tdb.fi>
Fri, 4 Dec 2015 12:54:55 +0000 (14:54 +0200)
committerMikko Rasa <tdb@tdb.fi>
Fri, 4 Dec 2015 12:54:55 +0000 (14:54 +0200)
source/instancescene.cpp
source/orderedscene.cpp
source/scene.cpp
source/scene.h
source/simplescene.cpp
source/zsortedscene.cpp

index fef3e9697e602051248bfe35f827078eac8a4117..b3a89f338a42cfff32500ccace3256e0b1e7727b 100644 (file)
@@ -38,11 +38,19 @@ void InstanceScene::finish_frame() const
 
 void InstanceScene::render(Renderer &renderer, const Tag &tag) const
 {
-       setup_frustum(renderer);
-       for(InstanceMap::const_iterator i=renderables.begin(); i!=renderables.end(); ++i)
-               for(RenderableSet::const_iterator j=i->second.begin(); j!=i->second.end(); ++j)
-                       if(!frustum_cull(renderer, **j))
+       if(setup_frustum(renderer))
+       {
+               for(InstanceMap::const_iterator i=renderables.begin(); i!=renderables.end(); ++i)
+                       for(RenderableSet::const_iterator j=i->second.begin(); j!=i->second.end(); ++j)
+                               if(!frustum_cull(**j))
+                                       renderer.render(**j, tag);
+       }
+       else
+       {
+               for(InstanceMap::const_iterator i=renderables.begin(); i!=renderables.end(); ++i)
+                       for(RenderableSet::const_iterator j=i->second.begin(); j!=i->second.end(); ++j)
                                renderer.render(**j, tag);
+       }
 }
 
 } // namespace GL
index f129cdab33e20b68549a933cb61c2473f729f241..965300cc25500d2f069f0d20e5c8da5bbd5b84f4 100644 (file)
@@ -49,10 +49,17 @@ void OrderedScene::finish_frame() const
 
 void OrderedScene::render(Renderer &renderer, const Tag &tag) const
 {
-       setup_frustum(renderer);
-       for(RenderableList::const_iterator i=renderables.begin(); i!=renderables.end(); ++i)
-               if(!frustum_cull(renderer, **i))
+       if(setup_frustum(renderer))
+       {
+               for(RenderableList::const_iterator i=renderables.begin(); i!=renderables.end(); ++i)
+                       if(!frustum_cull(**i))
+                               renderer.render(**i, tag);
+       }
+       else
+       {
+               for(RenderableList::const_iterator i=renderables.begin(); i!=renderables.end(); ++i)
                        renderer.render(**i, tag);
+       }
 }
 
 } // namespace GL
index 7896d23d6246830d0f1716beaf830a0cff4e8665..7d3c167975e7a3c548de29cc5aff782439e46a3a 100644 (file)
@@ -21,42 +21,48 @@ void Scene::render(const Tag &tag) const
        render(renderer, tag);
 }
 
-void Scene::setup_frustum(const Renderer &renderer) const
+bool Scene::setup_frustum(const Renderer &renderer) const
 {
        const Camera *camera = renderer.get_camera();
        if(!camera)
-               return;
+               return false;
+
+       const Matrix &matrix = camera->get_object_matrix();
 
        float y = tan(camera->get_field_of_view()/2.0f);
        float s = sqrt(y*y+1);
-       frustum_edges[0] = Vector3(0, 1/s, y/s);
-       frustum_edges[1] = Vector3(0, -1/s, y/s);
+       frustum_edges[0] = matrix*Vector4(0, 1/s, y/s, 0);
+       frustum_edges[1] = matrix*Vector4(0, -1/s, y/s, 0);
 
        float x = y*camera->get_aspect();
        s = sqrt(x*x+1);
-       frustum_edges[2] = Vector3(1/s, 0, x/s);
-       frustum_edges[3] = Vector3(-1/s, 0, x/s);
+       frustum_edges[2] = matrix*Vector4(1/s, 0, x/s, 0);
+       frustum_edges[3] = matrix*Vector4(-1/s, 0, x/s, 0);
+
+       const Vector3 &camera_pos = camera->get_position();
+       Vector4 pos4 = compose(camera_pos, 0.0f);
+       for(unsigned i=0; i<4; ++i)
+               frustum_edges[i].w = -inner_product(frustum_edges[i], pos4);
+
+       const Vector3 &look_dir = camera->get_look_direction();
+       frustum_edges[4] = compose(look_dir, -dot(camera_pos, look_dir)-camera->get_far_clip());
+       frustum_edges[5] = compose(-look_dir, dot(camera_pos, look_dir)+camera->get_near_clip());
+
+       return true;
 }
 
-bool Scene::frustum_cull(const Renderer &renderer, const Renderable &renderable) const
+bool Scene::frustum_cull(const Renderable &renderable) const
 {
        const Matrix *matrix = renderable.get_matrix();
        const Geometry::BoundingSphere<float, 3> *bsphere = renderable.get_bounding_sphere();
-       const Camera *camera = renderer.get_camera();
-       if(!matrix || !bsphere || !camera)
+       if(!matrix || !bsphere)
                return false;
 
-       Matrix mvm = camera->get_matrix()**matrix;
-       float n = camera->get_near_clip();
-       float f = camera->get_far_clip();
-
-       Vector3 center = mvm*bsphere->get_center();
+       Vector4 center = *matrix*compose(bsphere->get_center(), 1.0f);
        float radius = bsphere->get_radius();
 
-       if(center.z-radius>-n || center.z+radius<-f)
-               return true;
-       for(unsigned i=0; i<4; ++i)
-               if(dot(center, frustum_edges[i])>radius)
+       for(unsigned i=0; i<6; ++i)
+               if(inner_product(center, frustum_edges[i])>radius)
                        return true;
 
        return false;
index 611f3508e2d7e103a592787aa5e08ea75f0f1625..27656a59db2fbbdc5af57de1daf498361f5f07a5 100644 (file)
@@ -28,7 +28,7 @@ public:
 
 protected:
        std::list<Renderable *> owned_data;
-       mutable Vector3 frustum_edges[4];
+       mutable Vector4 frustum_edges[6];
 
        Scene() { }
 private:
@@ -44,8 +44,8 @@ public:
        virtual void render(const Tag & = Tag()) const;
 
 protected:
-       void setup_frustum(const Renderer &) const;
-       bool frustum_cull(const Renderer &, const Renderable &) const;
+       bool setup_frustum(const Renderer &) const;
+       bool frustum_cull(const Renderable &) const;
 };
 
 } // namespace GL
index 7629a8284b4ef213168f60a6d6973a95cab85834..73d08de2322f2194864490bf8b0bb7022678a654 100644 (file)
@@ -28,10 +28,17 @@ void SimpleScene::finish_frame() const
 
 void SimpleScene::render(Renderer &renderer, const Tag &tag) const
 {
-       setup_frustum(renderer);
-       for(RenderableSet::const_iterator i=renderables.begin(); i!=renderables.end(); ++i)
-               if(!frustum_cull(renderer, **i))
+       if(setup_frustum(renderer))
+       {
+               for(RenderableSet::const_iterator i=renderables.begin(); i!=renderables.end(); ++i)
+                       if(!frustum_cull(**i))
+                               renderer.render(**i, tag);
+       }
+       else
+       {
+               for(RenderableSet::const_iterator i=renderables.begin(); i!=renderables.end(); ++i)
                        renderer.render(**i, tag);
+       }
 }
 
 } // namespace GL
index c9bce6798a83959e8568dde9454b80b2ff28b2b0..f61de57720fc8f394f72aa752f678964a2046ace 100644 (file)
@@ -36,9 +36,9 @@ void ZSortedScene::render(Renderer &renderer, const Tag &tag) const
        float radius_factor = 1.0f-reference;
        float sign = order*2.0f-1.0f;
 
-       setup_frustum(renderer);
+       bool use_frustum = setup_frustum(renderer);
        for(RenderableSet::const_iterator i=renderables.begin(); i!=renderables.end(); ++i)
-               if(!frustum_cull(renderer, **i))
+               if(!use_frustum || !frustum_cull(**i))
                {
                        float z = 0;
                        if(const Matrix *model_matrix = (*i)->get_matrix())