X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Fobject.cpp;h=938e83f911211cd75b2e50302b6ad00d67345005;hb=b617c5d7b5283ad260a77f01e42e6170cabbc03d;hp=0469c31a99629187f17ce7d5dde803823636bb2e;hpb=85e83ace47e5a9a8ae7263886255dd81afc69278;p=libs%2Fgl.git diff --git a/source/object.cpp b/source/object.cpp index 0469c31a..938e83f9 100644 --- a/source/object.cpp +++ b/source/object.cpp @@ -5,6 +5,8 @@ Copyright © 2007 Mikko Rasa, Mikkosoft Productions Distributed under the LGPL */ +#include +#include #include "except.h" #include "material.h" #include "mesh.h" @@ -12,6 +14,7 @@ Distributed under the LGPL #include "objectinstance.h" #include "program.h" #include "programdata.h" +#include "technique.h" #include "texture.h" #include "texunit.h" @@ -21,121 +24,114 @@ namespace Msp { namespace GL { Object::Object(): - mesh(0), - shprog(0), - shdata(0), - material(0) + meshes(1), + technique(0), + own_mesh(false), + own_technique(false) { } Object::~Object() { - delete shdata; + if(own_technique) + delete technique; + if(own_mesh) + delete meshes[0]; } -void Object::render(const ObjectInstance *inst) const +void Object::render(const Tag &tag) const { - setup_render(); + const RenderPass *pass = get_pass(tag); + if(!pass) + return; - if(inst) - inst->setup_render(); - - mesh->draw(); + Bind bind(*pass); + meshes[0]->draw(); +} - if(inst) - inst->finish_render(); +void Object::render(const ObjectInstance &inst, const Tag &tag) const +{ + const RenderPass *pass = get_pass(tag); + if(!pass) + return; - finish_render(); + Bind bind(*pass); + render_instance(inst, tag); + meshes[0]->draw(); } -void Object::render(const list &insts) const +const RenderPass *Object::get_pass(const Tag &tag) const { - setup_render(); - - for(list::const_iterator i=insts.begin(); i!=insts.end(); ++i) - { - (*i)->setup_render(); + if(!technique->has_pass(tag)) + return 0; + return &technique->get_pass(tag); +} - mesh->draw(); +void Object::render_instance(const ObjectInstance &inst, const Tag &tag) const +{ + inst.setup_render(tag); + unsigned lod = min(inst.get_level_of_detail(), meshes.size()-1); + meshes[lod]->draw(); + inst.finish_render(tag); +} - (*i)->finish_render(); - } - finish_render(); +Object::Loader::Loader(Object &o): + DataFile::CollectionObjectLoader(o, 0) +{ + init(); } -void Object::setup_render() const +Object::Loader::Loader(Object &o, Collection &c): + DataFile::CollectionObjectLoader(o, &c) { - if(!mesh) - throw InvalidState("Trying to render Object without mesh"); - - if(shprog) - { - shprog->bind(); - shdata->apply(); - for(unsigned i=0; ibind(); - } - } - else if(!textures.empty()) - textures.front()->bind(); - - if(material) - material->apply(); + init(); } -void Object::finish_render() const +void Object::Loader::init() { - if(shprog) - Program::unbind(); - for(unsigned i=0; i(&Loader::mesh)); + add("mesh", static_cast(&Loader::mesh)); + add("technique", &Loader::technique); + add("technique", &Object::technique); +} -Object::Loader::Loader(Object &o, Collection &c): - obj(o), - coll(c) +void Object::Loader::lod_mesh(unsigned l, const string &n) { - add("material", &Object::material); - add("mesh", &Object::mesh); - add("shader", &Loader::shader); - add("texture", &Loader::texture); + obj.meshes.resize(l+1, 0); + obj.meshes[l] = get_collection().get(n); } -Object::Loader::~Loader() +void Object::Loader::mesh() { - if(obj.shdata) - { - for(unsigned i=0; iuniform(obj.shprog->get_uniform_location(textures[i]), static_cast(i)); - } + if(obj.meshes[0]) + throw InvalidState("A mesh is already loaded"); + + RefPtr msh = new Mesh; + load_sub(*msh); + obj.meshes[0] = msh.release(); + obj.own_mesh = true; } -void Object::Loader::shader(const string &n) +void Object::Loader::mesh(const std::string &n) { - obj.shprog=&coll.get(n); - if(!obj.shdata) - obj.shdata=new ProgramData; + if(obj.meshes[0]) + throw InvalidState("A mesh is already loaded"); + + obj.meshes[0] = get_collection().get(n); } -void Object::Loader::texture(const string &n) +void Object::Loader::technique() { - unsigned eqsign=n.find('='); - if(eqsign!=string::npos) - { - obj.textures.push_back(&coll.get(n.substr(eqsign+1))); - textures.push_back(n.substr(0, eqsign)); - } + RefPtr tech = new Technique; + if(coll) + load_sub(*tech, get_collection()); else - { - obj.textures.push_back(&coll.get(n)); - textures.push_back(n); - } + load_sub(*tech); + obj.technique = tech.release(); + obj.own_technique = true; } } // namespace GL