X-Git-Url: http://git.tdb.fi/?p=libs%2Fgl.git;a=blobdiff_plain;f=source%2Fobject.cpp;h=938e83f911211cd75b2e50302b6ad00d67345005;hp=db2866115a05561500843a7c7b0c7540b3be328c;hb=b617c5d7b5283ad260a77f01e42e6170cabbc03d;hpb=50e504e2171295d5922ddf87b358e0024db3ce40 diff --git a/source/object.cpp b/source/object.cpp index db286611..938e83f9 100644 --- a/source/object.cpp +++ b/source/object.cpp @@ -5,15 +5,16 @@ Copyright © 2007 Mikko Rasa, Mikkosoft Productions Distributed under the LGPL */ +#include #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 "technique.h" #include "texture.h" #include "texunit.h" @@ -23,194 +24,114 @@ namespace Msp { namespace GL { Object::Object(): - meshes(1, static_cast(0)), - main_texture(0), - material(0) -{ - normal_pass=&passes[0]; -} + meshes(1), + technique(0), + own_mesh(false), + own_technique(false) +{ } Object::~Object() { - for(map::iterator i=passes.begin(); i!=passes.end(); ++i) - delete i->second.shdata; -} - -bool Object::has_pass(const Tag &tag) const -{ - return passes.count(tag.id); -} - -const ObjectPass &Object::get_pass(const Tag &tag) const -{ - map::const_iterator i=passes.find(tag.id); - if(i==passes.end()) - throw KeyError("Unknown pass"); - return i->second; + if(own_technique) + delete technique; + if(own_mesh) + delete meshes[0]; } void Object::render(const Tag &tag) const { - render(get_pass(tag), 0); + const RenderPass *pass = get_pass(tag); + if(!pass) + return; + + Bind bind(*pass); + meshes[0]->draw(); } void Object::render(const ObjectInstance &inst, const Tag &tag) const { - render(get_pass(tag), &inst); -} + const RenderPass *pass = get_pass(tag); + if(!pass) + return; -void Object::setup_render(const ObjectPass &pass) const -{ - if(!meshes[0]) - throw InvalidState("Trying to render Object without mesh"); - - if(pass.shprog) - { - pass.shprog->bind(); - pass.shdata->apply(); - for(unsigned i=0; ibind(); - } - } - else if(main_texture && pass.use_textures) - main_texture->bind(); - - if(material) - material->bind(); + Bind bind(*pass); + render_instance(inst, tag); + meshes[0]->draw(); } -void Object::finish_render(const ObjectPass &pass) const +const RenderPass *Object::get_pass(const Tag &tag) const { - if(pass.shprog) - { - Program::unbind(); - for(unsigned i=textures.size(); i--;) - { - TexUnit::activate(i); - Texture::unbind(); - } - } - else if(main_texture) - Texture::unbind(); - - if(material) - Material::unbind(); + if(!technique->has_pass(tag)) + return 0; + return &technique->get_pass(tag); } -void Object::render(const ObjectPass &pass, const ObjectInstance *inst) const +void Object::render_instance(const ObjectInstance &inst, const Tag &tag) const { - setup_render(pass); - - if(inst) - render_instance(pass, *inst); - else - meshes[0]->draw(); - - finish_render(pass); + inst.setup_render(tag); + unsigned lod = min(inst.get_level_of_detail(), meshes.size()-1); + meshes[lod]->draw(); + inst.finish_render(tag); } -void Object::render_instance(const ObjectPass &pass, const ObjectInstance &inst) const + +Object::Loader::Loader(Object &o): + DataFile::CollectionObjectLoader(o, 0) { - inst.setup_render(pass); - unsigned lod=min(inst.get_level_of_detail(), meshes.size()-1); - meshes[lod]->draw(); - inst.finish_render(pass); + init(); } - Object::Loader::Loader(Object &o, Collection &c): - obj(o), - coll(c) + DataFile::CollectionObjectLoader(o, &c) { - add("lod_mesh", &Loader::lod_mesh); - add("material", &Object::material); - 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); + init(); } -void Object::Loader::finish() +void Object::Loader::init() { - 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)); - } + allow_pointer_reload = false; + + add("lod_mesh", &Loader::lod_mesh); + add("mesh", static_cast(&Loader::mesh)); + add("mesh", static_cast(&Loader::mesh)); + add("technique", &Loader::technique); + add("technique", &Object::technique); } void Object::Loader::lod_mesh(unsigned l, const string &n) { obj.meshes.resize(l+1, 0); - obj.meshes[l]=coll.get(n); + obj.meshes[l] = get_collection().get(n); } -void Object::Loader::material_inline() +void Object::Loader::mesh() { - RefPtr mat=new Material; - load_sub(*mat); - coll.add(format("%p%p", &obj, mat.get()), mat.get()); - obj.material=mat.release(); -} + if(obj.meshes[0]) + throw InvalidState("A mesh is already loaded"); -void Object::Loader::mesh(const string &n) -{ - obj.meshes[0]=coll.get(n); + RefPtr msh = new Mesh; + load_sub(*msh); + obj.meshes[0] = msh.release(); + obj.own_mesh = true; } -void Object::Loader::pass(const string &n) +void Object::Loader::mesh(const std::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; -} + if(obj.meshes[0]) + throw InvalidState("A mesh is already loaded"); -void Object::Loader::shader(const string &n) -{ - 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(); - } + obj.meshes[0] = get_collection().get(n); } -void Object::Loader::shader_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); - } -} - -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"); + load_sub(*tech); + obj.technique = tech.release(); + obj.own_technique = true; } } // namespace GL