]> git.tdb.fi Git - libs/gl.git/blob - source/zsortedscene.h
Use a persistent sorted array in ZSortedScene
[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 Scene
27 {
28 private:
29         struct SortedRenderable
30         {
31                 bool in_frustum;
32                 float depth;
33                 const Renderable *renderable;
34
35                 SortedRenderable(const Renderable *);
36
37                 bool operator<(const SortedRenderable &o) const { return depth<o.depth; }
38         };
39
40         typedef std::set<const Renderable *> RenderableSet;
41         typedef std::vector<SortedRenderable> SortedArray;
42
43         RenderableSet renderables;
44         SortOrder order;
45         DepthReference reference;
46         mutable SortedArray sorted_cache;
47
48 public:
49         ZSortedScene();
50
51         virtual void add(const Renderable &);
52         virtual void remove(const Renderable &);
53
54         /// Sets the sort order.  Default is back to front.
55         void set_order(SortOrder);
56
57         /// Sets the reference point for sorting.  Default is furthest from camera.
58         void set_reference(DepthReference);
59
60         virtual void render(Renderer &, const Tag &) const;
61 };
62
63 } // namespace GL
64 } // namespace Msp
65
66 #endif