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