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)),
29 normal_pass=&passes[0];
34 for(map<unsigned, ObjectPass>::iterator i=passes.begin(); i!=passes.end(); ++i)
35 delete i->second.shdata;
38 bool Object::has_pass(const Tag &tag) const
40 return passes.count(tag.id);
43 const ObjectPass &Object::get_pass(const Tag &tag) const
45 map<unsigned, ObjectPass>::const_iterator i=passes.find(tag.id);
47 throw KeyError("Unknown pass");
51 void Object::render(const Tag &tag) const
53 render(get_pass(tag), 0);
56 void Object::render(const ObjectInstance &inst, const Tag &tag) const
58 render(get_pass(tag), &inst);
61 void Object::render(const list<const ObjectInstance *> &insts, const Tag &tag) const
63 render(get_pass(tag), insts);
66 void Object::setup_render(const ObjectPass &pass) const
69 throw InvalidState("Trying to render Object without mesh");
75 for(unsigned i=0; i<textures.size(); ++i)
81 else if(!textures.empty())
82 textures.front()->bind();
88 void Object::finish_render(const ObjectPass &pass) const
92 for(unsigned i=textures.size(); i--;)
99 void Object::render(const ObjectPass &pass, const ObjectInstance *inst) const
106 inst->setup_render(pass);
107 lod=min(inst->get_level_of_detail(), meshes.size()-1);
113 inst->finish_render(pass);
118 void Object::render(const ObjectPass &pass, const list<const ObjectInstance *> &insts) const
122 for(list<const ObjectInstance *>::const_iterator i=insts.begin(); i!=insts.end(); ++i)
124 (*i)->setup_render(pass);
126 unsigned lod=min((*i)->get_level_of_detail(), meshes.size()-1);
129 (*i)->finish_render(pass);
136 Object::Loader::Loader(Object &o, Collection &c):
140 add("lod_mesh", &Loader::lod_mesh);
141 add("material", &Object::material);
142 add("material_inline", &Loader::material_inline);
143 add("mesh", &Loader::mesh);
144 add("pass", &Loader::pass);
145 add("shader", &Loader::shader);
146 add("texture", &Loader::texture);
149 Object::Loader::~Loader()
151 for(map<unsigned, ObjectPass>::iterator i=obj.passes.begin(); i!=obj.passes.end(); ++i)
154 for(unsigned j=0; j<textures.size(); ++j)
155 i->second.shdata->uniform(i->second.shprog->get_uniform_location(textures[j]), static_cast<int>(j));
159 void Object::Loader::lod_mesh(unsigned l, const string &n)
161 obj.meshes.resize(l+1, 0);
162 obj.meshes[l]=coll.get<Mesh>(n);
165 void Object::Loader::material_inline()
167 RefPtr<Material> mat=new Material;
169 coll.add(format("%p%p", &obj, mat.get()), mat.get());
170 obj.material=mat.release();
173 void Object::Loader::mesh(const string &n)
175 obj.meshes[0]=coll.get<Mesh>(n);
178 void Object::Loader::pass(const string &n)
180 unsigned id=Tag(n).id;
181 if(obj.passes.count(id))
182 throw KeyError("Duplicate pass name");
188 void Object::Loader::shader(const string &n)
190 Program *shprog=coll.get<Program>(n);
191 if(shprog) // Allow for unsupported shaders
193 RefPtr<ProgramData> shdata=new ProgramData;
194 load_sub(*shdata, *shprog);
196 obj.normal_pass->shprog=shprog;
197 if(obj.normal_pass->shdata)
198 delete obj.normal_pass->shdata;
199 obj.normal_pass->shdata=shdata.release();
203 void Object::Loader::texture(const string &n)
205 unsigned eqsign=n.find('=');
206 if(eqsign!=string::npos)
208 obj.textures.push_back(coll.get<Texture>(n.substr(eqsign+1)));
209 textures.push_back(n.substr(0, eqsign));
213 obj.textures.push_back(coll.get<Texture>(n));
214 textures.push_back(n);