From: Mikko Rasa Date: Fri, 4 Dec 2015 12:54:55 +0000 (+0200) Subject: Further tweak frustum culling to be more efficient X-Git-Url: http://git.tdb.fi/?a=commitdiff_plain;h=3919a742c65783a9ebce05a88427bdcd8f6a6c92;p=libs%2Fgl.git Further tweak frustum culling to be more efficient --- diff --git a/source/instancescene.cpp b/source/instancescene.cpp index fef3e969..b3a89f33 100644 --- a/source/instancescene.cpp +++ b/source/instancescene.cpp @@ -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 diff --git a/source/orderedscene.cpp b/source/orderedscene.cpp index f129cdab..965300cc 100644 --- a/source/orderedscene.cpp +++ b/source/orderedscene.cpp @@ -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 diff --git a/source/scene.cpp b/source/scene.cpp index 7896d23d..7d3c1679 100644 --- a/source/scene.cpp +++ b/source/scene.cpp @@ -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 *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; diff --git a/source/scene.h b/source/scene.h index 611f3508..27656a59 100644 --- a/source/scene.h +++ b/source/scene.h @@ -28,7 +28,7 @@ public: protected: std::list 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 diff --git a/source/simplescene.cpp b/source/simplescene.cpp index 7629a828..73d08de2 100644 --- a/source/simplescene.cpp +++ b/source/simplescene.cpp @@ -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 diff --git a/source/zsortedscene.cpp b/source/zsortedscene.cpp index c9bce679..f61de577 100644 --- a/source/zsortedscene.cpp +++ b/source/zsortedscene.cpp @@ -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())