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