3 #include "zsortedscene.h"
10 void ZSortedScene::add(Renderable &r)
12 if(renderables.insert(&r).second && !sorted_cache.empty())
13 sorted_cache.push_back(&r);
16 void ZSortedScene::remove(Renderable &r)
18 renderables.erase(&r);
22 void ZSortedScene::set_order(SortOrder o)
27 void ZSortedScene::set_reference(DepthReference r)
32 void ZSortedScene::populate_cache() const
34 if(sorted_cache.empty() && !renderables.empty())
36 sorted_cache.reserve(renderables.size());
37 sorted_cache.insert(sorted_cache.end(), renderables.begin(), renderables.end());
41 void ZSortedScene::setup_frame(Renderer &renderer)
44 for(const SortedRenderable &r: sorted_cache)
45 r.renderable->setup_frame(renderer);
48 void ZSortedScene::finish_frame()
50 for(const SortedRenderable &r: sorted_cache)
51 r.renderable->finish_frame();
54 void ZSortedScene::render(Renderer &renderer, Tag tag) const
56 if(renderables.empty())
61 const Camera *camera = renderer.get_camera();
64 for(const SortedRenderable &r: sorted_cache)
65 renderer.render(*r.renderable, tag);
69 const Vector3 &camera_pos = camera->get_position();
70 const Vector3 &look_dir = camera->get_look_direction();
71 float radius_factor = reference-1.0f;
72 float sign = 1.0f-order*2.0f;
74 bool use_frustum = setup_frustum(renderer);
75 for(SortedRenderable &r: sorted_cache)
77 r.in_frustum = (!use_frustum || !frustum_cull(*r.renderable));
81 if(const Matrix *matrix = r.renderable->get_matrix())
83 if(const Geometry::BoundingSphere<float, 3> *bsphere = r.renderable->get_bounding_sphere())
84 r.depth = dot(*matrix*bsphere->get_center()-camera_pos, look_dir)+bsphere->get_radius()*radius_factor;
86 r.depth = dot(*matrix*Vector3()-camera_pos, look_dir);
93 for(auto i=sorted_cache.begin(), j=i; i!=sorted_cache.end(); ++i)
99 if(j!=sorted_cache.begin() && *j<*(j-1))
101 SortedRenderable sr = *j;
104 while(k!=sorted_cache.begin() && sr<*(k-1))
115 for(auto i=sorted_cache.begin(); (i!=sorted_cache.end() && i->in_frustum); ++i)
116 renderer.render(*i->renderable, tag);
120 ZSortedScene::SortedRenderable::SortedRenderable(Renderable *r):