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"
24 Object::Object(const Mesh *m, const Technique *t)
33 if(ResourceManager *rm = lods[0].mesh->get_manager())
34 rm->unwatch_resource(*lods[0].mesh, *this);
37 Object::LevelOfDetail &Object::get_lod(unsigned i, const char *caller)
40 throw out_of_range(caller);
41 if(i>0 && (!lods[0].mesh || !lods[0].technique))
42 throw invalid_operation(caller);
45 lods.push_back(lods.back());
50 void Object::set_mesh(unsigned i, const Mesh *m)
52 RefPtr<const Mesh> &ptr = get_lod(i, "Object::set_mesh").mesh;
54 if(ResourceManager *rm = ptr->get_manager())
55 rm->unwatch_resource(*ptr, *this);
60 if(ResourceManager *rm = m->get_manager())
61 rm->watch_resource(*m, *this);
63 update_bounding_sphere();
66 void Object::update_bounding_sphere()
68 vector<Vector3> points;
69 for(vector<LevelOfDetail>::const_iterator i=lods.begin(); i!=lods.end(); ++i)
71 if(!i->mesh || !i->mesh->is_loaded())
74 const VertexArray &vertices = i->mesh->get_vertices();
76 int offset = vertices.get_format().offset(VERTEX3);
80 offset = vertices.get_format().offset(VERTEX2);
86 unsigned n_vertices = vertices.size();
87 points.reserve(points.size()+n_vertices);
88 for(unsigned j=0; j<n_vertices; ++j)
90 const float *v = vertices[j];
91 points.push_back(Vector3(v[offset], v[offset+1], (three ? v[offset+2] : 0.0f)));
95 /* Don't touch the bounding sphere if we had no vertices to avoid
96 overwriting a possible hint. */
100 bounding_sphere = Geometry::BoundingSphere<float, 3>::from_point_cloud(points.begin(), points.end());
103 const Mesh *Object::get_mesh(unsigned i) const
108 return lods[i].mesh.get();
111 void Object::set_technique(unsigned i, const Technique *t)
113 RefPtr<const Technique> &ptr = get_lod(i, "Object::set_technique").technique;
118 const Technique *Object::get_technique(unsigned i) const
123 return lods[i].technique.get();
126 void Object::render(const Tag &tag) const
128 const RenderPass *pass = get_pass(tag, 0);
132 Bind bind_shader(pass->get_shader_program());
133 if(pass->get_shader_data())
134 pass->get_shader_data()->apply();
135 Bind bind_material(pass->get_material());
136 Bind bind_texturing(pass->get_texturing());
138 lods.front().mesh->draw();
141 void Object::render(Renderer &renderer, const Tag &tag) const
143 const RenderPass *pass = get_pass(tag, 0);
147 Renderer::Push push(renderer);
148 pass->apply(renderer);
150 setup_render(renderer, tag);
151 lods.front().mesh->draw(renderer);
152 finish_render(renderer, tag);
155 void Object::render(Renderer &renderer, const ObjectInstance &inst, const Tag &tag) const
157 unsigned lod = min<unsigned>(inst.get_level_of_detail(renderer), lods.size()-1);
158 const RenderPass *pass = get_pass(tag, lod);
162 Renderer::Push push(renderer);
163 pass->apply(renderer);
165 setup_render(renderer, tag);
166 inst.setup_render(renderer, tag);
167 lods[lod].mesh->draw(renderer);
168 inst.finish_render(renderer, tag);
169 finish_render(renderer, tag);
172 const RenderPass *Object::get_pass(const Tag &tag, unsigned lod) const
174 const Technique *tech = lods[lod].technique.get();
176 throw logic_error("no technique");
177 if(!tech->has_pass(tag))
179 return &tech->get_pass(tag);
182 void Object::resource_loaded(Resource &res)
184 if(&res==lods.front().mesh.get() && bounding_sphere.is_empty())
185 update_bounding_sphere();
189 Object::Loader::Loader(Object &o):
195 Object::Loader::Loader(Object &o, Collection &c):
201 void Object::Loader::init()
203 add("bounding_sphere_hint", &Loader::bounding_sphere_hint);
204 add("level_of_detail", &Loader::level_of_detail);
207 void Object::Loader::finish()
209 obj.update_bounding_sphere();
212 void Object::Loader::bounding_sphere_hint(float x, float y, float z, float r)
214 obj.bounding_sphere = Geometry::BoundingSphere<float, 3>(Vector3(x, y, z), r);
217 void Object::Loader::level_of_detail(unsigned i)
219 LodLoader ldr(obj, i, coll);
224 Object::LodLoader::LodLoader(Object &o, unsigned i, Collection *c):
225 DataFile::CollectionObjectLoader<Object>(o, c),
227 lod(obj.get_lod(index, "Object::LodLoader::LodLoader"))
229 add("mesh", &LodLoader::mesh_inline);
230 add("mesh", &LodLoader::mesh);
231 add("technique", &LodLoader::technique_inline);
232 add("technique", &LodLoader::technique);
235 void Object::LodLoader::mesh(const string &n)
237 obj.set_mesh(index, &get_collection().get<Mesh>(n));
240 void Object::LodLoader::mesh_inline()
242 RefPtr<Mesh> msh = new Mesh;
247 void Object::LodLoader::technique(const std::string &n)
249 obj.set_technique(index, &get_collection().get<Technique>(n));
252 void Object::LodLoader::technique_inline()
254 RefPtr<Technique> tech = new Technique;
256 load_sub(*tech, get_collection());
259 lod.technique = tech;