]> git.tdb.fi Git - libs/gl.git/blob - source/object.cpp
Drop Renderable::has_pass; renderables are now expected to ignore unknown 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 "technique.h"
18 #include "texture.h"
19 #include "texunit.h"
20
21 using namespace std;
22
23 namespace Msp {
24 namespace GL {
25
26 Object::Object():
27         meshes(1, static_cast<Mesh *>(0)),
28         technique(0),
29         main_texture(0),
30         material(0)
31 { }
32
33 Object::~Object()
34 {
35 }
36
37 void Object::render(const Tag &tag) const
38 {
39         if(!can_render(tag))
40                 return;
41
42         const ObjectPass *pass=get_pass(tag);
43         setup_render(pass);
44         meshes[0]->draw();
45         finish_render(pass);
46 }
47
48 void Object::render(const ObjectInstance &inst, const Tag &tag) const
49 {
50         if(!can_render(tag))
51                 return;
52
53         const ObjectPass *pass=get_pass(tag);
54         setup_render(pass);
55         render_instance(inst, tag);
56         meshes[0]->draw();
57         finish_render(pass);
58 }
59
60 bool Object::can_render(const Tag &tag) const
61 {
62         if(technique)
63                 return technique->has_pass(tag);
64         else
65                 return tag.id==0;
66 }
67
68 const ObjectPass *Object::get_pass(const Tag &tag) const
69 {
70         if(technique)
71                 return &technique->get_pass(tag);
72         else if(tag.id==0)
73                 return 0;
74         throw KeyError("Unknown pass");
75 }
76
77 void Object::setup_render(const ObjectPass *pass) const
78 {
79         if(!meshes[0])
80                 throw InvalidState("Trying to render Object without mesh");
81
82         if(pass && pass->shprog)
83         {
84                 pass->shprog->bind();
85                 pass->shdata->apply();
86                 for(unsigned i=0; i<textures.size(); ++i)
87                 {
88                         TexUnit::activate(i);
89                         textures[i]->bind();
90                 }
91         }
92         else if(main_texture && (!pass || pass->use_textures))
93                 main_texture->bind();
94
95         if(material)
96                 material->bind();
97 }
98
99 void Object::finish_render(const ObjectPass *pass) const
100 {
101         if(pass && pass->shprog)
102         {
103                 Program::unbind();
104                 for(unsigned i=textures.size(); i--;)
105                 {
106                         TexUnit::activate(i);
107                         Texture::unbind();
108                 }
109         }
110         else if(main_texture)
111                 Texture::unbind();
112
113         if(material)
114                 Material::unbind();
115 }
116
117 void Object::render_instance(const ObjectInstance &inst, const Tag &tag) const
118 {
119         inst.setup_render(tag);
120         unsigned lod=min<unsigned>(inst.get_level_of_detail(), meshes.size()-1);
121         meshes[lod]->draw();
122         inst.finish_render(tag);
123 }
124
125
126 Object::Loader::Loader(Object &o, Collection &c):
127         obj(o),
128         coll(c)
129 {
130         add("lod_mesh", &Loader::lod_mesh);
131         add("material", &Object::material);
132         add("material_inline", &Loader::material_inline);
133         add("mesh",     &Loader::mesh);
134         add("shader_texture", &Loader::shader_texture);
135         add("technique", &Loader::technique);
136         add("texture",  &Loader::texture);
137 }
138
139 void Object::Loader::finish()
140 {
141         if(obj.technique && !obj.main_texture)
142                 obj.main_texture=obj.technique->get_main_texture();
143         for(unsigned i=0; i<obj.textures.size(); ++i)
144         {
145                 if(!obj.textures[i])
146                 {
147                         obj.textures[i]=obj.technique->get_texture(i);
148                         if(!obj.textures[i])
149                                 throw Exception("Object does not specify all textures required by Technique");
150                 }
151         }
152 }
153
154 void Object::Loader::lod_mesh(unsigned l, const string &n)
155 {
156         obj.meshes.resize(l+1, 0);
157         obj.meshes[l]=coll.get<Mesh>(n);
158 }
159
160 void Object::Loader::material_inline()
161 {
162         RefPtr<Material> mat=new Material;
163         load_sub(*mat);
164         coll.add(format("_%p", mat.get()), mat.get());
165         obj.material=mat.release();
166 }
167
168 void Object::Loader::mesh(const string &n)
169 {
170         obj.meshes[0]=coll.get<Mesh>(n);
171 }
172
173 void Object::Loader::shader_texture(const string &n)
174 {
175         if(!obj.technique)
176                 throw InvalidState("Can't specify shader textures without a Technique");
177
178         string::size_type eqsign=n.find('=');
179         if(eqsign==string::npos)
180                 throw InvalidParameterValue("Must specify texture slot name");
181
182         obj.textures[obj.technique->get_texture_index(n.substr(0, eqsign))]=coll.get<Texture>(n.substr(eqsign+1));
183 }
184
185 void Object::Loader::technique(const string &n)
186 {
187         obj.technique=coll.get<Technique>(n);
188         obj.textures.resize(obj.technique->get_n_textures());
189         obj.material=obj.technique->get_material();
190 }
191
192 void Object::Loader::texture(const string &n)
193 {
194         if(obj.main_texture)
195                 throw Exception("Only one main texture may be specified");
196
197         Texture *tex=coll.get<Texture>(n);
198         if(obj.technique)
199                 obj.textures[obj.technique->get_texture_index("texture")]=tex;
200         obj.main_texture=tex;
201 }
202
203 } // namespace GL
204 } // namespace Msp