From: Mikko Rasa Date: Mon, 5 Dec 2016 16:35:22 +0000 (+0200) Subject: Store Renderables as non-const pointers or references X-Git-Url: http://git.tdb.fi/?a=commitdiff_plain;h=d031a80ea06e3ccd01041e9c6024fa62adf25160;p=libs%2Fgl.git Store Renderables as non-const pointers or references It's irked me for a while how some renderables (mostly effects) need to do internal updates in setup_frame, but it's const so I end up with a ton of mutables. Since almost all of my use cases actually pass in non-const Renderables anyway, I'm just going to change this. --- diff --git a/source/effect.cpp b/source/effect.cpp index c1be70f1..36a50bc3 100644 --- a/source/effect.cpp +++ b/source/effect.cpp @@ -3,7 +3,7 @@ namespace Msp { namespace GL { -Effect::Effect(const Renderable &r): +Effect::Effect(Renderable &r): renderable(r) { enabled_passes.insert(Tag()); diff --git a/source/effect.h b/source/effect.h index e99bc257..1211c3b5 100644 --- a/source/effect.h +++ b/source/effect.h @@ -15,10 +15,10 @@ the wrapped Renderable's render method, and clean up after itself. class Effect: public Renderable { protected: - const Renderable &renderable; + Renderable &renderable; std::set enabled_passes; - Effect(const Renderable &); + Effect(Renderable &); public: virtual ~Effect() { } diff --git a/source/instancescene.cpp b/source/instancescene.cpp index b3a89f33..bbaf49ed 100644 --- a/source/instancescene.cpp +++ b/source/instancescene.cpp @@ -6,12 +6,12 @@ namespace Msp { namespace GL { -void InstanceScene::add(const Renderable &r) +void InstanceScene::add(Renderable &r) { renderables[r.get_instance_key()].insert(&r); } -void InstanceScene::remove(const Renderable &r) +void InstanceScene::remove(Renderable &r) { InstanceMap::iterator i = renderables.find(r.get_instance_key()); if(i!=renderables.end()) diff --git a/source/instancescene.h b/source/instancescene.h index ecbd63df..d23fbb1f 100644 --- a/source/instancescene.h +++ b/source/instancescene.h @@ -16,14 +16,14 @@ the same key rendering order is unspecified. class InstanceScene: public Scene { private: - typedef std::set RenderableSet; + typedef std::set RenderableSet; typedef std::map InstanceMap; InstanceMap renderables; public: - virtual void add(const Renderable &); - virtual void remove(const Renderable &); + virtual void add(Renderable &); + virtual void remove(Renderable &); virtual void setup_frame() const; virtual void finish_frame() const; diff --git a/source/occludedscene.cpp b/source/occludedscene.cpp index 3d540a72..2e140a53 100644 --- a/source/occludedscene.cpp +++ b/source/occludedscene.cpp @@ -36,13 +36,13 @@ OccludedScene::~OccludedScene() glDeleteQueries(queries.size(), &queries[0]); } -void OccludedScene::add(const Renderable &r) +void OccludedScene::add(Renderable &r) { renderables.insert(&r); cache_dirty = true; } -void OccludedScene::remove(const Renderable &r) +void OccludedScene::remove(Renderable &r) { renderables.erase(&r); cache_dirty = true; diff --git a/source/occludedscene.h b/source/occludedscene.h index 3f8ca76c..4d26483a 100644 --- a/source/occludedscene.h +++ b/source/occludedscene.h @@ -19,7 +19,7 @@ class OccludedScene: public Scene private: struct OccludedRenderable { - const Renderable *renderable; + Renderable *renderable; const Geometry::BoundingSphere *bounding_sphere; bool in_frustum; bool occluder; @@ -28,7 +28,7 @@ private: OccludedRenderable(); }; - typedef std::set RenderableSet; + typedef std::set RenderableSet; typedef std::vector OccludedArray; Mesh bounding_mesh; @@ -42,8 +42,8 @@ public: OccludedScene(); ~OccludedScene(); - virtual void add(const Renderable &); - virtual void remove(const Renderable &); + virtual void add(Renderable &); + virtual void remove(Renderable &); virtual void render(Renderer &, const Tag &) const; }; diff --git a/source/orderedscene.cpp b/source/orderedscene.cpp index 965300cc..f104562f 100644 --- a/source/orderedscene.cpp +++ b/source/orderedscene.cpp @@ -5,30 +5,30 @@ namespace Msp { namespace GL { -void OrderedScene::add(const Renderable &r) +void OrderedScene::add(Renderable &r) { renderables.push_back(&r); } -void OrderedScene::remove(const Renderable &r) +void OrderedScene::remove(Renderable &r) { RenderableList::iterator end = std::remove(renderables.begin(), renderables.end(), &r); renderables.erase(end, renderables.end()); } -void OrderedScene::prepend(const Renderable &r) +void OrderedScene::prepend(Renderable &r) { renderables.push_front(&r); } -void OrderedScene::insert(unsigned index, const Renderable &r) +void OrderedScene::insert(unsigned index, Renderable &r) { RenderableList::iterator i = renderables.begin(); for(; (i!=renderables.end() && index); ++i, --index) ; renderables.insert(i, &r); } -void OrderedScene::insert_after(const Renderable &after, const Renderable &r) +void OrderedScene::insert_after(Renderable &after, Renderable &r) { RenderableList::iterator i = renderables.begin(); for(; (i!=renderables.end() && *i!=&after); ++i) ; diff --git a/source/orderedscene.h b/source/orderedscene.h index 340eceda..ee06dfae 100644 --- a/source/orderedscene.h +++ b/source/orderedscene.h @@ -14,16 +14,16 @@ in the middle and removing them are O(N) operations. class OrderedScene: public Scene { private: - typedef std::list RenderableList; + typedef std::list RenderableList; RenderableList renderables; public: - virtual void add(const Renderable &); - virtual void remove(const Renderable &); - void prepend(const Renderable &); - void insert(unsigned, const Renderable &); - void insert_after(const Renderable &, const Renderable &); + virtual void add(Renderable &); + virtual void remove(Renderable &); + void prepend(Renderable &); + void insert(unsigned, Renderable &); + void insert_after(Renderable &, Renderable &); virtual void setup_frame() const; virtual void finish_frame() const; diff --git a/source/pipeline.cpp b/source/pipeline.cpp index 0469cb77..ce9c8f68 100644 --- a/source/pipeline.cpp +++ b/source/pipeline.cpp @@ -81,7 +81,7 @@ Pipeline::Pass &Pipeline::add_pass(const Tag &tag) return passes.back(); } -void Pipeline::add_renderable(const Renderable &r) +void Pipeline::add_renderable(Renderable &r) { for(vector::iterator i=renderables.begin(); i!=renderables.end(); ++i) if(i->renderable==&r) @@ -93,7 +93,7 @@ void Pipeline::add_renderable(const Renderable &r) renderables.push_back(&r); } -void Pipeline::add_renderable_for_pass(const Renderable &r, const Tag &tag) +void Pipeline::add_renderable_for_pass(Renderable &r, const Tag &tag) { for(vector::iterator i=renderables.begin(); i!=renderables.end(); ++i) if(i->renderable==&r) @@ -106,7 +106,7 @@ void Pipeline::add_renderable_for_pass(const Renderable &r, const Tag &tag) renderables.back().passes.insert(tag); } -void Pipeline::remove_renderable(const Renderable &r) +void Pipeline::remove_renderable(Renderable &r) { for(vector::iterator i=renderables.begin(); i!=renderables.end(); ++i) if(i->renderable==&r) @@ -116,7 +116,7 @@ void Pipeline::remove_renderable(const Renderable &r) } } -Pipeline::Pass &Pipeline::add_pass(const Tag &tag, const Renderable &r) +Pipeline::Pass &Pipeline::add_pass(const Tag &tag, Renderable &r) { passes.push_back(Pass(tag, &r)); return passes.back(); @@ -256,7 +256,7 @@ void Pipeline::create_targets(unsigned recreate) } -Pipeline::Pass::Pass(const Tag &t, const Renderable *r): +Pipeline::Pass::Pass(const Tag &t, Renderable *r): tag(t), lighting(0), depth_test(0), @@ -286,7 +286,7 @@ void Pipeline::Pass::set_clipping(const Clipping *c) } -Pipeline::Slot::Slot(const Renderable *r): +Pipeline::Slot::Slot(Renderable *r): renderable(r) { } diff --git a/source/pipeline.h b/source/pipeline.h index f6a4ab33..b450d0a5 100644 --- a/source/pipeline.h +++ b/source/pipeline.h @@ -43,10 +43,10 @@ public: const DepthTest *depth_test; const Blend *blend; const Clipping *clipping; - const Renderable *renderable; + Renderable *renderable; public: - Pass(const Tag &, const Renderable *); + Pass(const Tag &, Renderable *); const Tag &get_tag() const { return tag; } @@ -58,16 +58,16 @@ public: const DepthTest *get_depth_test() const { return depth_test; } const Blend *get_blend() const { return blend; } const Clipping *get_clipping() const { return clipping; } - const Renderable *get_renderable() const { return renderable; } + Renderable *get_renderable() const { return renderable; } }; private: struct Slot { - const Renderable *renderable; + Renderable *renderable; std::set passes; - Slot(const Renderable *); + Slot(Renderable *); }; typedef std::list PassList; @@ -93,13 +93,13 @@ public: // Deprecated void set_camera(const Camera *); Pass &add_pass(const Tag &tag); - void add_renderable(const Renderable &); - void add_renderable_for_pass(const Renderable &, const Tag &); - void remove_renderable(const Renderable &); + void add_renderable(Renderable &); + void add_renderable_for_pass(Renderable &, const Tag &); + void remove_renderable(Renderable &); /** Adds a pass to the pipeline. It's permissible to add the same Renderable multiple times. */ - Pass &add_pass(const Tag &, const Renderable &); + Pass &add_pass(const Tag &, Renderable &); /** Adds a postprocessor to the pipeline. */ void add_postprocessor(PostProcessor &); diff --git a/source/scene.h b/source/scene.h index 9e2da3df..e6e337b9 100644 --- a/source/scene.h +++ b/source/scene.h @@ -39,8 +39,8 @@ private: public: virtual ~Scene(); - virtual void add(const Renderable &) = 0; - virtual void remove(const Renderable &) = 0; + virtual void add(Renderable &) = 0; + virtual void remove(Renderable &) = 0; protected: bool setup_frustum(const Renderer &) const; diff --git a/source/shadowmap.cpp b/source/shadowmap.cpp index 4ad192ca..d8853d9e 100644 --- a/source/shadowmap.cpp +++ b/source/shadowmap.cpp @@ -15,7 +15,7 @@ using namespace std; namespace Msp { namespace GL { -ShadowMap::ShadowMap(unsigned s, const Renderable &r, const Light &l): +ShadowMap::ShadowMap(unsigned s, Renderable &r, const Light &l): Effect(r), size(s), light(l), diff --git a/source/shadowmap.h b/source/shadowmap.h index 119d369c..658b59a9 100644 --- a/source/shadowmap.h +++ b/source/shadowmap.h @@ -34,7 +34,7 @@ private: mutable bool rendered; public: - ShadowMap(unsigned, const Renderable &, const Light &); + ShadowMap(unsigned, Renderable &, const Light &); /** Sets the ShadowMap target point and radius. The transformation matrix is computed so that a sphere with the specified parameters will be completely diff --git a/source/simplescene.cpp b/source/simplescene.cpp index b8c90517..137873da 100644 --- a/source/simplescene.cpp +++ b/source/simplescene.cpp @@ -4,14 +4,14 @@ namespace Msp { namespace GL { -void SimpleScene::add(const Renderable &r) +void SimpleScene::add(Renderable &r) { // Add to cache as well if the cache is valid if(renderables.insert(&r).second && !cache.empty()) cache.push_back(&r); } -void SimpleScene::remove(const Renderable &r) +void SimpleScene::remove(Renderable &r) { renderables.erase(&r); cache.clear(); diff --git a/source/simplescene.h b/source/simplescene.h index fd834111..8b91150c 100644 --- a/source/simplescene.h +++ b/source/simplescene.h @@ -13,15 +13,15 @@ A simple yet efficient scene. Rendering order is unspecified. class SimpleScene: public Scene { private: - typedef std::set RenderableSet; - typedef std::vector RenderableArray; + typedef std::set RenderableSet; + typedef std::vector RenderableArray; RenderableSet renderables; mutable RenderableArray cache; public: - virtual void add(const Renderable &); - virtual void remove(const Renderable &); + virtual void add(Renderable &); + virtual void remove(Renderable &); private: void populate_cache() const; diff --git a/source/view.cpp b/source/view.cpp index 6548c7f1..759e90be 100644 --- a/source/view.cpp +++ b/source/view.cpp @@ -23,7 +23,7 @@ void View::set_camera(Camera *c) camera = c; } -void View::set_content(const Renderable *r) +void View::set_content(Renderable *r) { content = r; } diff --git a/source/view.h b/source/view.h index bd39e8ff..f7f47fc7 100644 --- a/source/view.h +++ b/source/view.h @@ -23,7 +23,7 @@ private: Graphics::GLContext &context; Framebuffer ⌖ Camera *camera; - const Renderable *content; + Renderable *content; std::list synced_cameras; public: @@ -36,7 +36,7 @@ public: float get_aspect() const { return static_cast(get_width())/get_height(); } void set_camera(Camera *); - void set_content(const Renderable *); + void set_content(Renderable *); // Deprecated void synchronize_camera_aspect(Camera &); diff --git a/source/zsortedscene.cpp b/source/zsortedscene.cpp index f40903dd..fe60803a 100644 --- a/source/zsortedscene.cpp +++ b/source/zsortedscene.cpp @@ -12,13 +12,13 @@ ZSortedScene::ZSortedScene(): reference(FURTHEST) { } -void ZSortedScene::add(const Renderable &r) +void ZSortedScene::add(Renderable &r) { if(renderables.insert(&r).second && !sorted_cache.empty()) sorted_cache.push_back(&r); } -void ZSortedScene::remove(const Renderable &r) +void ZSortedScene::remove(Renderable &r) { renderables.erase(&r); sorted_cache.clear(); @@ -104,7 +104,7 @@ void ZSortedScene::render(Renderer &renderer, const Tag &tag) const } -ZSortedScene::SortedRenderable::SortedRenderable(const Renderable *r): +ZSortedScene::SortedRenderable::SortedRenderable(Renderable *r): renderable(r), in_frustum(false), depth(0.0f) diff --git a/source/zsortedscene.h b/source/zsortedscene.h index b032cbcd..d417777e 100644 --- a/source/zsortedscene.h +++ b/source/zsortedscene.h @@ -30,16 +30,16 @@ class ZSortedScene: public Scene private: struct SortedRenderable { - const Renderable *renderable; + Renderable *renderable; bool in_frustum; float depth; - SortedRenderable(const Renderable *); + SortedRenderable(Renderable *); bool operator<(const SortedRenderable &o) const { return depth RenderableSet; + typedef std::set RenderableSet; typedef std::vector SortedArray; RenderableSet renderables; @@ -50,8 +50,8 @@ private: public: ZSortedScene(); - virtual void add(const Renderable &); - virtual void remove(const Renderable &); + virtual void add(Renderable &); + virtual void remove(Renderable &); /// Sets the sort order. Default is back to front. void set_order(SortOrder);