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