X-Git-Url: http://git.tdb.fi/?p=libs%2Fgl.git;a=blobdiff_plain;f=source%2Fobject.cpp;h=d573fdd4975695eddfd3d164eca0128d97936937;hp=0469c31a99629187f17ce7d5dde803823636bb2e;hb=f14435e58bfa0fa697a06ba9a454bb30cd37d9d8;hpb=85e83ace47e5a9a8ae7263886255dd81afc69278 diff --git a/source/object.cpp b/source/object.cpp index 0469c31a..d573fdd4 100644 --- a/source/object.cpp +++ b/source/object.cpp @@ -1,10 +1,5 @@ -/* $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" @@ -12,8 +7,9 @@ Distributed under the LGPL #include "objectinstance.h" #include "program.h" #include "programdata.h" -#include "texture.h" -#include "texunit.h" +#include "renderer.h" +#include "technique.h" +#include "texturing.h" using namespace std; @@ -21,121 +17,163 @@ namespace Msp { namespace GL { Object::Object(): - mesh(0), - shprog(0), - shdata(0), - material(0) + meshes(1) { } Object::~Object() { - delete shdata; } -void Object::render(const ObjectInstance *inst) const +void Object::set_mesh(unsigned i, const Mesh *m) { - setup_render(); + if(i>meshes.size()) + throw InvalidParameterValue("LODs must be continuous"); - if(inst) - inst->setup_render(); + if(i==meshes.size()) + meshes.push_back(m); + else + meshes[i] = m; + meshes[i].keep(); +} - mesh->draw(); +const Mesh *Object::get_mesh(unsigned i) const +{ + if(i>=meshes.size()) + return 0; - if(inst) - inst->finish_render(); + return meshes[i].get(); +} - finish_render(); +void Object::set_technique(const Technique *t) +{ + technique = t; + technique.keep(); } -void Object::render(const list &insts) const +void Object::render(const Tag &tag) const { - setup_render(); + const RenderPass *pass = get_pass(tag); + if(!pass) + return; - for(list::const_iterator i=insts.begin(); i!=insts.end(); ++i) - { - (*i)->setup_render(); + 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()); - mesh->draw(); + meshes.front()->draw(); +} - (*i)->finish_render(); - } +void Object::render(Renderer &renderer, const Tag &tag) const +{ + 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()); - finish_render(); + meshes.front()->draw(renderer); } -void Object::setup_render() const +void Object::render(Renderer &renderer, const ObjectInstance &inst, const Tag &tag) const { - if(!mesh) - throw InvalidState("Trying to render Object without mesh"); + 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()); + + 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); +} + +const RenderPass *Object::get_pass(const Tag &tag) const +{ + if(!technique || !technique->has_pass(tag)) + return 0; + return &technique->get_pass(tag); +} - if(shprog) - { - shprog->bind(); - shdata->apply(); - for(unsigned i=0; ibind(); - } - } - else if(!textures.empty()) - textures.front()->bind(); - if(material) - material->apply(); +Object::Loader::Loader(Object &o): + DataFile::CollectionObjectLoader(o, 0) +{ + init(); } -void Object::finish_render() const +Object::Loader::Loader(Object &o, Collection &c): + DataFile::CollectionObjectLoader(o, &c) { - if(shprog) - Program::unbind(); - for(unsigned i=0; i msh = new Mesh; + load_sub(*msh); + obj.meshes.front() = msh; +} + +void Object::Loader::mesh_inline_lod(unsigned l) { - add("material", &Object::material); - add("mesh", &Object::mesh); - add("shader", &Loader::shader); - add("texture", &Loader::texture); + if(l>obj.meshes.size()) + throw InvalidParameterValue("LODs must be continuous"); + + RefPtr msh = new Mesh; + load_sub(*msh); + if(l==obj.meshes.size()) + obj.meshes.push_back(msh); + else + obj.meshes[l] = msh; } -Object::Loader::~Loader() +void Object::Loader::mesh(const std::string &n) { - if(obj.shdata) - { - for(unsigned i=0; iuniform(obj.shprog->get_uniform_location(textures[i]), static_cast(i)); - } + obj.set_mesh(get_collection().get(n)); } -void Object::Loader::shader(const string &n) +void Object::Loader::mesh_lod(unsigned l, const string &n) { - obj.shprog=&coll.get(n); - if(!obj.shdata) - obj.shdata=new ProgramData; + obj.set_mesh(l, get_collection().get(n)); } -void Object::Loader::texture(const string &n) +void Object::Loader::technique_inline() { - 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; +} + +void Object::Loader::technique(const std::string &n) +{ + obj.set_technique(get_collection().get(n)); } } // namespace GL