X-Git-Url: http://git.tdb.fi/?p=libs%2Fgl.git;a=blobdiff_plain;f=source%2Frender%2Fzsortedscene.cpp;h=25255dc693569a9d34b2adfc7b64f709089af63e;hp=46b14dbb6a672dbfd044b1ffa7463695e9212244;hb=016f0f0dd51511f98d0bf398d99199d7dec1543c;hpb=7aaec9a70b8d7733429bec043f8e33e02956f266 diff --git a/source/render/zsortedscene.cpp b/source/render/zsortedscene.cpp index 46b14dbb..25255dc6 100644 --- a/source/render/zsortedscene.cpp +++ b/source/render/zsortedscene.cpp @@ -1,3 +1,4 @@ +#include #include "camera.h" #include "renderer.h" #include "zsortedscene.h" @@ -7,21 +8,25 @@ using namespace std; namespace Msp { namespace GL { -ZSortedScene::ZSortedScene(): - order(BACK_TO_FRONT), - reference(FURTHEST) -{ } - void ZSortedScene::add(Renderable &r) { - if(renderables.insert(&r).second && !sorted_cache.empty()) - sorted_cache.push_back(&r); + auto i = lower_bound(content, &r); + if(i==content.end() || *i!=&r) + { + content.insert(i, &r); + if(!sorted_cache.empty()) + sorted_cache.push_back(&r); + } } void ZSortedScene::remove(Renderable &r) { - renderables.erase(&r); - sorted_cache.clear(); + auto i = lower_bound(content, &r); + if(i!=content.end() && *i==&r) + { + content.erase(i); + sorted_cache.clear(); + } } void ZSortedScene::set_order(SortOrder o) @@ -36,29 +41,29 @@ void ZSortedScene::set_reference(DepthReference r) void ZSortedScene::populate_cache() const { - if(sorted_cache.empty() && !renderables.empty()) + if(sorted_cache.empty() && !content.empty()) { - sorted_cache.reserve(renderables.size()); - sorted_cache.insert(sorted_cache.end(), renderables.begin(), renderables.end()); + sorted_cache.reserve(content.size()); + sorted_cache.insert(sorted_cache.end(), content.begin(), content.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); + for(const SortedRenderable &r: sorted_cache) + r.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(); + for(const SortedRenderable &r: sorted_cache) + r.renderable->finish_frame(); } -void ZSortedScene::render(Renderer &renderer, const Tag &tag) const +void ZSortedScene::render(Renderer &renderer, Tag tag) const { - if(renderables.empty()) + if(content.empty()) return; populate_cache(); @@ -66,8 +71,8 @@ void ZSortedScene::render(Renderer &renderer, const Tag &tag) const const Camera *camera = renderer.get_camera(); if(!camera) { - for(SortedArray::const_iterator i=sorted_cache.begin(); i!=sorted_cache.end(); ++i) - renderer.render(*i->renderable, tag); + for(const SortedRenderable &r: sorted_cache) + r.renderable->render(renderer, tag); return; } @@ -77,25 +82,25 @@ void ZSortedScene::render(Renderer &renderer, const Tag &tag) const float sign = 1.0f-order*2.0f; bool use_frustum = setup_frustum(renderer); - for(SortedArray::iterator i=sorted_cache.begin(); i!=sorted_cache.end(); ++i) + for(SortedRenderable &r: sorted_cache) { - i->in_frustum = (!use_frustum || !frustum_cull(*i->renderable)); - if(!i->in_frustum) + r.in_frustum = (!use_frustum || !frustum_cull(*r.renderable)); + if(!r.in_frustum) continue; - if(const Matrix *matrix = i->renderable->get_matrix()) + if(const Matrix *matrix = r.renderable->get_matrix()) { - if(const Geometry::BoundingSphere *bsphere = i->renderable->get_bounding_sphere()) - i->depth = dot(*matrix*bsphere->get_center()-camera_pos, look_dir)+bsphere->get_radius()*radius_factor; + if(const Geometry::BoundingSphere *bsphere = r.renderable->get_bounding_sphere()) + r.depth = dot(*matrix*bsphere->get_center()-camera_pos, look_dir)+bsphere->get_radius()*radius_factor; else - i->depth = dot(*matrix*Vector3()-camera_pos, look_dir); - i->depth *= sign; + r.depth = dot(*matrix*Vector3()-camera_pos, look_dir); + r.depth *= sign; } else - i->depth = 0; + r.depth = 0; } - for(SortedArray::iterator i=sorted_cache.begin(), j=i; i!=sorted_cache.end(); ++i) + for(auto i=sorted_cache.begin(), j=i; i!=sorted_cache.end(); ++i) if(i->in_frustum) { if(i!=j) @@ -104,7 +109,7 @@ void ZSortedScene::render(Renderer &renderer, const Tag &tag) const if(j!=sorted_cache.begin() && *j<*(j-1)) { SortedRenderable sr = *j; - SortedArray::iterator k = j-1; + auto k = j-1; *j = *k; while(k!=sorted_cache.begin() && sr<*(k-1)) { @@ -117,8 +122,8 @@ void ZSortedScene::render(Renderer &renderer, const Tag &tag) const ++j; } - for(SortedArray::const_iterator i=sorted_cache.begin(); (i!=sorted_cache.end() && i->in_frustum); ++i) - renderer.render(*i->renderable, tag); + for(auto i=sorted_cache.begin(); (i!=sorted_cache.end() && i->in_frustum); ++i) + i->renderable->render(renderer, tag); }