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