1 #include <msp/core/algorithm.h>
4 #include "zsortedscene.h"
11 void ZSortedScene::add(Renderable &r)
13 auto i = lower_bound(content, &r);
14 if(i==content.end() || *i!=&r)
16 content.insert(i, &r);
17 if(!sorted_cache.empty())
18 sorted_cache.push_back(&r);
22 void ZSortedScene::remove(Renderable &r)
24 auto i = lower_bound(content, &r);
25 if(i!=content.end() && *i==&r)
32 void ZSortedScene::set_order(SortOrder o)
37 void ZSortedScene::set_reference(DepthReference r)
42 void ZSortedScene::populate_cache() const
44 if(sorted_cache.empty() && !content.empty())
46 sorted_cache.reserve(content.size());
47 sorted_cache.insert(sorted_cache.end(), content.begin(), content.end());
51 void ZSortedScene::setup_frame(Renderer &renderer)
54 for(const SortedRenderable &r: sorted_cache)
55 r.renderable->setup_frame(renderer);
58 void ZSortedScene::finish_frame()
60 for(const SortedRenderable &r: sorted_cache)
61 r.renderable->finish_frame();
64 void ZSortedScene::render(Renderer &renderer, Tag tag) const
71 const Camera *camera = renderer.get_camera();
74 for(const SortedRenderable &r: sorted_cache)
75 r.renderable->render(renderer, tag);
79 const Vector3 &camera_pos = camera->get_position();
80 const Vector3 &look_dir = camera->get_look_direction();
81 float radius_factor = reference-1.0f;
82 float sign = 1.0f-order*2.0f;
84 bool use_frustum = setup_frustum(renderer);
85 for(SortedRenderable &r: sorted_cache)
87 r.in_frustum = (!use_frustum || !frustum_cull(*r.renderable));
91 if(const Matrix *matrix = r.renderable->get_matrix())
93 if(const Geometry::BoundingSphere<float, 3> *bsphere = r.renderable->get_bounding_sphere())
94 r.depth = dot(*matrix*bsphere->get_center()-camera_pos, look_dir)+bsphere->get_radius()*radius_factor;
96 r.depth = dot(*matrix*Vector3()-camera_pos, look_dir);
103 for(auto i=sorted_cache.begin(), j=i; i!=sorted_cache.end(); ++i)
109 if(j!=sorted_cache.begin() && *j<*(j-1))
111 SortedRenderable sr = *j;
114 while(k!=sorted_cache.begin() && sr<*(k-1))
125 for(auto i=sorted_cache.begin(); (i!=sorted_cache.end() && i->in_frustum); ++i)
126 i->renderable->render(renderer, tag);
130 ZSortedScene::SortedRenderable::SortedRenderable(Renderable *r):