]> git.tdb.fi Git - libs/gl.git/blob - source/object.cpp
Use DevIL for loading images
[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 ObjectInstance *inst) const
52 {
53         render(*normal_pass, inst);
54 }
55
56 void Object::render(const string &pn, const ObjectInstance *inst) const
57 {
58         render(get_pass(pn), inst);
59 }
60
61 void Object::render(const list<const ObjectInstance *> &insts) const
62 {
63         render(*normal_pass, insts);
64 }
65
66 void Object::render(const string &pn, const list<const ObjectInstance *> &insts) const
67 {
68         render(get_pass(pn), insts);
69 }
70
71 void Object::setup_render(const ObjectPass &pass) const
72 {
73         if(!meshes[0])
74                 throw InvalidState("Trying to render Object without mesh");
75
76         if(pass.shprog)
77         {
78                 pass.shprog->bind();
79                 pass.shdata->apply();
80                 for(unsigned i=0; i<textures.size(); ++i)
81                 {
82                         TexUnit::activate(i);
83                         textures[i]->bind();
84                 }
85         }
86         else if(!textures.empty())
87                 textures.front()->bind();
88
89         if(material)
90                 material->apply();
91 }
92
93 void Object::finish_render(const ObjectPass &pass) const
94 {
95         if(pass.shprog)
96                 Program::unbind();
97         for(unsigned i=textures.size(); i--;)
98         {
99                 TexUnit::activate(i);
100                 Texture::unbind();
101         }
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("texture",  &Loader::texture);
152 }
153
154 Object::Loader::~Loader()
155 {
156         for(map<string, ObjectPass>::iterator i=obj.passes.begin(); i!=obj.passes.end(); ++i)
157                 if(i->second.shdata)
158                 {
159                         for(unsigned j=0; j<textures.size(); ++j)
160                                 i->second.shdata->uniform(i->second.shprog->get_uniform_location(textures[j]), static_cast<int>(j));
161                 }
162 }
163
164 void Object::Loader::lod_mesh(unsigned l, const string &n)
165 {
166         obj.meshes.resize(l+1, 0);
167         obj.meshes[l]=&coll.get<Mesh>(n);
168 }
169
170 void Object::Loader::material_inline()
171 {
172         throw Exception("material_inline not supported yet");
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         if(obj.passes.count(n))
187                 throw KeyError("Duplicate pass name");
188         ObjectPass p;
189         load_sub(p, coll);
190         obj.passes[n]=p;
191 }
192
193 void Object::Loader::shader(const string &n)
194 {
195         Program *shprog=&coll.get<Program>(n);
196         if(shprog)  // Allow for unsupported shaders
197         {
198                 RefPtr<ProgramData> shdata=new ProgramData;
199                 load_sub(*shdata, *shprog);
200
201                 obj.normal_pass->shprog=shprog;
202                 if(obj.normal_pass->shdata)
203                         delete obj.normal_pass->shdata;
204                 obj.normal_pass->shdata=shdata.release();
205         }
206 }
207
208 void Object::Loader::texture(const string &n)
209 {
210         unsigned eqsign=n.find('=');
211         if(eqsign!=string::npos)
212         {
213                 obj.textures.push_back(&coll.get<Texture>(n.substr(eqsign+1)));
214                 textures.push_back(n.substr(0, eqsign));
215         }
216         else
217         {
218                 obj.textures.push_back(&coll.get<Texture>(n));
219                 textures.push_back(n);
220         }
221 }
222
223 } // namespace GL
224 } // namespace Msp