3 #include "zsortedscene.h"
10 ZSortedScene::ZSortedScene():
15 void ZSortedScene::add(Renderable &r)
17 if(renderables.insert(&r).second && !sorted_cache.empty())
18 sorted_cache.push_back(&r);
21 void ZSortedScene::remove(Renderable &r)
23 renderables.erase(&r);
27 void ZSortedScene::set_order(SortOrder o)
32 void ZSortedScene::set_reference(DepthReference r)
37 void ZSortedScene::populate_cache() const
39 if(sorted_cache.empty() && !renderables.empty())
41 sorted_cache.reserve(renderables.size());
42 sorted_cache.insert(sorted_cache.end(), renderables.begin(), renderables.end());
46 void ZSortedScene::setup_frame(Renderer &renderer)
49 for(SortedArray::const_iterator i=sorted_cache.begin(); i!=sorted_cache.end(); ++i)
50 i->renderable->setup_frame(renderer);
53 void ZSortedScene::finish_frame()
55 for(SortedArray::const_iterator i=sorted_cache.begin(); i!=sorted_cache.end(); ++i)
56 i->renderable->finish_frame();
59 void ZSortedScene::render(Renderer &renderer, const Tag &tag) const
61 if(renderables.empty())
66 const Camera *camera = renderer.get_camera();
69 for(SortedArray::const_iterator i=sorted_cache.begin(); i!=sorted_cache.end(); ++i)
70 renderer.render(*i->renderable, tag);
74 const Vector3 &camera_pos = camera->get_position();
75 const Vector3 &look_dir = camera->get_look_direction();
76 float radius_factor = reference-1.0f;
77 float sign = 1.0f-order*2.0f;
79 bool use_frustum = setup_frustum(renderer);
80 for(SortedArray::iterator i=sorted_cache.begin(); i!=sorted_cache.end(); ++i)
82 i->in_frustum = (!use_frustum || !frustum_cull(*i->renderable));
86 if(const Matrix *matrix = i->renderable->get_matrix())
88 if(const Geometry::BoundingSphere<float, 3> *bsphere = i->renderable->get_bounding_sphere())
89 i->depth = dot(*matrix*bsphere->get_center()-camera_pos, look_dir)+bsphere->get_radius()*radius_factor;
91 i->depth = dot(*matrix*Vector3()-camera_pos, look_dir);
98 for(SortedArray::iterator i=sorted_cache.begin(), j=i; i!=sorted_cache.end(); ++i)
104 if(j!=sorted_cache.begin() && *j<*(j-1))
106 SortedRenderable sr = *j;
107 SortedArray::iterator k = j-1;
109 while(k!=sorted_cache.begin() && sr<*(k-1))
120 for(SortedArray::const_iterator i=sorted_cache.begin(); (i!=sorted_cache.end() && i->in_frustum); ++i)
121 renderer.render(*i->renderable, tag);
125 ZSortedScene::SortedRenderable::SortedRenderable(Renderable *r):