]> git.tdb.fi Git - libs/gl.git/blob - source/object.cpp
Add class Renderable
[libs/gl.git] / source / object.cpp
1 /* $Id$
2
3 This file is part of libmspgl
4 Copyright © 2007  Mikko Rasa, Mikkosoft Productions
5 Distributed under the LGPL
6 */
7
8 #include <msp/strings/formatter.h>
9 #include "except.h"
10 #include "material.h"
11 #include "mesh.h"
12 #include "object.h"
13 #include "objectinstance.h"
14 #include "objectpass.h"
15 #include "program.h"
16 #include "programdata.h"
17 #include "texture.h"
18 #include "texunit.h"
19
20 using namespace std;
21
22 namespace Msp {
23 namespace GL {
24
25 Object::Object():
26         meshes(1, static_cast<Mesh *>(0)),
27         material(0)
28 {
29         normal_pass=&passes[""];
30 }
31
32 Object::~Object()
33 {
34         for(map<string, ObjectPass>::iterator i=passes.begin(); i!=passes.end(); ++i)
35                 delete i->second.shdata;
36 }
37
38 bool Object::has_pass(const string &pn) const
39 {
40         return passes.count(pn);
41 }
42
43 const ObjectPass &Object::get_pass(const string &pn) const
44 {
45         map<string, ObjectPass>::const_iterator i=passes.find(pn);
46         if(i==passes.end())
47                 throw KeyError("Unknown pass");
48         return i->second;
49 }
50
51 void Object::render() const
52 {
53         render(*normal_pass, 0);
54 }
55
56 void Object::render(const ObjectInstance &inst) const
57 {
58         render(*normal_pass, &inst);
59 }
60
61 void Object::render(const string &pn) const
62 {
63         render(get_pass(pn), 0);
64 }
65
66 void Object::render(const string &pn, const ObjectInstance &inst) const
67 {
68         render(get_pass(pn), &inst);
69 }
70
71 void Object::render(const list<const ObjectInstance *> &insts) const
72 {
73         render(*normal_pass, insts);
74 }
75
76 void Object::render(const string &pn, const list<const ObjectInstance *> &insts) const
77 {
78         render(get_pass(pn), insts);
79 }
80
81 void Object::setup_render(const ObjectPass &pass) const
82 {
83         if(!meshes[0])
84                 throw InvalidState("Trying to render Object without mesh");
85
86         if(pass.shprog)
87         {
88                 pass.shprog->bind();
89                 pass.shdata->apply();
90                 for(unsigned i=0; i<textures.size(); ++i)
91                 {
92                         TexUnit::activate(i);
93                         textures[i]->bind();
94                 }
95         }
96         else if(!textures.empty())
97                 textures.front()->bind();
98
99         if(material)
100                 material->apply();
101 }
102
103 void Object::finish_render(const ObjectPass &pass) const
104 {
105         if(pass.shprog)
106                 Program::unbind();
107         for(unsigned i=textures.size(); i--;)
108         {
109                 TexUnit::activate(i);
110                 Texture::unbind();
111         }
112 }
113
114 void Object::render(const ObjectPass &pass, const ObjectInstance *inst) const
115 {
116         setup_render(pass);
117
118         unsigned lod=0;
119         if(inst)
120         {
121                 inst->setup_render(pass);
122                 lod=min(inst->get_level_of_detail(), meshes.size()-1);
123         }
124
125         meshes[lod]->draw();
126
127         if(inst)
128                 inst->finish_render(pass);
129
130         finish_render(pass);
131 }
132
133 void Object::render(const ObjectPass &pass, const list<const ObjectInstance *> &insts) const
134 {
135         setup_render(pass);
136
137         for(list<const ObjectInstance *>::const_iterator i=insts.begin(); i!=insts.end(); ++i)
138         {
139                 (*i)->setup_render(pass);
140
141                 unsigned lod=min((*i)->get_level_of_detail(), meshes.size()-1);
142                 meshes[lod]->draw();
143
144                 (*i)->finish_render(pass);
145         }
146
147         finish_render(pass);
148 }
149
150
151 Object::Loader::Loader(Object &o, Collection &c):
152         obj(o),
153         coll(c)
154 {
155         add("lod_mesh", &Loader::lod_mesh);
156         add("material", &Object::material);
157         add("material_inline", &Loader::material_inline);
158         add("mesh",     &Loader::mesh);
159         add("pass",     &Loader::pass);
160         add("shader",   &Loader::shader);
161         add("texture",  &Loader::texture);
162 }
163
164 Object::Loader::~Loader()
165 {
166         for(map<string, ObjectPass>::iterator i=obj.passes.begin(); i!=obj.passes.end(); ++i)
167                 if(i->second.shdata)
168                 {
169                         for(unsigned j=0; j<textures.size(); ++j)
170                                 i->second.shdata->uniform(i->second.shprog->get_uniform_location(textures[j]), static_cast<int>(j));
171                 }
172 }
173
174 void Object::Loader::lod_mesh(unsigned l, const string &n)
175 {
176         obj.meshes.resize(l+1, 0);
177         obj.meshes[l]=coll.get<Mesh>(n);
178 }
179
180 void Object::Loader::material_inline()
181 {
182         throw Exception("material_inline not supported yet");
183         /*RefPtr<Material> mat=new Material;
184         load_sub(*mat);
185         coll.add(format("%p%p", &obj, mat.get()), mat.get());
186         obj.material=mat.release();*/
187 }
188
189 void Object::Loader::mesh(const string &n)
190 {
191         obj.meshes[0]=coll.get<Mesh>(n);
192 }
193
194 void Object::Loader::pass(const string &n)
195 {
196         if(obj.passes.count(n))
197                 throw KeyError("Duplicate pass name");
198         ObjectPass p;
199         load_sub(p, coll);
200         obj.passes[n]=p;
201 }
202
203 void Object::Loader::shader(const string &n)
204 {
205         Program *shprog=coll.get<Program>(n);
206         if(shprog)  // Allow for unsupported shaders
207         {
208                 RefPtr<ProgramData> shdata=new ProgramData;
209                 load_sub(*shdata, *shprog);
210
211                 obj.normal_pass->shprog=shprog;
212                 if(obj.normal_pass->shdata)
213                         delete obj.normal_pass->shdata;
214                 obj.normal_pass->shdata=shdata.release();
215         }
216 }
217
218 void Object::Loader::texture(const string &n)
219 {
220         unsigned eqsign=n.find('=');
221         if(eqsign!=string::npos)
222         {
223                 obj.textures.push_back(coll.get<Texture>(n.substr(eqsign+1)));
224                 textures.push_back(n.substr(0, eqsign));
225         }
226         else
227         {
228                 obj.textures.push_back(coll.get<Texture>(n));
229                 textures.push_back(n);
230         }
231 }
232
233 } // namespace GL
234 } // namespace Msp