X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Fobject.cpp;h=f2d1f8bf74a9781c64d829e2f7950513296dbae1;hb=927a1aa0a3a27e463ec0efc08bd08e7c4e969909;hp=0469c31a99629187f17ce7d5dde803823636bb2e;hpb=85e83ace47e5a9a8ae7263886255dd81afc69278;p=libs%2Fgl.git diff --git a/source/object.cpp b/source/object.cpp index 0469c31a..f2d1f8bf 100644 --- a/source/object.cpp +++ b/source/object.cpp @@ -5,13 +5,17 @@ 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" @@ -21,121 +25,179 @@ namespace Msp { namespace GL { Object::Object(): - mesh(0), - shprog(0), - shdata(0), + meshes(1, static_cast(0)), + technique(0), + main_texture(0), material(0) { } Object::~Object() { - delete shdata; } -void Object::render(const ObjectInstance *inst) const +void Object::render(const Tag &tag) const { - setup_render(); + if(!can_render(tag)) + return; - if(inst) - inst->setup_render(); - - mesh->draw(); - - if(inst) - inst->finish_render(); - - finish_render(); + const ObjectPass *pass=get_pass(tag); + setup_render(pass); + meshes[0]->draw(); + finish_render(pass); } -void Object::render(const list &insts) const +void Object::render(const ObjectInstance &inst, const Tag &tag) const { - setup_render(); - - for(list::const_iterator i=insts.begin(); i!=insts.end(); ++i) - { - (*i)->setup_render(); - - mesh->draw(); + if(!can_render(tag)) + return; + + const ObjectPass *pass=get_pass(tag); + setup_render(pass); + render_instance(inst, tag); + meshes[0]->draw(); + finish_render(pass); +} - (*i)->finish_render(); - } +bool Object::can_render(const Tag &tag) const +{ + if(technique) + return technique->has_pass(tag); + else + return tag.id==0; +} - finish_render(); +const ObjectPass *Object::get_pass(const Tag &tag) const +{ + if(technique) + return &technique->get_pass(tag); + else if(tag.id==0) + return 0; + throw KeyError("Unknown pass"); } -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 && 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 || pass->use_textures)) + main_texture->bind(); if(material) - material->apply(); + material->bind(); } -void Object::finish_render() const +void Object::finish_render(const ObjectPass *pass) const { - if(shprog) - Program::unbind(); - for(unsigned i=0; ishprog) { - TexUnit::activate(i); - Texture::unbind(); + Program::unbind(); + for(unsigned i=textures.size(); i--;) + { + TexUnit::activate(i); + Texture::unbind(); + } } + else if(main_texture) + Texture::unbind(); + + if(material) + Material::unbind(); +} + +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); } 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("mesh", &Object::mesh); - add("shader", &Loader::shader); + add("material_inline", &Loader::material_inline); + add("mesh", &Loader::mesh); + add("shader_texture", &Loader::shader_texture); + add("technique", &Loader::technique); add("texture", &Loader::texture); } -Object::Loader::~Loader() +void Object::Loader::finish() { - if(obj.shdata) + if(obj.technique && !obj.main_texture) + obj.main_texture=obj.technique->get_main_texture(); + for(unsigned i=0; iuniform(obj.shprog->get_uniform_location(textures[i]), static_cast(i)); + if(!obj.textures[i]) + { + obj.textures[i]=obj.technique->get_texture(i); + if(!obj.textures[i]) + throw Exception("Object does not specify all textures required by Technique"); + } } } -void Object::Loader::shader(const string &n) +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() { - obj.shprog=&coll.get(n); - if(!obj.shdata) - obj.shdata=new ProgramData; + RefPtr mat=new Material; + load_sub(*mat); + coll->add(format("_%p", mat.get()), mat.get()); + obj.material=mat.release(); +} + +void Object::Loader::mesh(const string &n) +{ + obj.meshes[0]=coll->get(n); +} + +void Object::Loader::shader_texture(const string &n) +{ + if(!obj.technique) + throw InvalidState("Can't specify shader textures without a Technique"); + + string::size_type eqsign=n.find('='); + if(eqsign==string::npos) + throw InvalidParameterValue("Must specify texture slot name"); + + obj.textures[obj.technique->get_texture_index(n.substr(0, eqsign))]=coll->get(n.substr(eqsign+1)); +} + +void Object::Loader::technique(const string &n) +{ + obj.technique=coll->get(n); + obj.textures.resize(obj.technique->get_n_textures()); + obj.material=obj.technique->get_material(); } void Object::Loader::texture(const string &n) { - 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)); - } - else - { - obj.textures.push_back(&coll.get(n)); - textures.push_back(n); - } + if(obj.main_texture) + throw Exception("Only one main texture may be specified"); + + Texture *tex=coll->get(n); + if(obj.technique) + obj.textures[obj.technique->get_texture_index("texture")]=tex; + obj.main_texture=tex; } } // namespace GL