if(i==content.end() || *i!=&r)
{
content.insert(i, &r);
- if(!sorted_cache.empty())
- sorted_cache.push_back(&r);
+ for(auto &kvp: sorted_cache)
+ kvp.second.push_back(&r);
}
}
reference = r;
}
-void ZSortedScene::populate_cache() const
-{
- if(sorted_cache.empty() && !content.empty())
- {
- sorted_cache.reserve(content.size());
- sorted_cache.insert(sorted_cache.end(), content.begin(), content.end());
- }
-}
-
void ZSortedScene::setup_frame(Renderer &renderer)
{
- populate_cache();
- for(const SortedRenderable &r: sorted_cache)
- r.renderable->setup_frame(renderer);
+ for(Renderable *r: content)
+ r->setup_frame(renderer);
}
void ZSortedScene::finish_frame()
{
- for(const SortedRenderable &r: sorted_cache)
- r.renderable->finish_frame();
+ for(Renderable *r: content)
+ r->finish_frame();
}
void ZSortedScene::render(Renderer &renderer, Tag tag) const
if(content.empty())
return;
- populate_cache();
-
const Camera *camera = renderer.get_camera();
if(!camera)
{
- for(const SortedRenderable &r: sorted_cache)
- r.renderable->render(renderer, tag);
+ for(Renderable *r: content)
+ r->render(renderer, tag);
return;
}
+ vector<SortedRenderable> &cache = sorted_cache[camera];
+ if(cache.empty() && !content.empty())
+ {
+ cache.reserve(content.size());
+ cache.insert(cache.end(), content.begin(), content.end());
+ }
+
const Vector3 &camera_pos = camera->get_position();
const Vector3 &look_dir = camera->get_look_direction();
float radius_factor = reference-1.0f;
float sign = 1.0f-order*2.0f;
- for(SortedRenderable &r: sorted_cache)
+ for(SortedRenderable &r: cache)
{
r.in_frustum = camera->is_in_frustum(*r.renderable);
if(!r.in_frustum)
r.depth = 0;
}
- for(auto i=sorted_cache.begin(), j=i; i!=sorted_cache.end(); ++i)
+ for(auto i=cache.begin(), j=i; i!=cache.end(); ++i)
if(i->in_frustum)
{
if(i!=j)
swap(*i, *j);
- if(j!=sorted_cache.begin() && *j<*(j-1))
+ if(j!=cache.begin() && *j<*(j-1))
{
SortedRenderable sr = *j;
auto k = j-1;
*j = *k;
- while(k!=sorted_cache.begin() && sr<*(k-1))
+ while(k!=cache.begin() && sr<*(k-1))
{
*k = *(k-1);
--k;
++j;
}
- for(auto i=sorted_cache.begin(); (i!=sorted_cache.end() && i->in_frustum); ++i)
+ for(auto i=cache.begin(); (i!=cache.end() && i->in_frustum); ++i)
i->renderable->render(renderer, tag);
}
std::vector<Renderable *> content;
SortOrder order = BACK_TO_FRONT;
DepthReference reference = FURTHEST;
- mutable std::vector<SortedRenderable> sorted_cache;
+ mutable std::map<const Camera *, std::vector<SortedRenderable> > sorted_cache;
public:
virtual void add(Renderable &);
/// Sets the reference point for sorting. Default is furthest from camera.
void set_reference(DepthReference);
-private:
- void populate_cache() const;
-
-public:
virtual void setup_frame(Renderer &);
virtual void finish_frame();