3 This file is part of libmspgl
4 Copyright © 2007 Mikko Rasa, Mikkosoft Productions
5 Distributed under the LGPL
8 #include <msp/strings/formatter.h>
13 #include "objectinstance.h"
14 #include "objectpass.h"
16 #include "programdata.h"
26 meshes(1, static_cast<Mesh *>(0)),
30 normal_pass=&passes[0];
35 for(map<unsigned, ObjectPass>::iterator i=passes.begin(); i!=passes.end(); ++i)
36 delete i->second.shdata;
39 bool Object::has_pass(const Tag &tag) const
41 return passes.count(tag.id);
44 const ObjectPass &Object::get_pass(const Tag &tag) const
46 map<unsigned, ObjectPass>::const_iterator i=passes.find(tag.id);
48 throw KeyError("Unknown pass");
52 void Object::render(const Tag &tag) const
54 render(get_pass(tag), 0);
57 void Object::render(const ObjectInstance &inst, const Tag &tag) const
59 render(get_pass(tag), &inst);
62 void Object::setup_render(const ObjectPass &pass) const
65 throw InvalidState("Trying to render Object without mesh");
71 for(unsigned i=0; i<textures.size(); ++i)
77 else if(main_texture && pass.use_textures)
84 void Object::finish_render(const ObjectPass &pass) const
89 for(unsigned i=textures.size(); i--;)
99 void Object::render(const ObjectPass &pass, const ObjectInstance *inst) const
104 render_instance(pass, *inst);
111 void Object::render_instance(const ObjectPass &pass, const ObjectInstance &inst) const
113 inst.setup_render(pass);
114 unsigned lod=min(inst.get_level_of_detail(), meshes.size()-1);
116 inst.finish_render(pass);
120 Object::Loader::Loader(Object &o, Collection &c):
124 add("lod_mesh", &Loader::lod_mesh);
125 add("material", &Object::material);
126 add("material_inline", &Loader::material_inline);
127 add("mesh", &Loader::mesh);
128 add("pass", &Loader::pass);
129 add("shader", &Loader::shader);
130 add("shader_texture", &Loader::shader_texture);
131 add("texture", &Loader::texture);
134 void Object::Loader::finish()
136 for(map<unsigned, ObjectPass>::iterator i=obj.passes.begin(); i!=obj.passes.end(); ++i)
139 for(unsigned j=0; j<textures.size(); ++j)
140 i->second.shdata->uniform(i->second.shprog->get_uniform_location(textures[j]), static_cast<int>(j));
144 void Object::Loader::lod_mesh(unsigned l, const string &n)
146 obj.meshes.resize(l+1, 0);
147 obj.meshes[l]=coll.get<Mesh>(n);
150 void Object::Loader::material_inline()
152 RefPtr<Material> mat=new Material;
154 coll.add(format("%p%p", &obj, mat.get()), mat.get());
155 obj.material=mat.release();
158 void Object::Loader::mesh(const string &n)
160 obj.meshes[0]=coll.get<Mesh>(n);
163 void Object::Loader::pass(const string &n)
165 unsigned id=Tag(n).id;
166 if(obj.passes.count(id))
167 throw KeyError("Duplicate pass name");
173 void Object::Loader::shader(const string &n)
175 Program *shprog=coll.get<Program>(n);
176 if(shprog) // Allow for unsupported shaders
178 RefPtr<ProgramData> shdata=new ProgramData;
179 load_sub(*shdata, *shprog);
181 obj.normal_pass->shprog=shprog;
182 if(obj.normal_pass->shdata)
183 delete obj.normal_pass->shdata;
184 obj.normal_pass->shdata=shdata.release();
188 void Object::Loader::shader_texture(const string &n)
190 unsigned eqsign=n.find('=');
191 if(eqsign!=string::npos)
193 obj.textures.push_back(coll.get<Texture>(n.substr(eqsign+1)));
194 textures.push_back(n.substr(0, eqsign));
198 obj.textures.push_back(coll.get<Texture>(n));
199 textures.push_back(n);
203 void Object::Loader::texture(const string &n)
206 throw Exception("Only one main texture may be specified");
208 obj.main_texture=coll.get<Texture>(n);
209 obj.textures.push_back(obj.main_texture);
210 textures.push_back("texture");