X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Fobject.cpp;h=78f445577caa54209044e3db4dc40dd1e33b5cec;hb=b8b06a0ff96763ba7e188d9fcacbd8c0e3dcd515;hp=0469c31a99629187f17ce7d5dde803823636bb2e;hpb=85e83ace47e5a9a8ae7263886255dd81afc69278;p=libs%2Fgl.git diff --git a/source/object.cpp b/source/object.cpp index 0469c31a..78f44557 100644 --- a/source/object.cpp +++ b/source/object.cpp @@ -5,11 +5,13 @@ Copyright © 2007 Mikko Rasa, Mikkosoft Productions Distributed under the LGPL */ +#include #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" @@ -21,79 +23,97 @@ namespace Msp { namespace GL { Object::Object(): - mesh(0), - shprog(0), - shdata(0), + meshes(1, static_cast(0)), + main_texture(0), material(0) -{ } +{ + normal_pass=&passes[0]; +} Object::~Object() { - delete shdata; + for(map::iterator i=passes.begin(); i!=passes.end(); ++i) + delete i->second.shdata; } -void Object::render(const ObjectInstance *inst) const +bool Object::has_pass(const Tag &tag) const { - setup_render(); - - if(inst) - inst->setup_render(); - - mesh->draw(); - - if(inst) - inst->finish_render(); - - finish_render(); + return passes.count(tag.id); } -void Object::render(const list &insts) const +const ObjectPass &Object::get_pass(const Tag &tag) const { - setup_render(); - - for(list::const_iterator i=insts.begin(); i!=insts.end(); ++i) - { - (*i)->setup_render(); - - mesh->draw(); + map::const_iterator i=passes.find(tag.id); + if(i==passes.end()) + throw KeyError("Unknown pass"); + return i->second; +} - (*i)->finish_render(); - } +void Object::render(const Tag &tag) const +{ + render(get_pass(tag), 0); +} - finish_render(); +void Object::render(const ObjectInstance &inst, const Tag &tag) const +{ + render(get_pass(tag), &inst); } -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; ibind(); } } - else if(!textures.empty()) - textures.front()->bind(); + else if(main_texture && pass.use_textures) + main_texture->bind(); if(material) material->apply(); } -void Object::finish_render() const +void Object::finish_render(const ObjectPass &pass) const { - if(shprog) - Program::unbind(); - for(unsigned i=0; idraw(); + + finish_render(pass); +} + +void Object::render_instance(const ObjectPass &pass, const ObjectInstance &inst) const +{ + inst.setup_render(pass); + unsigned lod=min(inst.get_level_of_detail(), meshes.size()-1); + meshes[lod]->draw(); + inst.finish_render(pass); } @@ -101,42 +121,94 @@ 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("shader_texture", &Loader::shader_texture); add("texture", &Loader::texture); } -Object::Loader::~Loader() +void Object::Loader::finish() { - if(obj.shdata) - { - for(unsigned i=0; iuniform(obj.shprog->get_uniform_location(textures[i]), static_cast(i)); - } + for(map::iterator i=obj.passes.begin(); i!=obj.passes.end(); ++i) + if(i->second.shdata) + { + for(unsigned j=0; jsecond.shdata->uniform(i->second.shprog->get_uniform_location(textures[j]), static_cast(j)); + } +} + +void Object::Loader::lod_mesh(unsigned l, const string &n) +{ + obj.meshes.resize(l+1, 0); + obj.meshes[l]=coll.get(n); +} + +void Object::Loader::material_inline() +{ + RefPtr 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(n); +} + +void Object::Loader::pass(const string &n) +{ + unsigned id=Tag(n).id; + if(obj.passes.count(id)) + throw KeyError("Duplicate pass name"); + ObjectPass p; + load_sub(p, coll); + obj.passes[id]=p; } void Object::Loader::shader(const string &n) { - obj.shprog=&coll.get(n); - if(!obj.shdata) - obj.shdata=new ProgramData; + Program *shprog=coll.get(n); + if(shprog) // Allow for unsupported shaders + { + RefPtr 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) +void Object::Loader::shader_texture(const string &n) { unsigned eqsign=n.find('='); if(eqsign!=string::npos) { - obj.textures.push_back(&coll.get(n.substr(eqsign+1))); + obj.textures.push_back(coll.get(n.substr(eqsign+1))); textures.push_back(n.substr(0, eqsign)); } else { - obj.textures.push_back(&coll.get(n)); + obj.textures.push_back(coll.get(n)); textures.push_back(n); } } +void Object::Loader::texture(const string &n) +{ + if(obj.main_texture) + throw Exception("Only one main texture may be specified"); + + obj.main_texture=coll.get(n); + obj.textures.push_back(obj.main_texture); + textures.push_back("texture"); +} + } // namespace GL } // namespace Msp