]> git.tdb.fi Git - libs/gl.git/blob - source/object.cpp
Allow bounding sphere hints for objects and export such from Blender
[libs/gl.git] / source / object.cpp
1 #include <msp/datafile/collection.h>
2 #include <msp/strings/format.h>
3 #include "error.h"
4 #include "material.h"
5 #include "mesh.h"
6 #include "object.h"
7 #include "objectinstance.h"
8 #include "program.h"
9 #include "programdata.h"
10 #include "renderer.h"
11 #include "resourcemanager.h"
12 #include "technique.h"
13 #include "texturing.h"
14
15 using namespace std;
16
17 namespace Msp {
18 namespace GL {
19
20 Object::Object():
21         lods(1)
22 { }
23
24 Object::Object(const Mesh *m, const Technique *t)
25 {
26         set_mesh(m);
27         set_technique(t);
28 }
29
30 Object::~Object()
31 {
32         if(lods[0].mesh)
33                 if(ResourceManager *rm = lods[0].mesh->get_manager())
34                         rm->unwatch_resource(*lods[0].mesh, *this);
35 }
36
37 Object::LevelOfDetail &Object::get_lod(unsigned i, const char *caller)
38 {
39         if(i>lods.size())
40                 throw out_of_range(caller);
41         if(i>0 && (!lods[0].mesh || !lods[0].technique))
42                 throw invalid_operation(caller);
43
44         if(i==lods.size())
45                 lods.push_back(lods.back());
46
47         return lods[i];
48 }
49
50 void Object::set_mesh(unsigned i, const Mesh *m)
51 {
52         RefPtr<const Mesh> &ptr = get_lod(i, "Object::set_mesh").mesh;
53         if(i==0 && ptr)
54                 if(ResourceManager *rm = ptr->get_manager())
55                         rm->unwatch_resource(*ptr, *this);
56         ptr = m;
57         ptr.keep();
58
59         if(i==0 && m)
60                 if(ResourceManager *rm = m->get_manager())
61                         rm->watch_resource(*m, *this);
62
63         update_bounding_sphere();
64 }
65
66 void Object::update_bounding_sphere()
67 {
68         vector<Vector3> points;
69         for(vector<LevelOfDetail>::const_iterator i=lods.begin(); i!=lods.end(); ++i)
70         {
71                 if(!i->mesh || !i->mesh->is_loaded())
72                         continue;
73
74                 const VertexArray &vertices = i->mesh->get_vertices();
75
76                 int offset = vertices.get_format().offset(VERTEX3);
77                 bool three = true;
78                 if(offset<0)
79                 {
80                         offset = vertices.get_format().offset(VERTEX2);
81                         three = false;
82                         if(offset<0)
83                                 continue;
84                 }
85
86                 unsigned n_vertices = vertices.size();
87                 points.reserve(points.size()+n_vertices);
88                 for(unsigned j=0; j<n_vertices; ++j)
89                 {
90                         const float *v = vertices[j];
91                         points.push_back(Vector3(v[offset], v[offset+1], (three ? v[offset+2] : 0.0f)));
92                 }
93         }
94
95         /* Don't touch the bounding sphere if we had no vertices to avoid
96         overwriting a possible hint. */
97         if(points.empty())
98                 return;
99
100         bounding_sphere = Geometry::BoundingSphere<float, 3>::from_point_cloud(points.begin(), points.end());
101 }
102
103 const Mesh *Object::get_mesh(unsigned i) const
104 {
105         if(i>=lods.size())
106                 return 0;
107
108         return lods[i].mesh.get();
109 }
110
111 void Object::set_technique(unsigned i, const Technique *t)
112 {
113         RefPtr<const Technique> &ptr = get_lod(i, "Object::set_technique").technique;
114         ptr = t;
115         ptr.keep();
116 }
117
118 const Technique *Object::get_technique(unsigned i) const
119 {
120         if(i>=lods.size())
121                 return 0;
122
123         return lods[i].technique.get();
124 }
125
126 void Object::render(const Tag &tag) const
127 {
128         const RenderPass *pass = get_pass(tag, 0);
129         if(!pass)
130                 return;
131
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());
137
138         lods.front().mesh->draw();
139 }
140
141 void Object::render(Renderer &renderer, const Tag &tag) const
142 {
143         const RenderPass *pass = get_pass(tag, 0);
144         if(!pass)
145                 return;
146
147         Renderer::Push push(renderer);
148         pass->apply(renderer);
149
150         setup_render(renderer, tag);
151         lods.front().mesh->draw(renderer);
152         finish_render(renderer, tag);
153 }
154
155 void Object::render(Renderer &renderer, const ObjectInstance &inst, const Tag &tag) const
156 {
157         unsigned lod = min<unsigned>(inst.get_level_of_detail(renderer), lods.size()-1);
158         const RenderPass *pass = get_pass(tag, lod);
159         if(!pass)
160                 return;
161
162         Renderer::Push push(renderer);
163         pass->apply(renderer);
164
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);
170 }
171
172 const RenderPass *Object::get_pass(const Tag &tag, unsigned lod) const
173 {
174         const Technique *tech = lods[lod].technique.get();
175         if(!tech)
176                 throw logic_error("no technique");
177         if(!tech->has_pass(tag))
178                 return 0;
179         return &tech->get_pass(tag);
180 }
181
182 void Object::resource_loaded(Resource &res)
183 {
184         if(&res==lods.front().mesh.get() && bounding_sphere.is_empty())
185                 update_bounding_sphere();
186 }
187
188
189 Object::Loader::Loader(Object &o):
190         LodLoader(o, 0, 0)
191 {
192         init();
193 }
194
195 Object::Loader::Loader(Object &o, Collection &c):
196         LodLoader(o, 0, &c)
197 {
198         init();
199 }
200
201 void Object::Loader::init()
202 {
203         add("bounding_sphere_hint", &Loader::bounding_sphere_hint);
204         add("level_of_detail", &Loader::level_of_detail);
205 }
206
207 void Object::Loader::finish()
208 {
209         obj.update_bounding_sphere();
210 }
211
212 void Object::Loader::bounding_sphere_hint(float x, float y, float z, float r)
213 {
214         obj.bounding_sphere = Geometry::BoundingSphere<float, 3>(Vector3(x, y, z), r);
215 }
216
217 void Object::Loader::level_of_detail(unsigned i)
218 {
219         LodLoader ldr(obj, i, coll);
220         load_sub_with(ldr);
221 }
222
223
224 Object::LodLoader::LodLoader(Object &o, unsigned i, Collection *c):
225         DataFile::CollectionObjectLoader<Object>(o, c),
226         index(i),
227         lod(obj.get_lod(index, "Object::LodLoader::LodLoader"))
228 {
229         add("mesh",      &LodLoader::mesh_inline);
230         add("mesh",      &LodLoader::mesh);
231         add("technique", &LodLoader::technique_inline);
232         add("technique", &LodLoader::technique);
233 }
234
235 void Object::LodLoader::mesh(const string &n)
236 {
237         obj.set_mesh(index, &get_collection().get<Mesh>(n));
238 }
239
240 void Object::LodLoader::mesh_inline()
241 {
242         RefPtr<Mesh> msh = new Mesh;
243         load_sub(*msh);
244         lod.mesh = msh;
245 }
246
247 void Object::LodLoader::technique(const std::string &n)
248 {
249         obj.set_technique(index, &get_collection().get<Technique>(n));
250 }
251
252 void Object::LodLoader::technique_inline()
253 {
254         RefPtr<Technique> tech = new Technique;
255         if(coll)
256                 load_sub(*tech, get_collection());
257         else
258                 load_sub(*tech);
259         lod.technique = tech;
260 }
261
262 } // namespace GL
263 } // namespace Msp