]> git.tdb.fi Git - libs/gl.git/blob - source/render/object.h
Mark constant data as const
[libs/gl.git] / source / render / object.h
1 #ifndef MSP_GL_OBJECT_H_
2 #define MSP_GL_OBJECT_H_
3
4 #include <vector>
5 #include "renderable.h"
6 #include "renderpass.h"
7 #include "resourceobserver.h"
8
9 namespace Msp {
10 namespace GL {
11
12 class Material;
13 class Mesh;
14 class ObjectInstance;
15 class Technique;
16 class Texture;
17
18 /**
19 Combines a Mesh with a Technique to give it an appearance.  The Technique will
20 define which render passes the Object supports.
21
22 In many cases, it's desirable to include multiple copies of an Object in a
23 Scene, with different model matrices.  ObjectInstances can be used to alter the
24 rendering of an object on a per-instance basis.
25
26 Objects can have multiple levels of detail.  The most detailed level has index
27 0, with increasing indices having less detail.  When rendering an instance, the
28 instance's get_level_of_detail method is called to determine which LoD to use.
29 */
30 class Object: public Renderable, private ResourceObserver
31 {
32 private:
33         struct LevelOfDetail;
34
35         class LodLoader: public DataFile::CollectionObjectLoader<Object>
36         {
37         private:
38                 unsigned index;
39                 LevelOfDetail &lod;
40
41         public:
42                 LodLoader(Object &, unsigned, Collection *);
43
44         private:
45                 void mesh(const std::string &);
46                 void mesh_inline();
47                 void technique(const std::string &);
48                 void technique_inline();
49         };
50
51 public:
52         class Loader: public LodLoader
53         {
54         public:
55                 Loader(Object &);
56                 Loader(Object &, Collection &);
57         private:
58                 void init();
59                 virtual void finish();
60
61                 void bounding_sphere_hint(float, float, float, float);
62                 void level_of_detail(unsigned);
63         };
64
65 private:
66         struct LevelOfDetail
67         {
68                 const Mesh *mesh;
69                 const Technique *technique;
70         };
71
72         std::vector<LevelOfDetail> lods;
73         Geometry::BoundingSphere<float, 3> bounding_sphere;
74         bool lod0_watched;
75
76         static const Matrix identity_matrix;
77
78 public:
79         Object();
80         Object(const Mesh *, const Technique *);
81         ~Object();
82
83 private:
84         LevelOfDetail &get_lod(unsigned, const char *);
85
86 public:
87         /** Sets the mesh for the highest level of detail (index 0). */
88         void set_mesh(const Mesh *m) { set_mesh(0, m); }
89
90         /** Sets the mesh for a given level of detail.  Previous LoDs must have been
91         defined. */
92         void set_mesh(unsigned, const Mesh *);
93
94 private:
95         void update_bounding_sphere();
96 public:
97         const Mesh *get_mesh(unsigned = 0) const;
98
99         /** Sets the technique for the highest level of detail (index 0). */
100         void set_technique(const Technique *t) { set_technique(0, t); }
101
102         /** Sets the technique for a given level of detail.  Previous LoDs must have
103         been defined. */
104         void set_technique(unsigned, const Technique *);
105
106         const Technique *get_technique(unsigned = 0) const;
107         unsigned get_n_lods() const { return lods.size(); }
108
109         virtual const Matrix *get_matrix() const { return &identity_matrix; }
110         virtual const Geometry::BoundingSphere<float, 3> *get_bounding_sphere() const { return &bounding_sphere; }
111
112         virtual void render(Renderer &, Tag = Tag()) const;
113
114         /** Renders an instance of the object.  The instance's hook functions are
115         called before and after drawing the mesh. */
116         virtual void render(Renderer &, const ObjectInstance &, Tag = Tag()) const;
117
118 protected:
119         virtual void setup_render(Renderer &, Tag) const { }
120         virtual void finish_render(Renderer &, Tag) const { }
121
122 private:
123         const RenderPass *get_pass(Tag, unsigned) const;
124
125         virtual void resource_loaded(Resource &);
126         virtual void resource_removed(Resource &);
127 };
128
129 } // namespace GL
130 } // namespace Msp
131
132 #endif