From: Mikko Rasa Date: Mon, 9 Sep 2013 16:06:44 +0000 (+0300) Subject: Add interfaces for culling X-Git-Url: http://git.tdb.fi/?a=commitdiff_plain;h=4edd75061a1e24572bd672fb72e1bc1838008ce7;p=libs%2Fgl.git Add interfaces for culling Culler implementations to come soon. --- diff --git a/source/culler.h b/source/culler.h new file mode 100644 index 00000000..c813f8d1 --- /dev/null +++ b/source/culler.h @@ -0,0 +1,25 @@ +#ifndef MSP_GL_CULLER_H_ +#define MSP_GL_CULLER_H_ + +namespace Msp { +namespace GL { + +class Renderable; +class Renderer; + +class Culler +{ +protected: + Culler() { } +public: + virtual ~Culler() { } + + virtual void setup_frame(const Renderer &) { }; + + virtual bool cull(const Renderer &, const Renderable &) const = 0; +}; + +} // namespace GL +} // namespace Msp + +#endif diff --git a/source/instancescene.cpp b/source/instancescene.cpp index 7b0979c8..0ff9b520 100644 --- a/source/instancescene.cpp +++ b/source/instancescene.cpp @@ -38,9 +38,11 @@ void InstanceScene::finish_frame() const void InstanceScene::render(Renderer &renderer, const Tag &tag) const { + setup_cullers(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) - renderer.render(**j, tag); + if(!cull(renderer, **j)) + renderer.render(**j, tag); } } // namespace GL diff --git a/source/orderedscene.cpp b/source/orderedscene.cpp index e738cad7..fe968f0c 100644 --- a/source/orderedscene.cpp +++ b/source/orderedscene.cpp @@ -49,8 +49,10 @@ void OrderedScene::finish_frame() const void OrderedScene::render(Renderer &renderer, const Tag &tag) const { + setup_cullers(renderer); for(RenderableList::const_iterator i=renderables.begin(); i!=renderables.end(); ++i) - renderer.render(**i, tag); + if(!cull(renderer, **i)) + renderer.render(**i, tag); } } // namespace GL diff --git a/source/scene.cpp b/source/scene.cpp index d95909e9..517158f0 100644 --- a/source/scene.cpp +++ b/source/scene.cpp @@ -1,14 +1,43 @@ +#include "culler.h" #include "renderer.h" #include "scene.h" +using namespace std; + namespace Msp { namespace GL { +void Scene::add_culler(Culler &c) +{ + cullers.push_back(&c); +} + +void Scene::remove_culler(Culler &c) +{ + list::iterator i = find(cullers.begin(), cullers.end(), &c); + if(i!=cullers.end()) + cullers.erase(i); +} + void Scene::render(const Tag &tag) const { Renderer renderer(0); render(renderer, tag); } +void Scene::setup_cullers(const Renderer &renderer) const +{ + for(list::const_iterator i=cullers.begin(); i!=cullers.end(); ++i) + (*i)->setup_frame(renderer); +} + +bool Scene::cull(const Renderer &renderer, const Renderable &renderable) const +{ + for(list::const_iterator i=cullers.begin(); i!=cullers.end(); ++i) + if((*i)->cull(renderer, renderable)) + return true; + return false; +} + } // namespace GL } // namespace Msp diff --git a/source/scene.h b/source/scene.h index 19048eb3..e3aa1fab 100644 --- a/source/scene.h +++ b/source/scene.h @@ -6,6 +6,8 @@ namespace Msp { namespace GL { +class Culler; + /** Scenes are containers for other Renderables. This is a base class that can't be instantiated. At the moment the available Scene types are SimpleScene, @@ -14,6 +16,8 @@ InstancedScene and OrderedScene. class Scene: public Renderable { protected: + std::list cullers; + Scene() { } public: virtual ~Scene() { } @@ -21,8 +25,15 @@ public: virtual void add(const Renderable &) = 0; virtual void remove(const Renderable &) = 0; + void add_culler(Culler &); + void remove_culler(Culler &); + using Renderable::render; virtual void render(const Tag & = Tag()) const; + +protected: + virtual void setup_cullers(const Renderer &) const; + virtual bool cull(const Renderer &, const Renderable &) const; }; } // namespace GL diff --git a/source/simplescene.cpp b/source/simplescene.cpp index f866f678..c537ff3b 100644 --- a/source/simplescene.cpp +++ b/source/simplescene.cpp @@ -28,8 +28,10 @@ void SimpleScene::finish_frame() const void SimpleScene::render(Renderer &renderer, const Tag &tag) const { + setup_cullers(renderer); for(RenderableSet::const_iterator i=renderables.begin(); i!=renderables.end(); ++i) - renderer.render(**i, tag); + if(!cull(renderer, **i)) + renderer.render(**i, tag); } } // namespace GL