]> git.tdb.fi Git - libs/gl.git/blobdiff - source/zsortedscene.h
Add a Scene type that sorts its renderables by depth
[libs/gl.git] / source / zsortedscene.h
diff --git a/source/zsortedscene.h b/source/zsortedscene.h
new file mode 100644 (file)
index 0000000..f9f6f7e
--- /dev/null
@@ -0,0 +1,57 @@
+#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