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):
34 if(ResourceManager *rm = lods[0].mesh->get_manager())
35 rm->unwatch_resource(*lods[0].mesh, *this);
38 Object::LevelOfDetail &Object::get_lod(unsigned i, const char *caller)
41 throw out_of_range(caller);
42 if(i>0 && (!lods[0].mesh || !lods[0].technique))
43 throw invalid_operation(caller);
46 lods.push_back(lods.back());
51 void Object::set_mesh(unsigned i, const Mesh *m)
53 RefPtr<const Mesh> &ptr = get_lod(i, "Object::set_mesh").mesh;
55 if(ResourceManager *rm = ptr->get_manager())
56 rm->unwatch_resource(*ptr, *this);
61 if(ResourceManager *rm = m->get_manager())
62 rm->watch_resource(*m, *this);
64 update_bounding_sphere();
67 void Object::update_bounding_sphere()
69 vector<Vector3> points;
70 for(vector<LevelOfDetail>::const_iterator i=lods.begin(); i!=lods.end(); ++i)
72 if(!i->mesh || !i->mesh->is_loaded())
75 const VertexArray &vertices = i->mesh->get_vertices();
77 int offset = vertices.get_format().offset(VERTEX3);
81 offset = vertices.get_format().offset(VERTEX2);
87 unsigned n_vertices = vertices.size();
88 points.reserve(points.size()+n_vertices);
89 for(unsigned j=0; j<n_vertices; ++j)
91 const float *v = vertices[j];
92 points.push_back(Vector3(v[offset], v[offset+1], (three ? v[offset+2] : 0.0f)));
96 /* Don't touch the bounding sphere if we had no vertices to avoid
97 overwriting a possible hint. */
101 bounding_sphere = Geometry::BoundingSphere<float, 3>::from_point_cloud(points.begin(), points.end());
104 const Mesh *Object::get_mesh(unsigned i) const
109 return lods[i].mesh.get();
112 void Object::set_technique(unsigned i, const Technique *t)
114 RefPtr<const Technique> &ptr = get_lod(i, "Object::set_technique").technique;
119 const Technique *Object::get_technique(unsigned i) const
124 return lods[i].technique.get();
127 void Object::render(const Tag &tag) const
129 const RenderPass *pass = get_pass(tag, 0);
133 Bind bind_shader(pass->get_shader_program());
134 if(pass->get_shader_data())
135 pass->get_shader_data()->apply();
136 Bind bind_material(pass->get_material());
137 Bind bind_texturing(pass->get_texturing());
139 lods.front().mesh->draw();
142 void Object::render(Renderer &renderer, const Tag &tag) const
144 const RenderPass *pass = get_pass(tag, 0);
148 Renderer::Push push(renderer);
149 pass->apply(renderer);
151 setup_render(renderer, tag);
152 lods.front().mesh->draw(renderer);
153 finish_render(renderer, tag);
156 void Object::render(Renderer &renderer, const ObjectInstance &inst, const Tag &tag) const
158 unsigned lod = min<unsigned>(inst.get_level_of_detail(renderer), lods.size()-1);
159 const RenderPass *pass = get_pass(tag, lod);
163 Renderer::Push push(renderer);
164 pass->apply(renderer);
166 setup_render(renderer, tag);
167 inst.setup_render(renderer, tag);
168 lods[lod].mesh->draw(renderer);
169 inst.finish_render(renderer, tag);
170 finish_render(renderer, tag);
173 const RenderPass *Object::get_pass(const Tag &tag, unsigned lod) const
175 const Technique *tech = lods[lod].technique.get();
177 throw logic_error("no technique");
178 if(!tech->has_pass(tag))
180 return &tech->get_pass(tag);
183 void Object::resource_loaded(Resource &res)
185 if(&res==lods.front().mesh.get() && bounding_sphere.is_empty())
186 update_bounding_sphere();
190 Object::Loader::Loader(Object &o):
196 Object::Loader::Loader(Object &o, Collection &c):
202 void Object::Loader::init()
204 add("bounding_sphere_hint", &Loader::bounding_sphere_hint);
205 add("level_of_detail", &Loader::level_of_detail);
208 void Object::Loader::finish()
210 obj.update_bounding_sphere();
213 void Object::Loader::bounding_sphere_hint(float x, float y, float z, float r)
215 obj.bounding_sphere = Geometry::BoundingSphere<float, 3>(Vector3(x, y, z), r);
218 void Object::Loader::level_of_detail(unsigned i)
220 LodLoader ldr(obj, i, coll);
225 Object::LodLoader::LodLoader(Object &o, unsigned i, Collection *c):
226 DataFile::CollectionObjectLoader<Object>(o, c),
228 lod(obj.get_lod(index, "Object::LodLoader::LodLoader"))
230 add("mesh", &LodLoader::mesh_inline);
231 add("mesh", &LodLoader::mesh);
232 add("technique", &LodLoader::technique_inline);
233 add("technique", &LodLoader::technique);
236 void Object::LodLoader::mesh(const string &n)
238 obj.set_mesh(index, &get_collection().get<Mesh>(n));
241 void Object::LodLoader::mesh_inline()
243 RefPtr<Mesh> msh = new Mesh;
248 void Object::LodLoader::technique(const std::string &n)
250 obj.set_technique(index, &get_collection().get<Technique>(n));
253 void Object::LodLoader::technique_inline()
255 RefPtr<Technique> tech = new Technique;
257 load_sub(*tech, get_collection());
260 lod.technique = tech;