]> git.tdb.fi Git - libs/gl.git/blob - source/render/object.cpp
Validate GLSL flow control
[libs/gl.git] / source / render / object.cpp
1 #include <msp/datafile/collection.h>
2 #include <msp/strings/format.h>
3 #include "error.h"
4 #include "material.h"
5 #include "mesh.h"
6 #include "object.h"
7 #include "objectinstance.h"
8 #include "program.h"
9 #include "programdata.h"
10 #include "renderer.h"
11 #include "resourcemanager.h"
12 #include "technique.h"
13 #include "texturing.h"
14
15 using namespace std;
16
17 namespace Msp {
18 namespace GL {
19
20 Matrix Object::identity_matrix;
21
22 Object::Object():
23         lods(1),
24         lod0_watched(false)
25 { }
26
27 Object::Object(const Mesh *m, const Technique *t):
28         lods(1),
29         lod0_watched(false)
30 {
31         set_mesh(m);
32         set_technique(t);
33 }
34
35 // TODO should have copy-c'tor to set watch on lod0 mesh if necessary
36
37 Object::~Object()
38 {
39         if(lods[0].mesh && lod0_watched)
40                 if(ResourceManager *rm = lods[0].mesh->get_manager())
41                         rm->unobserve_resource(*lods[0].mesh, *this);
42 }
43
44 Object::LevelOfDetail &Object::get_lod(unsigned i, const char *caller)
45 {
46         if(i>lods.size())
47                 throw out_of_range(caller);
48         if(i>0 && (!lods[0].mesh || !lods[0].technique))
49                 throw invalid_operation(caller);
50
51         if(i==lods.size())
52                 lods.push_back(lods.back());
53
54         return lods[i];
55 }
56
57 void Object::set_mesh(unsigned i, const Mesh *m)
58 {
59         RefPtr<const Mesh> &ptr = get_lod(i, "Object::set_mesh").mesh;
60         if(i==0 && ptr && lod0_watched)
61                 if(ResourceManager *rm = ptr->get_manager())
62                         rm->unobserve_resource(*ptr, *this);
63         ptr = m;
64         ptr.keep();
65         lod0_watched = false;
66
67         if(i==0 && m)
68                 if(ResourceManager *rm = m->get_manager())
69                 {
70                         rm->observe_resource(*m, *this);
71                         lod0_watched = true;
72                 }
73
74         update_bounding_sphere();
75 }
76
77 void Object::update_bounding_sphere()
78 {
79         vector<Vector3> points;
80         for(vector<LevelOfDetail>::const_iterator i=lods.begin(); i!=lods.end(); ++i)
81         {
82                 if(!i->mesh || !i->mesh->is_loaded())
83                         continue;
84
85                 const VertexArray &vertices = i->mesh->get_vertices();
86
87                 int offset = vertices.get_format().offset(VERTEX3);
88                 bool three = true;
89                 if(offset<0)
90                 {
91                         offset = vertices.get_format().offset(VERTEX2);
92                         three = false;
93                         if(offset<0)
94                                 continue;
95                 }
96
97                 unsigned n_vertices = vertices.size();
98                 points.reserve(points.size()+n_vertices);
99                 for(unsigned j=0; j<n_vertices; ++j)
100                 {
101                         const float *v = vertices[j];
102                         points.push_back(Vector3(v[offset], v[offset+1], (three ? v[offset+2] : 0.0f)));
103                 }
104         }
105
106         /* Don't touch the bounding sphere if we had no vertices to avoid
107         overwriting a possible hint. */
108         if(points.empty())
109                 return;
110
111         bounding_sphere = Geometry::BoundingSphere<float, 3>::from_point_cloud(points.begin(), points.end());
112 }
113
114 const Mesh *Object::get_mesh(unsigned i) const
115 {
116         if(i>=lods.size())
117                 return 0;
118
119         return lods[i].mesh.get();
120 }
121
122 void Object::set_technique(unsigned i, const Technique *t)
123 {
124         RefPtr<const Technique> &ptr = get_lod(i, "Object::set_technique").technique;
125         ptr = t;
126         ptr.keep();
127 }
128
129 const Technique *Object::get_technique(unsigned i) const
130 {
131         if(i>=lods.size())
132                 return 0;
133
134         return lods[i].technique.get();
135 }
136
137 void Object::render(Renderer &renderer, const Tag &tag) const
138 {
139         const RenderPass *pass = get_pass(tag, 0);
140         if(!pass)
141                 return;
142
143         const Mesh *mesh = lods.front().mesh.get();
144         if (!mesh)
145                 throw logic_error("no mesh");
146
147         Renderer::Push push(renderer);
148         pass->apply(renderer);
149
150         setup_render(renderer, tag);
151         mesh->draw(renderer);
152         finish_render(renderer, tag);
153 }
154
155 void Object::render(Renderer &renderer, const ObjectInstance &inst, const Tag &tag) const
156 {
157         unsigned lod = min<unsigned>(inst.get_level_of_detail(renderer), lods.size()-1);
158         const RenderPass *pass = get_pass(tag, lod);
159         if(!pass)
160                 return;
161
162         const Mesh *mesh = lods[lod].mesh.get();
163         if (!mesh)
164                 throw logic_error("no mesh");
165
166         Renderer::Push push(renderer);
167         pass->apply(renderer);
168
169         setup_render(renderer, tag);
170         inst.setup_render(renderer, tag);
171         mesh->draw(renderer);
172         inst.finish_render(renderer, tag);
173         finish_render(renderer, tag);
174 }
175
176 const RenderPass *Object::get_pass(const Tag &tag, unsigned lod) const
177 {
178         const Technique *tech = lods[lod].technique.get();
179         if(!tech)
180                 throw logic_error("no technique");
181         return tech->find_pass(tag);
182 }
183
184 void Object::resource_loaded(Resource &res)
185 {
186         if(&res==lods.front().mesh.get() && bounding_sphere.is_empty())
187                 update_bounding_sphere();
188 }
189
190 void Object::resource_removed(Resource &res)
191 {
192         if(&res==lods.front().mesh.get())
193                 lod0_watched = false;
194 }
195
196
197 Object::Loader::Loader(Object &o):
198         LodLoader(o, 0, 0)
199 {
200         init();
201 }
202
203 Object::Loader::Loader(Object &o, Collection &c):
204         LodLoader(o, 0, &c)
205 {
206         init();
207 }
208
209 void Object::Loader::init()
210 {
211         add("bounding_sphere_hint", &Loader::bounding_sphere_hint);
212         add("level_of_detail", &Loader::level_of_detail);
213 }
214
215 void Object::Loader::finish()
216 {
217         obj.update_bounding_sphere();
218 }
219
220 void Object::Loader::bounding_sphere_hint(float x, float y, float z, float r)
221 {
222         obj.bounding_sphere = Geometry::BoundingSphere<float, 3>(Vector3(x, y, z), r);
223 }
224
225 void Object::Loader::level_of_detail(unsigned i)
226 {
227         LodLoader ldr(obj, i, coll);
228         load_sub_with(ldr);
229 }
230
231
232 Object::LodLoader::LodLoader(Object &o, unsigned i, Collection *c):
233         DataFile::CollectionObjectLoader<Object>(o, c),
234         index(i),
235         lod(obj.get_lod(index, "Object::LodLoader::LodLoader"))
236 {
237         add("mesh",      &LodLoader::mesh_inline);
238         add("mesh",      &LodLoader::mesh);
239         add("technique", &LodLoader::technique_inline);
240         add("technique", &LodLoader::technique);
241 }
242
243 void Object::LodLoader::mesh(const string &n)
244 {
245         obj.set_mesh(index, &get_collection().get<Mesh>(n));
246 }
247
248 void Object::LodLoader::mesh_inline()
249 {
250         RefPtr<Mesh> msh = new Mesh;
251         load_sub(*msh);
252         lod.mesh = msh;
253 }
254
255 void Object::LodLoader::technique(const std::string &n)
256 {
257         obj.set_technique(index, &get_collection().get<Technique>(n));
258 }
259
260 void Object::LodLoader::technique_inline()
261 {
262         RefPtr<Technique> tech = new Technique;
263         if(coll)
264                 load_sub(*tech, get_collection());
265         else
266                 load_sub(*tech);
267         lod.technique = tech;
268 }
269
270 } // namespace GL
271 } // namespace Msp