]> git.tdb.fi Git - libs/gl.git/blob - source/object.cpp
Add a Tag class to identify passes
[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[0];
30 }
31
32 Object::~Object()
33 {
34         for(map<unsigned, ObjectPass>::iterator i=passes.begin(); i!=passes.end(); ++i)
35                 delete i->second.shdata;
36 }
37
38 bool Object::has_pass(const Tag &tag) const
39 {
40         return passes.count(tag.id);
41 }
42
43 const ObjectPass &Object::get_pass(const Tag &tag) const
44 {
45         map<unsigned, ObjectPass>::const_iterator i=passes.find(tag.id);
46         if(i==passes.end())
47                 throw KeyError("Unknown pass");
48         return i->second;
49 }
50
51 void Object::render(const Tag &tag) const
52 {
53         render(get_pass(tag), 0);
54 }
55
56 void Object::render(const ObjectInstance &inst, const Tag &tag) const
57 {
58         render(get_pass(tag), &inst);
59 }
60
61 void Object::render(const list<const ObjectInstance *> &insts, const Tag &tag) const
62 {
63         render(get_pass(tag), insts);
64 }
65
66 void Object::setup_render(const ObjectPass &pass) const
67 {
68         if(!meshes[0])
69                 throw InvalidState("Trying to render Object without mesh");
70
71         if(pass.shprog)
72         {
73                 pass.shprog->bind();
74                 pass.shdata->apply();
75                 for(unsigned i=0; i<textures.size(); ++i)
76                 {
77                         TexUnit::activate(i);
78                         textures[i]->bind();
79                 }
80         }
81         else if(!textures.empty())
82                 textures.front()->bind();
83
84         if(material)
85                 material->apply();
86 }
87
88 void Object::finish_render(const ObjectPass &pass) const
89 {
90         if(pass.shprog)
91                 Program::unbind();
92         for(unsigned i=textures.size(); i--;)
93         {
94                 TexUnit::activate(i);
95                 Texture::unbind();
96         }
97 }
98
99 void Object::render(const ObjectPass &pass, const ObjectInstance *inst) const
100 {
101         setup_render(pass);
102
103         unsigned lod=0;
104         if(inst)
105         {
106                 inst->setup_render(pass);
107                 lod=min(inst->get_level_of_detail(), meshes.size()-1);
108         }
109
110         meshes[lod]->draw();
111
112         if(inst)
113                 inst->finish_render(pass);
114
115         finish_render(pass);
116 }
117
118 void Object::render(const ObjectPass &pass, const list<const ObjectInstance *> &insts) const
119 {
120         setup_render(pass);
121
122         for(list<const ObjectInstance *>::const_iterator i=insts.begin(); i!=insts.end(); ++i)
123         {
124                 (*i)->setup_render(pass);
125
126                 unsigned lod=min((*i)->get_level_of_detail(), meshes.size()-1);
127                 meshes[lod]->draw();
128
129                 (*i)->finish_render(pass);
130         }
131
132         finish_render(pass);
133 }
134
135
136 Object::Loader::Loader(Object &o, Collection &c):
137         obj(o),
138         coll(c)
139 {
140         add("lod_mesh", &Loader::lod_mesh);
141         add("material", &Object::material);
142         add("material_inline", &Loader::material_inline);
143         add("mesh",     &Loader::mesh);
144         add("pass",     &Loader::pass);
145         add("shader",   &Loader::shader);
146         add("texture",  &Loader::texture);
147 }
148
149 Object::Loader::~Loader()
150 {
151         for(map<unsigned, ObjectPass>::iterator i=obj.passes.begin(); i!=obj.passes.end(); ++i)
152                 if(i->second.shdata)
153                 {
154                         for(unsigned j=0; j<textures.size(); ++j)
155                                 i->second.shdata->uniform(i->second.shprog->get_uniform_location(textures[j]), static_cast<int>(j));
156                 }
157 }
158
159 void Object::Loader::lod_mesh(unsigned l, const string &n)
160 {
161         obj.meshes.resize(l+1, 0);
162         obj.meshes[l]=coll.get<Mesh>(n);
163 }
164
165 void Object::Loader::material_inline()
166 {
167         throw Exception("material_inline not supported yet");
168         /*RefPtr<Material> mat=new Material;
169         load_sub(*mat);
170         coll.add(format("%p%p", &obj, mat.get()), mat.get());
171         obj.material=mat.release();*/
172 }
173
174 void Object::Loader::mesh(const string &n)
175 {
176         obj.meshes[0]=coll.get<Mesh>(n);
177 }
178
179 void Object::Loader::pass(const string &n)
180 {
181         unsigned id=Tag(n).id;
182         if(obj.passes.count(id))
183                 throw KeyError("Duplicate pass name");
184         ObjectPass p;
185         load_sub(p, coll);
186         obj.passes[id]=p;
187 }
188
189 void Object::Loader::shader(const string &n)
190 {
191         Program *shprog=coll.get<Program>(n);
192         if(shprog)  // Allow for unsupported shaders
193         {
194                 RefPtr<ProgramData> shdata=new ProgramData;
195                 load_sub(*shdata, *shprog);
196
197                 obj.normal_pass->shprog=shprog;
198                 if(obj.normal_pass->shdata)
199                         delete obj.normal_pass->shdata;
200                 obj.normal_pass->shdata=shdata.release();
201         }
202 }
203
204 void Object::Loader::texture(const string &n)
205 {
206         unsigned eqsign=n.find('=');
207         if(eqsign!=string::npos)
208         {
209                 obj.textures.push_back(coll.get<Texture>(n.substr(eqsign+1)));
210                 textures.push_back(n.substr(0, eqsign));
211         }
212         else
213         {
214                 obj.textures.push_back(coll.get<Texture>(n));
215                 textures.push_back(n);
216         }
217 }
218
219 } // namespace GL
220 } // namespace Msp