]> git.tdb.fi Git - libs/gl.git/blob - source/zsortedscene.h
Add a Scene type that sorts its renderables by depth
[libs/gl.git] / source / zsortedscene.h
1 #ifndef MSP_GL_ZSORTEDSCENE_H_
2 #define MSP_GL_ZSORTEDSCENE_H_
3
4 #include "simplescene.h"
5
6 namespace Msp {
7 namespace GL {
8
9 enum SortOrder
10 {
11         FRONT_TO_BACK,
12         BACK_TO_FRONT
13 };
14
15 enum DepthReference
16 {
17         CLOSEST,
18         CENTER,
19         FURTHEST
20 };
21
22 /**
23 Sorts renderables by their distance from the camera before rendering.  Requires
24 renderables to have a matrix.
25 */
26 class ZSortedScene: public SimpleScene
27 {
28 private:
29         struct DepthRenderable
30         {
31                 float depth;
32                 const Renderable *renderable;
33
34                 DepthRenderable(float, const Renderable *);
35
36                 bool operator<(const DepthRenderable &o) const { return depth<o.depth; }
37         };
38
39         SortOrder order;
40         DepthReference reference;
41
42 public:
43         ZSortedScene();
44
45         /// Sets the sort order.  Default is back to front.
46         void set_order(SortOrder);
47
48         /// Sets the reference point for sorting.  Default is furthest from camera.
49         void set_reference(DepthReference);
50
51         virtual void render(Renderer &, const Tag &) const;
52 };
53
54 } // namespace GL
55 } // namespace Msp
56
57 #endif