Distributed under the LGPL
*/
+#include <msp/strings/formatter.h>
#include "except.h"
#include "material.h"
#include "mesh.h"
#include "object.h"
#include "objectinstance.h"
+#include "objectpass.h"
#include "program.h"
#include "programdata.h"
#include "texture.h"
namespace GL {
Object::Object():
- mesh(0),
- shprog(0),
- shdata(0),
+ meshes(1, static_cast<Mesh *>(0)),
material(0)
-{ }
+{
+ normal_pass=&passes[""];
+}
Object::~Object()
{
- delete shdata;
+ for(map<string, ObjectPass>::iterator i=passes.begin(); i!=passes.end(); ++i)
+ delete i->second.shdata;
}
-void Object::render(const ObjectInstance *inst) const
+bool Object::has_pass(const string &pn) const
{
- setup_render();
-
- if(inst)
- inst->setup_render();
+ return passes.count(pn);
+}
- mesh->draw();
+const ObjectPass &Object::get_pass(const string &pn) const
+{
+ map<string, ObjectPass>::const_iterator i=passes.find(pn);
+ if(i==passes.end())
+ throw KeyError("Unknown pass");
+ return i->second;
+}
- if(inst)
- inst->finish_render();
+void Object::render(const ObjectInstance *inst) const
+{
+ render(*normal_pass, inst);
+}
- finish_render();
+void Object::render(const string &pn, const ObjectInstance *inst) const
+{
+ render(get_pass(pn), inst);
}
void Object::render(const list<const ObjectInstance *> &insts) const
{
- setup_render();
-
- for(list<const ObjectInstance *>::const_iterator i=insts.begin(); i!=insts.end(); ++i)
- {
- (*i)->setup_render();
-
- mesh->draw();
-
- (*i)->finish_render();
- }
+ render(*normal_pass, insts);
+}
- finish_render();
+void Object::render(const string &pn, const list<const ObjectInstance *> &insts) const
+{
+ render(get_pass(pn), insts);
}
-void Object::setup_render() const
+void Object::setup_render(const ObjectPass &pass) const
{
- if(!mesh)
+ if(!meshes[0])
throw InvalidState("Trying to render Object without mesh");
- if(shprog)
+ if(pass.shprog)
{
- shprog->bind();
- shdata->apply();
+ pass.shprog->bind();
+ pass.shdata->apply();
for(unsigned i=0; i<textures.size(); ++i)
{
TexUnit::activate(i);
material->apply();
}
-void Object::finish_render() const
+void Object::finish_render(const ObjectPass &pass) const
{
- if(shprog)
+ if(pass.shprog)
Program::unbind();
- for(unsigned i=0; i<textures.size(); ++i)
+ for(unsigned i=textures.size(); i--;)
{
TexUnit::activate(i);
Texture::unbind();
}
}
+void Object::render(const ObjectPass &pass, const ObjectInstance *inst) const
+{
+ setup_render(pass);
+
+ unsigned lod=0;
+ if(inst)
+ {
+ inst->setup_render(pass);
+ lod=min(inst->get_level_of_detail(), meshes.size()-1);
+ }
+
+ meshes[lod]->draw();
+
+ if(inst)
+ inst->finish_render(pass);
+
+ finish_render(pass);
+}
+
+void Object::render(const ObjectPass &pass, const list<const ObjectInstance *> &insts) const
+{
+ setup_render(pass);
+
+ for(list<const ObjectInstance *>::const_iterator i=insts.begin(); i!=insts.end(); ++i)
+ {
+ (*i)->setup_render(pass);
+
+ unsigned lod=min((*i)->get_level_of_detail(), meshes.size()-1);
+ meshes[lod]->draw();
+
+ (*i)->finish_render(pass);
+ }
+
+ finish_render(pass);
+}
+
Object::Loader::Loader(Object &o, Collection &c):
obj(o),
coll(c)
{
+ add("lod_mesh", &Loader::lod_mesh);
add("material", &Object::material);
- add("mesh", &Object::mesh);
+ add("material_inline", &Loader::material_inline);
+ add("mesh", &Loader::mesh);
+ add("pass", &Loader::pass);
add("shader", &Loader::shader);
add("texture", &Loader::texture);
}
Object::Loader::~Loader()
{
- if(obj.shdata)
- {
- for(unsigned i=0; i<textures.size(); ++i)
- obj.shdata->uniform(obj.shprog->get_uniform_location(textures[i]), static_cast<int>(i));
- }
+ for(map<string, ObjectPass>::iterator i=obj.passes.begin(); i!=obj.passes.end(); ++i)
+ if(i->second.shdata)
+ {
+ for(unsigned j=0; j<textures.size(); ++j)
+ i->second.shdata->uniform(i->second.shprog->get_uniform_location(textures[j]), static_cast<int>(j));
+ }
+}
+
+void Object::Loader::lod_mesh(unsigned l, const string &n)
+{
+ obj.meshes.resize(l+1, 0);
+ obj.meshes[l]=&coll.get<Mesh>(n);
+}
+
+void Object::Loader::material_inline()
+{
+ throw Exception("material_inline not supported yet");
+ /*RefPtr<Material> mat=new Material;
+ load_sub(*mat);
+ coll.add(format("%p%p", &obj, mat.get()), mat.get());
+ obj.material=mat.release();*/
+}
+
+void Object::Loader::mesh(const string &n)
+{
+ obj.meshes[0]=&coll.get<Mesh>(n);
+}
+
+void Object::Loader::pass(const string &n)
+{
+ if(obj.passes.count(n))
+ throw KeyError("Duplicate pass name");
+ ObjectPass p;
+ load_sub(p, coll);
+ obj.passes[n]=p;
}
void Object::Loader::shader(const string &n)
{
- obj.shprog=&coll.get<Program>(n);
- if(!obj.shdata)
- obj.shdata=new ProgramData;
+ Program *shprog=&coll.get<Program>(n);
+ if(shprog) // Allow for unsupported shaders
+ {
+ RefPtr<ProgramData> shdata=new ProgramData;
+ load_sub(*shdata, *shprog);
+
+ obj.normal_pass->shprog=shprog;
+ if(obj.normal_pass->shdata)
+ delete obj.normal_pass->shdata;
+ obj.normal_pass->shdata=shdata.release();
+ }
}
void Object::Loader::texture(const string &n)