/**
Scenes are containers for other Renderables. This is a base class that can't
-be instantiated. At the moment the available Scene types are SimpleScene,
+be instantiated. Examples of available Scene types are SimpleScene,
InstancedScene and OrderedScene.
*/
class Scene: public Renderable
--- /dev/null
+#include "camera.h"
+#include "renderer.h"
+#include "zsortedscene.h"
+
+using namespace std;
+
+namespace Msp {
+namespace GL {
+
+ZSortedScene::ZSortedScene():
+ order(BACK_TO_FRONT),
+ reference(FURTHEST)
+{ }
+
+void ZSortedScene::set_order(SortOrder o)
+{
+ order = o;
+}
+
+void ZSortedScene::set_reference(DepthReference r)
+{
+ reference = r;
+}
+
+void ZSortedScene::render(Renderer &renderer, const Tag &tag) const
+{
+ const Camera *camera = renderer.get_camera();
+ if(!camera)
+ return SimpleScene::render(renderer, tag);
+
+ const Matrix &view_matrix = camera->get_matrix();
+
+ std::vector<DepthRenderable> sorted_renderables;
+ sorted_renderables.reserve(renderables.size());
+
+ float radius_factor = 1.0f-reference;
+ float sign = order*2.0f-1.0f;
+
+ setup_cullers(renderer);
+ for(RenderableSet::const_iterator i=renderables.begin(); i!=renderables.end(); ++i)
+ if(!cull(renderer, **i))
+ {
+ float z = 0;
+ if(const Matrix *model_matrix = (*i)->get_matrix())
+ {
+ if(const Geometry::BoundingSphere<float, 3> *bsphere = (*i)->get_bounding_sphere())
+ z = (view_matrix**model_matrix*Vector4(bsphere->get_center(), 1.0f)).z+bsphere->get_radius()*radius_factor;
+ else
+ z = (view_matrix**model_matrix*Vector4(0.0f, 0.0f, 0.0f, 1.0f)).z;
+ }
+ sorted_renderables.push_back(DepthRenderable(z*sign, *i));
+ }
+
+ sort(sorted_renderables.begin(), sorted_renderables.end());
+
+ for(vector<DepthRenderable>::const_iterator i=sorted_renderables.begin(); i!=sorted_renderables.end(); ++i)
+ i->renderable->render(renderer, tag);
+}
+
+
+ZSortedScene::DepthRenderable::DepthRenderable(float d, const Renderable *r):
+ depth(d),
+ renderable(r)
+{ }
+
+} // namespace GL
+} // namespace Msp
--- /dev/null
+#ifndef MSP_GL_ZSORTEDSCENE_H_
+#define MSP_GL_ZSORTEDSCENE_H_
+
+#include "simplescene.h"
+
+namespace Msp {
+namespace GL {
+
+enum SortOrder
+{
+ FRONT_TO_BACK,
+ BACK_TO_FRONT
+};
+
+enum DepthReference
+{
+ CLOSEST,
+ CENTER,
+ FURTHEST
+};
+
+/**
+Sorts renderables by their distance from the camera before rendering. Requires
+renderables to have a matrix.
+*/
+class ZSortedScene: public SimpleScene
+{
+private:
+ struct DepthRenderable
+ {
+ float depth;
+ const Renderable *renderable;
+
+ DepthRenderable(float, const Renderable *);
+
+ bool operator<(const DepthRenderable &o) const { return depth<o.depth; }
+ };
+
+ SortOrder order;
+ DepthReference reference;
+
+public:
+ ZSortedScene();
+
+ /// Sets the sort order. Default is back to front.
+ void set_order(SortOrder);
+
+ /// Sets the reference point for sorting. Default is furthest from camera.
+ void set_reference(DepthReference);
+
+ virtual void render(Renderer &, const Tag &) const;
+};
+
+} // namespace GL
+} // namespace Msp
+
+#endif