1 #include <msp/datafile/collection.h>
2 #include <msp/fs/utils.h>
3 #include <msp/strings/format.h>
7 #include "objectinstance.h"
9 #include "resourcemanager.h"
10 #include "technique.h"
17 const Matrix Object::identity_matrix;
23 Object::Object(const Mesh *m, const Technique *t):
30 // TODO should have copy-c'tor to set watch on lod0 mesh if necessary
34 if(lods[0].mesh && lod0_watched)
35 if(ResourceManager *rm = lods[0].mesh->get_manager())
36 rm->unobserve_resource(*lods[0].mesh, *this);
39 Object::LevelOfDetail &Object::get_lod(unsigned i, const char *caller)
42 throw out_of_range(caller);
43 if(i>0 && (!lods[0].mesh || !lods[0].technique))
44 throw invalid_operation(caller);
47 lods.push_back(lods.back());
52 void Object::set_mesh(unsigned i, const Mesh *m)
54 const Mesh *&ptr = get_lod(i, "Object::set_mesh").mesh;
55 if(i==0 && ptr && lod0_watched)
56 if(ResourceManager *rm = ptr->get_manager())
57 rm->unobserve_resource(*ptr, *this);
62 if(ResourceManager *rm = m->get_manager())
64 rm->observe_resource(*m, *this);
68 update_bounding_sphere();
71 void Object::update_bounding_sphere()
73 vector<Vector3> points;
74 for(const LevelOfDetail &l: lods)
76 if(!l.mesh || !l.mesh->is_loaded())
79 const VertexArray &vertices = l.mesh->get_vertices();
81 int offset = vertices.get_format().offset(VERTEX3);
85 offset = vertices.get_format().offset(VERTEX2);
91 unsigned n_vertices = vertices.size();
92 points.reserve(points.size()+n_vertices);
93 for(unsigned j=0; j<n_vertices; ++j)
95 const float *v = reinterpret_cast<const float *>(vertices[j]+offset);
96 points.push_back(Vector3(v[0], v[1], (three ? v[2] : 0.0f)));
100 /* Don't touch the bounding sphere if we had no vertices to avoid
101 overwriting a possible hint. */
105 bounding_sphere = Geometry::BoundingSphere<float, 3>::from_point_cloud(points.begin(), points.end());
108 const Mesh *Object::get_mesh(unsigned i) const
116 void Object::set_technique(unsigned i, const Technique *t)
118 get_lod(i, "Object::set_technique").technique = t;
121 const Technique *Object::get_technique(unsigned i) const
126 return lods[i].technique;
129 void Object::render(Renderer &renderer, Tag tag) const
131 const RenderMethod *method = get_method(tag, 0);
135 const Mesh *mesh = lods.front().mesh;
137 throw logic_error("no mesh");
139 Renderer::Push push(renderer);
140 method->apply(renderer);
142 setup_render(renderer, tag);
143 mesh->draw(renderer);
144 finish_render(renderer, tag);
147 void Object::render(Renderer &renderer, const ObjectInstance &inst, Tag tag) const
149 unsigned lod = min<unsigned>(inst.get_level_of_detail(renderer), lods.size()-1);
150 const RenderMethod *method = get_method(tag, lod);
154 const Mesh *mesh = lods[lod].mesh;
156 throw logic_error("no mesh");
158 Renderer::Push push(renderer);
159 method->apply(renderer);
161 setup_render(renderer, tag);
162 inst.setup_render(renderer, tag);
163 mesh->draw(renderer);
164 inst.finish_render(renderer, tag);
165 finish_render(renderer, tag);
168 const RenderMethod *Object::get_method(Tag tag, unsigned lod) const
170 const Technique *tech = lods[lod].technique;
172 throw logic_error("no technique");
173 return tech->find_method(tag);
176 void Object::resource_loaded(Resource &res)
178 if(&res==lods.front().mesh && bounding_sphere.is_empty())
179 update_bounding_sphere();
182 void Object::resource_removed(Resource &res)
184 if(&res==lods.front().mesh)
185 lod0_watched = false;
189 Object::Loader::Loader(Object &o, Collection &c):
192 add("bounding_sphere_hint", &Loader::bounding_sphere_hint);
193 add("level_of_detail", &Loader::level_of_detail);
196 void Object::Loader::finish()
198 obj.update_bounding_sphere();
201 void Object::Loader::bounding_sphere_hint(float x, float y, float z, float r)
203 obj.bounding_sphere = Geometry::BoundingSphere<float, 3>(Vector3(x, y, z), r);
206 void Object::Loader::level_of_detail(unsigned i)
208 LodLoader ldr(obj, i, get_collection());
213 Object::LodLoader::LodLoader(Object &o, unsigned i, Collection &c):
214 DataFile::CollectionObjectLoader<Object>(o, &c),
216 lod(obj.get_lod(index, "Object::LodLoader::LodLoader"))
218 add("mesh", &LodLoader::mesh_inline);
219 add("mesh", &LodLoader::mesh);
220 add("technique", &LodLoader::technique_inline);
221 add("technique", &LodLoader::technique);
224 void Object::LodLoader::mesh(const string &n)
226 obj.set_mesh(index, &get_collection().get<Mesh>(n));
229 void Object::LodLoader::mesh_inline()
231 RefPtr<Mesh> msh = new Mesh;
233 get_collection().add(format("%s/lod%d.mesh", FS::basename(get_source()), index), msh.get());
234 lod.mesh = msh.release();
237 void Object::LodLoader::technique(const string &n)
239 obj.set_technique(index, &get_collection().get<Technique>(n));
242 void Object::LodLoader::technique_inline()
244 RefPtr<Technique> tech = new Technique;
245 Technique::Loader ldr(*tech, get_collection());
246 string name = format("%s/lod%d.tech", FS::basename(get_source()), index);
247 ldr.set_inline_base_name(name);
248 load_sub(*tech, get_collection());
249 get_collection().add(name, tech.get());
250 lod.technique = tech.release();