]> git.tdb.fi Git - libs/gl.git/blob - source/object.cpp
Fix extension functions on Windows
[libs/gl.git] / source / object.cpp
1 #include <msp/datafile/collection.h>
2 #include <msp/strings/format.h>
3 #include "material.h"
4 #include "mesh.h"
5 #include "object.h"
6 #include "objectinstance.h"
7 #include "program.h"
8 #include "programdata.h"
9 #include "renderer.h"
10 #include "resourcemanager.h"
11 #include "technique.h"
12 #include "texturing.h"
13
14 using namespace std;
15
16 namespace Msp {
17 namespace GL {
18
19 Object::Object():
20         meshes(1)
21 { }
22
23 Object::Object(const Mesh *m, const Technique *t)
24 {
25         set_mesh(m);
26         set_technique(t);
27 }
28
29 // Avoid synthesizing ~RefPtr in files including object.h
30 Object::~Object()
31 {
32         if(meshes[0])
33                 if(ResourceManager *rm = meshes[0]->get_manager())
34                         rm->unwatch_resource(*meshes[0], *this);
35 }
36
37 void Object::set_mesh(unsigned i, const Mesh *m)
38 {
39         if(i>meshes.size())
40                 throw out_of_range("Object::set_mesh");
41
42         if(i==meshes.size())
43                 meshes.push_back(m);
44         else
45         {
46                 if(i==0 && meshes[i])
47                         if(ResourceManager *rm = meshes[i]->get_manager())
48                                 rm->unwatch_resource(*meshes[i], *this);
49                 meshes[i] = m;
50         }
51         meshes[i].keep();
52
53         if(i==0 && m)
54                 if(ResourceManager *rm = m->get_manager())
55                         rm->watch_resource(*m, *this);
56
57         update_bounding_sphere();
58 }
59
60 void Object::update_bounding_sphere()
61 {
62         vector<Vector3> points;
63         for(vector<RefPtr<const Mesh> >::const_iterator i=meshes.begin(); i!=meshes.end(); ++i)
64         {
65                 if(!*i)
66                         continue;
67
68                 const VertexArray &vertices = (*i)->get_vertices();
69
70                 int offset = vertices.get_format().offset(VERTEX3);
71                 bool three = true;
72                 if(offset<0)
73                 {
74                         offset = vertices.get_format().offset(VERTEX2);
75                         three = false;
76                         if(offset<0)
77                                 continue;
78                 }
79
80                 unsigned n_vertices = vertices.size();
81                 points.reserve(points.size()+n_vertices);
82                 for(unsigned j=0; j<n_vertices; ++j)
83                 {
84                         const float *v = vertices[j];
85                         points.push_back(Vector3(v[offset], v[offset+1], (three ? v[offset+2] : 0.0f)));
86                 }
87         }
88
89         bounding_sphere = Geometry::BoundingSphere<float, 3>::from_point_cloud(points.begin(), points.end());
90 }
91
92 const Mesh *Object::get_mesh(unsigned i) const
93 {
94         if(i>=meshes.size())
95                 return 0;
96
97         return meshes[i].get();
98 }
99
100 void Object::set_technique(const Technique *t)
101 {
102         technique = t;
103         technique.keep();
104 }
105
106 void Object::render(const Tag &tag) const
107 {
108         const RenderPass *pass = get_pass(tag);
109         if(!pass)
110                 return;
111
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());
117
118         meshes.front()->draw();
119 }
120
121 void Object::render(Renderer &renderer, const Tag &tag) const
122 {
123         const RenderPass *pass = get_pass(tag);
124         if(!pass)
125                 return;
126
127         Renderer::Push push(renderer);
128         pass->apply(renderer);
129
130         setup_render(renderer, tag);
131         meshes.front()->draw(renderer);
132         finish_render(renderer, tag);
133 }
134
135 void Object::render(Renderer &renderer, const ObjectInstance &inst, const Tag &tag) const
136 {
137         const RenderPass *pass = get_pass(tag);
138         if(!pass)
139                 return;
140
141         Renderer::Push push(renderer);
142         pass->apply(renderer);
143
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);
150 }
151
152 const RenderPass *Object::get_pass(const Tag &tag) const
153 {
154         if(!technique)
155                 throw logic_error("!technique");
156         if(!technique->has_pass(tag))
157                 return 0;
158         return &technique->get_pass(tag);
159 }
160
161 void Object::resource_loaded(Resource &res)
162 {
163         if(!meshes.empty() && &res==meshes.front().get() && bounding_sphere.is_empty())
164                 update_bounding_sphere();
165 }
166
167
168 Object::Loader::Loader(Object &o):
169         DataFile::CollectionObjectLoader<Object>(o, 0)
170 {
171         init();
172 }
173
174 Object::Loader::Loader(Object &o, Collection &c):
175         DataFile::CollectionObjectLoader<Object>(o, &c)
176 {
177         init();
178 }
179
180 void Object::Loader::init()
181 {
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);
188 }
189
190 void Object::Loader::finish()
191 {
192         obj.update_bounding_sphere();
193 }
194
195 void Object::Loader::mesh_inline()
196 {
197         RefPtr<Mesh> msh = new Mesh;
198         load_sub(*msh);
199         obj.meshes.front() = msh;
200 }
201
202 void Object::Loader::mesh_inline_lod(unsigned l)
203 {
204         if(l>obj.meshes.size())
205                 throw out_of_range("Object::Loader::mesh_inline_lod");
206
207         RefPtr<Mesh> msh = new Mesh;
208         load_sub(*msh);
209         if(l==obj.meshes.size())
210                 obj.meshes.push_back(msh);
211         else
212                 obj.meshes[l] = msh;
213 }
214
215 void Object::Loader::mesh(const std::string &n)
216 {
217         obj.set_mesh(&get_collection().get<Mesh>(n));
218 }
219
220 void Object::Loader::mesh_lod(unsigned l, const string &n)
221 {
222         obj.set_mesh(l, &get_collection().get<Mesh>(n));
223 }
224
225 void Object::Loader::technique_inline()
226 {
227         RefPtr<Technique> tech = new Technique;
228         if(coll)
229                 load_sub(*tech, get_collection());
230         else
231                 load_sub(*tech);
232         obj.technique = tech;
233 }
234
235 void Object::Loader::technique(const std::string &n)
236 {
237         obj.set_technique(&get_collection().get<Technique>(n));
238 }
239
240 } // namespace GL
241 } // namespace Msp