X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Fobject.cpp;h=5ae04feb03027dd078a99335d651adce924bfa2c;hb=12ac9bec8e03029c1042fd7732a2cfc04c9a9584;hp=2026e3d58f3d0b658ed4b3bdba7fe9d211d6ffd1;hpb=5658c6ea1a5caf1b408366cebf785f79d650ff53;p=libs%2Fgl.git diff --git a/source/object.cpp b/source/object.cpp index 2026e3d5..5ae04feb 100644 --- a/source/object.cpp +++ b/source/object.cpp @@ -5,13 +5,13 @@ 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" @@ -24,174 +24,114 @@ namespace Msp { namespace GL { Object::Object(): - meshes(1, static_cast(0)), + meshes(1), technique(0), - main_texture(0), - material(0) + own_mesh(false), + own_technique(false) { } Object::~Object() { -} - -bool Object::has_pass(const Tag &tag) const -{ - if(technique) - return technique->has_pass(tag); - else - return tag.id==0; + if(own_technique) + delete technique; + if(own_mesh) + delete meshes[0]; } void Object::render(const Tag &tag) const { - const ObjectPass *pass=get_pass(tag); - setup_render(pass); + const RenderPass *pass = get_pass(tag); + if(!pass) + return; + + Bind bind(pass); meshes[0]->draw(); - finish_render(pass); } void Object::render(const ObjectInstance &inst, const Tag &tag) const { - const ObjectPass *pass=get_pass(tag); - setup_render(pass); + const RenderPass *pass = get_pass(tag); + if(!pass) + return; + + Bind bind(pass); render_instance(inst, tag); meshes[0]->draw(); - finish_render(pass); } -const ObjectPass *Object::get_pass(const Tag &tag) const +const RenderPass *Object::get_pass(const Tag &tag) const { - if(technique) - return &technique->get_pass(tag); - else if(tag.id==0) + if(!technique || !technique->has_pass(tag)) return 0; - throw KeyError("Unknown pass"); -} - -void Object::setup_render(const ObjectPass *pass) const -{ - if(!meshes[0]) - throw InvalidState("Trying to render Object without mesh"); - - if(pass && pass->shprog) - { - pass->shprog->bind(); - pass->shdata->apply(); - for(unsigned i=0; ibind(); - } - } - else if(main_texture && (!pass || pass->use_textures)) - main_texture->bind(); - - if(material) - material->bind(); -} - -void Object::finish_render(const ObjectPass *pass) const -{ - if(pass && 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(); + return &technique->get_pass(tag); } 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); + 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) +Object::Loader::Loader(Object &o): + DataFile::CollectionObjectLoader(o, 0) { - add("lod_mesh", &Loader::lod_mesh); - add("material", &Object::material); - add("material_inline", &Loader::material_inline); - add("mesh", &Loader::mesh); - add("shader_texture", &Loader::shader_texture); - add("technique", &Loader::technique); - add("texture", &Loader::texture); + init(); } -void Object::Loader::finish() +Object::Loader::Loader(Object &o, Collection &c): + DataFile::CollectionObjectLoader(o, &c) { - if(obj.technique && !obj.main_texture) - obj.main_texture=obj.technique->get_main_texture(); - for(unsigned i=0; iget_texture(i); - if(!obj.textures[i]) - throw Exception("Object does not specify all textures required by Technique"); - } - } + init(); } -void Object::Loader::lod_mesh(unsigned l, const string &n) +void Object::Loader::init() { - obj.meshes.resize(l+1, 0); - obj.meshes[l]=coll.get(n); -} + allow_pointer_reload = false; -void Object::Loader::material_inline() -{ - RefPtr mat=new Material; - load_sub(*mat); - coll.add(format("_%p", mat.get()), mat.get()); - obj.material=mat.release(); + 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::mesh(const string &n) +void Object::Loader::lod_mesh(unsigned l, const string &n) { - obj.meshes[0]=coll.get(n); + obj.meshes.resize(l+1, 0); + obj.meshes[l] = get_collection().get(n); } -void Object::Loader::shader_texture(const string &n) +void Object::Loader::mesh() { - if(!obj.technique) - throw InvalidState("Can't specify shader textures without a Technique"); - - unsigned eqsign=n.find('='); - if(eqsign==string::npos) - throw InvalidParameterValue("Must specify texture slot name"); + if(obj.meshes[0]) + throw InvalidState("A mesh is already loaded"); - obj.textures[obj.technique->get_texture_index(n.substr(0, eqsign))]=coll.get(n.substr(eqsign+1)); + RefPtr msh = new Mesh; + load_sub(*msh); + obj.meshes[0] = msh.release(); + obj.own_mesh = true; } -void Object::Loader::technique(const string &n) +void Object::Loader::mesh(const std::string &n) { - obj.technique=coll.get(n); - obj.textures.resize(obj.technique->get_n_textures()); - obj.material=obj.technique->get_material(); + 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() { - 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; + RefPtr tech = new Technique; + if(coll) + load_sub(*tech, get_collection()); + else + load_sub(*tech); + obj.technique = tech.release(); + obj.own_technique = true; } } // namespace GL