1 #include <msp/datafile/collection.h>
2 #include <msp/strings/format.h>
7 #include "objectinstance.h"
9 #include "programdata.h"
11 #include "resourcemanager.h"
12 #include "technique.h"
13 #include "texturing.h"
20 Matrix Object::identity_matrix;
27 Object::Object(const Mesh *m, const Technique *t):
35 // TODO should have copy-c'tor to set watch on lod0 mesh if necessary
39 if(lods[0].mesh && lod0_watched)
40 if(ResourceManager *rm = lods[0].mesh->get_manager())
41 rm->unwatch_resource(*lods[0].mesh, *this);
44 Object::LevelOfDetail &Object::get_lod(unsigned i, const char *caller)
47 throw out_of_range(caller);
48 if(i>0 && (!lods[0].mesh || !lods[0].technique))
49 throw invalid_operation(caller);
52 lods.push_back(lods.back());
57 void Object::set_mesh(unsigned i, const Mesh *m)
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->unwatch_resource(*ptr, *this);
68 if(ResourceManager *rm = m->get_manager())
70 rm->watch_resource(*m, *this);
74 update_bounding_sphere();
77 void Object::update_bounding_sphere()
79 vector<Vector3> points;
80 for(vector<LevelOfDetail>::const_iterator i=lods.begin(); i!=lods.end(); ++i)
82 if(!i->mesh || !i->mesh->is_loaded())
85 const VertexArray &vertices = i->mesh->get_vertices();
87 int offset = vertices.get_format().offset(VERTEX3);
91 offset = vertices.get_format().offset(VERTEX2);
97 unsigned n_vertices = vertices.size();
98 points.reserve(points.size()+n_vertices);
99 for(unsigned j=0; j<n_vertices; ++j)
101 const float *v = vertices[j];
102 points.push_back(Vector3(v[offset], v[offset+1], (three ? v[offset+2] : 0.0f)));
106 /* Don't touch the bounding sphere if we had no vertices to avoid
107 overwriting a possible hint. */
111 bounding_sphere = Geometry::BoundingSphere<float, 3>::from_point_cloud(points.begin(), points.end());
114 const Mesh *Object::get_mesh(unsigned i) const
119 return lods[i].mesh.get();
122 void Object::set_technique(unsigned i, const Technique *t)
124 RefPtr<const Technique> &ptr = get_lod(i, "Object::set_technique").technique;
129 const Technique *Object::get_technique(unsigned i) const
134 return lods[i].technique.get();
137 void Object::render(Renderer &renderer, const Tag &tag) const
139 const RenderPass *pass = get_pass(tag, 0);
143 Renderer::Push push(renderer);
144 pass->apply(renderer);
146 setup_render(renderer, tag);
147 lods.front().mesh->draw(renderer);
148 finish_render(renderer, tag);
151 void Object::render(Renderer &renderer, const ObjectInstance &inst, const Tag &tag) const
153 unsigned lod = min<unsigned>(inst.get_level_of_detail(renderer), lods.size()-1);
154 const RenderPass *pass = get_pass(tag, lod);
158 Renderer::Push push(renderer);
159 pass->apply(renderer);
161 setup_render(renderer, tag);
162 inst.setup_render(renderer, tag);
163 lods[lod].mesh->draw(renderer);
164 inst.finish_render(renderer, tag);
165 finish_render(renderer, tag);
168 const RenderPass *Object::get_pass(const Tag &tag, unsigned lod) const
170 const Technique *tech = lods[lod].technique.get();
172 throw logic_error("no technique");
173 if(!tech->has_pass(tag))
175 return &tech->get_pass(tag);
178 void Object::resource_loaded(Resource &res)
180 if(&res==lods.front().mesh.get() && bounding_sphere.is_empty())
181 update_bounding_sphere();
184 void Object::resource_removed(Resource &res)
186 if(&res==lods.front().mesh.get())
187 lod0_watched = false;
191 Object::Loader::Loader(Object &o):
197 Object::Loader::Loader(Object &o, Collection &c):
203 void Object::Loader::init()
205 add("bounding_sphere_hint", &Loader::bounding_sphere_hint);
206 add("level_of_detail", &Loader::level_of_detail);
209 void Object::Loader::finish()
211 obj.update_bounding_sphere();
214 void Object::Loader::bounding_sphere_hint(float x, float y, float z, float r)
216 obj.bounding_sphere = Geometry::BoundingSphere<float, 3>(Vector3(x, y, z), r);
219 void Object::Loader::level_of_detail(unsigned i)
221 LodLoader ldr(obj, i, coll);
226 Object::LodLoader::LodLoader(Object &o, unsigned i, Collection *c):
227 DataFile::CollectionObjectLoader<Object>(o, c),
229 lod(obj.get_lod(index, "Object::LodLoader::LodLoader"))
231 add("mesh", &LodLoader::mesh_inline);
232 add("mesh", &LodLoader::mesh);
233 add("technique", &LodLoader::technique_inline);
234 add("technique", &LodLoader::technique);
237 void Object::LodLoader::mesh(const string &n)
239 obj.set_mesh(index, &get_collection().get<Mesh>(n));
242 void Object::LodLoader::mesh_inline()
244 RefPtr<Mesh> msh = new Mesh;
249 void Object::LodLoader::technique(const std::string &n)
251 obj.set_technique(index, &get_collection().get<Technique>(n));
254 void Object::LodLoader::technique_inline()
256 RefPtr<Technique> tech = new Technique;
258 load_sub(*tech, get_collection());
261 lod.technique = tech;