X-Git-Url: http://git.tdb.fi/?p=libs%2Fgl.git;a=blobdiff_plain;f=source%2Fobject.cpp;h=d573fdd4975695eddfd3d164eca0128d97936937;hp=d5d5231bab47dbdd9358a489ea975841298c4a87;hb=f14435e58bfa0fa697a06ba9a454bb30cd37d9d8;hpb=e759062876ee7fc81d1c2f40818d5bf97898d53d diff --git a/source/object.cpp b/source/object.cpp index d5d5231b..d573fdd4 100644 --- a/source/object.cpp +++ b/source/object.cpp @@ -1,22 +1,15 @@ -/* $Id$ - -This file is part of libmspgl -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 "renderer.h" #include "technique.h" -#include "texture.h" -#include "texunit.h" +#include "texturing.h" using namespace std; @@ -24,180 +17,163 @@ namespace Msp { namespace GL { Object::Object(): - meshes(1, static_cast(0)), - technique(0), - main_texture(0), - material(0) + meshes(1) { } Object::~Object() { } -void Object::render(const Tag &tag) const +void Object::set_mesh(unsigned i, const Mesh *m) { - if(!can_render(tag)) - return; + if(i>meshes.size()) + throw InvalidParameterValue("LODs must be continuous"); - const ObjectPass *pass=get_pass(tag); - setup_render(pass); - meshes[0]->draw(); - finish_render(pass); + if(i==meshes.size()) + meshes.push_back(m); + else + meshes[i] = m; + meshes[i].keep(); } -void Object::render(const ObjectInstance &inst, const Tag &tag) const +const Mesh *Object::get_mesh(unsigned i) const { - if(!can_render(tag)) - return; + if(i>=meshes.size()) + return 0; - const ObjectPass *pass=get_pass(tag); - setup_render(pass); - render_instance(inst, tag); - meshes[0]->draw(); - finish_render(pass); + return meshes[i].get(); } -bool Object::can_render(const Tag &tag) const +void Object::set_technique(const Technique *t) { - if(technique) - return technique->has_pass(tag); - else - return tag.id==0; + technique = t; + technique.keep(); } -const ObjectPass *Object::get_pass(const Tag &tag) const +void Object::render(const Tag &tag) const { - if(technique) - return &technique->get_pass(tag); - else if(tag.id==0) - return 0; - throw KeyError("Unknown pass"); + const RenderPass *pass = get_pass(tag); + if(!pass) + return; + + Bind bind_shader(pass->get_shader_program()); + if(pass->get_shader_data()) + pass->get_shader_data()->apply(); + Bind bind_material(pass->get_material()); + Bind bind_texturing(pass->get_texturing()); + + meshes.front()->draw(); } -void Object::setup_render(const ObjectPass *pass) const +void Object::render(Renderer &renderer, const Tag &tag) const { - if(!meshes[0]) - throw InvalidState("Trying to render Object without mesh"); + const RenderPass *pass = get_pass(tag); + if(!pass) + return; - 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(); + Renderer::Push push(renderer); + renderer.set_shader(pass->get_shader_program(), pass->get_shader_data()); + renderer.set_material(pass->get_material()); + renderer.set_texturing(pass->get_texturing()); - if(material) - material->bind(); + meshes.front()->draw(renderer); } -void Object::finish_render(const ObjectPass *pass) const +void Object::render(Renderer &renderer, const ObjectInstance &inst, const Tag &tag) const { - if(pass && pass->shprog) - { - Program::unbind(); - for(unsigned i=textures.size(); i--;) - { - TexUnit::activate(i); - Texture::unbind(); - } - } - else if(main_texture) - Texture::unbind(); + const RenderPass *pass = get_pass(tag); + if(!pass) + return; + + Renderer::Push push(renderer); + renderer.set_shader(pass->get_shader_program(), pass->get_shader_data()); + renderer.set_material(pass->get_material()); + renderer.set_texturing(pass->get_texturing()); - if(material) - Material::unbind(); + inst.setup_render(renderer, tag); + unsigned lod = min(inst.get_level_of_detail(), meshes.size()-1); + meshes[lod]->draw(renderer); + inst.finish_render(renderer, tag); } -void Object::render_instance(const ObjectInstance &inst, const Tag &tag) const +const RenderPass *Object::get_pass(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); + if(!technique || !technique->has_pass(tag)) + return 0; + return &technique->get_pass(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("mesh", &Loader::mesh_inline); + add("mesh", &Loader::mesh_inline_lod); + add("mesh", &Loader::mesh); + add("mesh", &Loader::mesh_lod); + add("technique", &Loader::technique_inline); + add("technique", &Loader::technique); + + // Deprecated alias, will be removed + add("lod_mesh", &Loader::mesh_lod); } -void Object::Loader::mesh(const string &n) +void Object::Loader::mesh_inline() { - obj.meshes[0]=coll.get(n); + RefPtr msh = new Mesh; + load_sub(*msh); + obj.meshes.front() = msh; } -void Object::Loader::shader_texture(const string &n) +void Object::Loader::mesh_inline_lod(unsigned l) { - if(!obj.technique) - throw InvalidState("Can't specify shader textures without a Technique"); + if(l>obj.meshes.size()) + throw InvalidParameterValue("LODs must be continuous"); - string::size_type eqsign=n.find('='); - if(eqsign==string::npos) - throw InvalidParameterValue("Must specify texture slot name"); + RefPtr msh = new Mesh; + load_sub(*msh); + if(l==obj.meshes.size()) + obj.meshes.push_back(msh); + else + obj.meshes[l] = msh; +} - obj.textures[obj.technique->get_texture_index(n.substr(0, eqsign))]=coll.get(n.substr(eqsign+1)); +void Object::Loader::mesh(const std::string &n) +{ + obj.set_mesh(get_collection().get(n)); } -void Object::Loader::technique(const string &n) +void Object::Loader::mesh_lod(unsigned l, const string &n) { - obj.technique=coll.get(n); - obj.textures.resize(obj.technique->get_n_textures()); - obj.material=obj.technique->get_material(); + obj.set_mesh(l, get_collection().get(n)); } -void Object::Loader::texture(const string &n) +void Object::Loader::technique_inline() { - if(obj.main_texture) - throw Exception("Only one main texture may be specified"); + RefPtr tech = new Technique; + if(coll) + load_sub(*tech, get_collection()); + else + load_sub(*tech); + obj.technique = tech; +} - Texture *tex=coll.get(n); - if(obj.technique) - obj.textures[obj.technique->get_texture_index("texture")]=tex; - obj.main_texture=tex; +void Object::Loader::technique(const std::string &n) +{ + obj.set_technique(get_collection().get(n)); } } // namespace GL