From: Mikko Rasa Date: Mon, 5 Dec 2016 17:21:34 +0000 (+0200) Subject: Add setup/finish_frame methods to OccludedScene and ZSortedScene X-Git-Url: http://git.tdb.fi/?a=commitdiff_plain;h=120fd27fdb0b1302ef8d4367446f93cc1a116b7b;p=libs%2Fgl.git Add setup/finish_frame methods to OccludedScene and ZSortedScene --- diff --git a/source/occludedscene.cpp b/source/occludedscene.cpp index 2e140a53..b3219123 100644 --- a/source/occludedscene.cpp +++ b/source/occludedscene.cpp @@ -48,41 +48,59 @@ void OccludedScene::remove(Renderable &r) cache_dirty = true; } -void OccludedScene::render(Renderer &renderer, const Tag &tag) const +void OccludedScene::populate_cache() const { - if(renderables.empty()) + if(!cache_dirty) return; - const Camera *camera = renderer.get_camera(); - if(!camera) + if(occluded_cache.size() new_queries(occluded_cache.size()-old_size); + glGenQueries(new_queries.size(), &new_queries[0]); + for(unsigned i=0; irenderable = *i; + for(; j!=occluded_cache.end(); ++j) { - if(occluded_cache.size() new_queries(occluded_cache.size()-old_size); - glGenQueries(new_queries.size(), &new_queries[0]); - for(unsigned i=0; irenderable = 0; + j->in_frustum = false; + } - OccludedArray::iterator j = occluded_cache.begin(); - for(RenderableSet::iterator i=renderables.begin(); i!=renderables.end(); ++i, ++j) - j->renderable = *i; - for(; j!=occluded_cache.end(); ++j) - { - j->renderable = 0; - j->in_frustum = false; - } + cache_dirty = false; +} + +void OccludedScene::setup_frame(Renderer &renderer) +{ + populate_cache(); + for(OccludedArray::const_iterator i=occluded_cache.begin(); i!=occluded_cache.end(); ++i) + i->renderable->setup_frame(renderer); +} - cache_dirty = false; +void OccludedScene::finish_frame() +{ + for(OccludedArray::const_iterator i=occluded_cache.begin(); i!=occluded_cache.end(); ++i) + i->renderable->finish_frame(); +} + +void OccludedScene::render(Renderer &renderer, const Tag &tag) const +{ + if(renderables.empty()) + return; + + populate_cache(); + + const Camera *camera = renderer.get_camera(); + if(!camera) + { + for(OccludedArray::const_iterator i=occluded_cache.begin(); i!=occluded_cache.end(); ++i) + renderer.render(*i->renderable, tag); + return; } const Vector3 &camera_pos = camera->get_position(); diff --git a/source/occludedscene.h b/source/occludedscene.h index 4d26483a..6f5e23bd 100644 --- a/source/occludedscene.h +++ b/source/occludedscene.h @@ -45,6 +45,13 @@ public: virtual void add(Renderable &); virtual void remove(Renderable &); +private: + void populate_cache() const; + +public: + virtual void setup_frame(Renderer &); + virtual void finish_frame(); + virtual void render(Renderer &, const Tag &) const; }; diff --git a/source/zsortedscene.cpp b/source/zsortedscene.cpp index fe60803a..46b14dbb 100644 --- a/source/zsortedscene.cpp +++ b/source/zsortedscene.cpp @@ -34,25 +34,43 @@ void ZSortedScene::set_reference(DepthReference r) reference = r; } +void ZSortedScene::populate_cache() const +{ + if(sorted_cache.empty() && !renderables.empty()) + { + sorted_cache.reserve(renderables.size()); + sorted_cache.insert(sorted_cache.end(), renderables.begin(), renderables.end()); + } +} + +void ZSortedScene::setup_frame(Renderer &renderer) +{ + populate_cache(); + for(SortedArray::const_iterator i=sorted_cache.begin(); i!=sorted_cache.end(); ++i) + i->renderable->setup_frame(renderer); +} + +void ZSortedScene::finish_frame() +{ + for(SortedArray::const_iterator i=sorted_cache.begin(); i!=sorted_cache.end(); ++i) + i->renderable->finish_frame(); +} + void ZSortedScene::render(Renderer &renderer, const Tag &tag) const { if(renderables.empty()) return; + populate_cache(); + const Camera *camera = renderer.get_camera(); if(!camera) { - for(RenderableSet::const_iterator i=renderables.begin(); i!=renderables.end(); ++i) - renderer.render(**i, tag); + for(SortedArray::const_iterator i=sorted_cache.begin(); i!=sorted_cache.end(); ++i) + renderer.render(*i->renderable, tag); return; } - if(sorted_cache.empty()) - { - sorted_cache.reserve(renderables.size()); - sorted_cache.insert(sorted_cache.end(), renderables.begin(), renderables.end()); - } - const Vector3 &camera_pos = camera->get_position(); const Vector3 &look_dir = camera->get_look_direction(); float radius_factor = reference-1.0f; diff --git a/source/zsortedscene.h b/source/zsortedscene.h index d417777e..f7a7f6a2 100644 --- a/source/zsortedscene.h +++ b/source/zsortedscene.h @@ -59,6 +59,13 @@ public: /// Sets the reference point for sorting. Default is furthest from camera. void set_reference(DepthReference); +private: + void populate_cache() const; + +public: + virtual void setup_frame(Renderer &); + virtual void finish_frame(); + virtual void render(Renderer &, const Tag &) const; };