]> git.tdb.fi Git - libs/gl.git/blobdiff - source/zsortedscene.cpp
Store a Transform in keyframes instead of a Matrix
[libs/gl.git] / source / zsortedscene.cpp
index 709c5c26787478cf260d3257d069550aa38e59e6..46b14dbb6a672dbfd044b1ffa7463695e9212244 100644 (file)
@@ -12,13 +12,13 @@ ZSortedScene::ZSortedScene():
        reference(FURTHEST)
 { }
 
-void ZSortedScene::add(const Renderable &r)
+void ZSortedScene::add(Renderable &r)
 {
        if(renderables.insert(&r).second && !sorted_cache.empty())
                sorted_cache.push_back(&r);
 }
 
-void ZSortedScene::remove(const Renderable &r)
+void ZSortedScene::remove(Renderable &r)
 {
        renderables.erase(&r);
        sorted_cache.clear();
@@ -34,25 +34,43 @@ void ZSortedScene::set_reference(DepthReference r)
        reference = r;
 }
 
+void ZSortedScene::populate_cache() const
+{
+       if(sorted_cache.empty() && !renderables.empty())
+       {
+               sorted_cache.reserve(renderables.size());
+               sorted_cache.insert(sorted_cache.end(), renderables.begin(), renderables.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);
+}
+
+void ZSortedScene::finish_frame()
+{
+       for(SortedArray::const_iterator i=sorted_cache.begin(); i!=sorted_cache.end(); ++i)
+               i->renderable->finish_frame();
+}
+
 void ZSortedScene::render(Renderer &renderer, const Tag &tag) const
 {
        if(renderables.empty())
                return;
 
+       populate_cache();
+
        const Camera *camera = renderer.get_camera();
        if(!camera)
        {
-               for(RenderableSet::const_iterator i=renderables.begin(); i!=renderables.end(); ++i)
-                       renderer.render(**i, tag);
+               for(SortedArray::const_iterator i=sorted_cache.begin(); i!=sorted_cache.end(); ++i)
+                       renderer.render(*i->renderable, tag);
                return;
        }
 
-       if(sorted_cache.empty())
-       {
-               sorted_cache.reserve(renderables.size());
-               sorted_cache.insert(sorted_cache.end(), renderables.begin(), renderables.end());
-       }
-
        const Vector3 &camera_pos = camera->get_position();
        const Vector3 &look_dir = camera->get_look_direction();
        float radius_factor = reference-1.0f;
@@ -62,19 +80,19 @@ void ZSortedScene::render(Renderer &renderer, const Tag &tag) const
        for(SortedArray::iterator i=sorted_cache.begin(); i!=sorted_cache.end(); ++i)
        {
                i->in_frustum = (!use_frustum || !frustum_cull(*i->renderable));
-               if(i->in_frustum)
+               if(!i->in_frustum)
+                       continue;
+
+               if(const Matrix *matrix = i->renderable->get_matrix())
                {
-                       if(const Matrix *model_matrix = i->renderable->get_matrix())
-                       {
-                               if(const Geometry::BoundingSphere<float, 3> *bsphere = i->renderable->get_bounding_sphere())
-                                       i->depth = dot(*model_matrix*bsphere->get_center()-camera_pos, look_dir)+bsphere->get_radius()*radius_factor;
-                               else
-                                       i->depth = dot(*model_matrix*Vector3()-camera_pos, look_dir);
-                               i->depth *= sign;
-                       }
+                       if(const Geometry::BoundingSphere<float, 3> *bsphere = i->renderable->get_bounding_sphere())
+                               i->depth = dot(*matrix*bsphere->get_center()-camera_pos, look_dir)+bsphere->get_radius()*radius_factor;
                        else
-                               i->depth = 0;
+                               i->depth = dot(*matrix*Vector3()-camera_pos, look_dir);
+                       i->depth *= sign;
                }
+               else
+                       i->depth = 0;
        }
 
        for(SortedArray::iterator i=sorted_cache.begin(), j=i; i!=sorted_cache.end(); ++i)
@@ -104,10 +122,10 @@ void ZSortedScene::render(Renderer &renderer, const Tag &tag) const
 }
 
 
-ZSortedScene::SortedRenderable::SortedRenderable(const Renderable *r):
+ZSortedScene::SortedRenderable::SortedRenderable(Renderable *r):
+       renderable(r),
        in_frustum(false),
-       depth(0.0f),
-       renderable(r)
+       depth(0.0f)
 { }
 
 } // namespace GL