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