1 #include <msp/datafile/collection.h>
2 #include <msp/strings/format.h>
6 #include "objectinstance.h"
8 #include "programdata.h"
10 #include "resourcemanager.h"
11 #include "technique.h"
12 #include "texturing.h"
23 Object::Object(const Mesh *m, const Technique *t)
29 // Avoid synthesizing ~RefPtr in files including object.h
33 if(ResourceManager *rm = meshes[0]->get_manager())
34 rm->unwatch_resource(*meshes[0], *this);
37 void Object::set_mesh(unsigned i, const Mesh *m)
40 throw out_of_range("Object::set_mesh");
47 if(ResourceManager *rm = meshes[i]->get_manager())
48 rm->unwatch_resource(*meshes[i], *this);
54 if(ResourceManager *rm = m->get_manager())
55 rm->watch_resource(*m, *this);
57 update_bounding_sphere();
60 void Object::update_bounding_sphere()
62 vector<Vector3> points;
63 for(vector<RefPtr<const Mesh> >::const_iterator i=meshes.begin(); i!=meshes.end(); ++i)
68 const VertexArray &vertices = (*i)->get_vertices();
70 int offset = vertices.get_format().offset(VERTEX3);
74 offset = vertices.get_format().offset(VERTEX2);
80 unsigned n_vertices = vertices.size();
81 points.reserve(points.size()+n_vertices);
82 for(unsigned j=0; j<n_vertices; ++j)
84 const float *v = vertices[j];
85 points.push_back(Vector3(v[offset], v[offset+1], (three ? v[offset+2] : 0.0f)));
89 bounding_sphere = Geometry::BoundingSphere<float, 3>::from_point_cloud(points.begin(), points.end());
92 const Mesh *Object::get_mesh(unsigned i) const
97 return meshes[i].get();
100 void Object::set_technique(const Technique *t)
106 void Object::render(const Tag &tag) const
108 const RenderPass *pass = get_pass(tag);
112 Bind bind_shader(pass->get_shader_program());
113 if(pass->get_shader_data())
114 pass->get_shader_data()->apply();
115 Bind bind_material(pass->get_material());
116 Bind bind_texturing(pass->get_texturing());
118 meshes.front()->draw();
121 void Object::render(Renderer &renderer, const Tag &tag) const
123 const RenderPass *pass = get_pass(tag);
127 Renderer::Push push(renderer);
128 pass->apply(renderer);
130 setup_render(renderer, tag);
131 meshes.front()->draw(renderer);
132 finish_render(renderer, tag);
135 void Object::render(Renderer &renderer, const ObjectInstance &inst, const Tag &tag) const
137 const RenderPass *pass = get_pass(tag);
141 Renderer::Push push(renderer);
142 pass->apply(renderer);
144 setup_render(renderer, tag);
145 inst.setup_render(renderer, tag);
146 unsigned lod = min<unsigned>(inst.get_level_of_detail(renderer), meshes.size()-1);
147 meshes[lod]->draw(renderer);
148 inst.finish_render(renderer, tag);
149 finish_render(renderer, tag);
152 const RenderPass *Object::get_pass(const Tag &tag) const
155 throw logic_error("!technique");
156 if(!technique->has_pass(tag))
158 return &technique->get_pass(tag);
161 void Object::resource_loaded(Resource &res)
163 if(!meshes.empty() && &res==meshes.front().get() && bounding_sphere.is_empty())
164 update_bounding_sphere();
168 Object::Loader::Loader(Object &o):
169 DataFile::CollectionObjectLoader<Object>(o, 0)
174 Object::Loader::Loader(Object &o, Collection &c):
175 DataFile::CollectionObjectLoader<Object>(o, &c)
180 void Object::Loader::init()
182 add("mesh", &Loader::mesh_inline);
183 add("mesh", &Loader::mesh_inline_lod);
184 add("mesh", &Loader::mesh);
185 add("mesh", &Loader::mesh_lod);
186 add("technique", &Loader::technique_inline);
187 add("technique", &Loader::technique);
190 void Object::Loader::finish()
192 obj.update_bounding_sphere();
195 void Object::Loader::mesh_inline()
197 RefPtr<Mesh> msh = new Mesh;
199 obj.meshes.front() = msh;
202 void Object::Loader::mesh_inline_lod(unsigned l)
204 if(l>obj.meshes.size())
205 throw out_of_range("Object::Loader::mesh_inline_lod");
207 RefPtr<Mesh> msh = new Mesh;
209 if(l==obj.meshes.size())
210 obj.meshes.push_back(msh);
215 void Object::Loader::mesh(const std::string &n)
217 obj.set_mesh(&get_collection().get<Mesh>(n));
220 void Object::Loader::mesh_lod(unsigned l, const string &n)
222 obj.set_mesh(l, &get_collection().get<Mesh>(n));
225 void Object::Loader::technique_inline()
227 RefPtr<Technique> tech = new Technique;
229 load_sub(*tech, get_collection());
232 obj.technique = tech;
235 void Object::Loader::technique(const std::string &n)
237 obj.set_technique(&get_collection().get<Technique>(n));