X-Git-Url: http://git.tdb.fi/?p=libs%2Fgl.git;a=blobdiff_plain;f=source%2Fobject.cpp;h=1852f91b50222ead401dfa177605c31d9d1ee21b;hp=e406755c1e81e2e1d27e5f77658c19d5034c6b21;hb=71240e5c5ef7165313664ee9fe81df95c0eff10b;hpb=01525e40e4bc32b885196dd85ff68e876678d19d diff --git a/source/object.cpp b/source/object.cpp index e406755c..1852f91b 100644 --- a/source/object.cpp +++ b/source/object.cpp @@ -1,223 +1,264 @@ -/* $Id$ - -This file is part of libmspgl -Copyright © 2007 Mikko Rasa, Mikkosoft Productions -Distributed under the LGPL -*/ - -#include -#include "except.h" +#include +#include +#include "error.h" #include "material.h" #include "mesh.h" #include "object.h" #include "objectinstance.h" -#include "objectpass.h" #include "program.h" #include "programdata.h" -#include "texture.h" -#include "texunit.h" +#include "renderer.h" +#include "resourcemanager.h" +#include "technique.h" +#include "texturing.h" using namespace std; namespace Msp { namespace GL { +Matrix Object::identity_matrix; + Object::Object(): - meshes(1, static_cast(0)), - material(0) + lods(1), + lod0_watched(false) +{ } + +Object::Object(const Mesh *m, const Technique *t): + lods(1), + lod0_watched(false) { - normal_pass=&passes[""]; + set_mesh(m); + set_technique(t); } +// TODO should have copy-c'tor to set watch on lod0 mesh if necessary + Object::~Object() { - for(map::iterator i=passes.begin(); i!=passes.end(); ++i) - delete i->second.shdata; + if(lods[0].mesh && lod0_watched) + if(ResourceManager *rm = lods[0].mesh->get_manager()) + rm->unwatch_resource(*lods[0].mesh, *this); } -bool Object::has_pass(const string &pn) const +Object::LevelOfDetail &Object::get_lod(unsigned i, const char *caller) { - return passes.count(pn); -} + if(i>lods.size()) + throw out_of_range(caller); + if(i>0 && (!lods[0].mesh || !lods[0].technique)) + throw invalid_operation(caller); -const ObjectPass &Object::get_pass(const string &pn) const -{ - map::const_iterator i=passes.find(pn); - if(i==passes.end()) - throw KeyError("Unknown pass"); - return i->second; -} + if(i==lods.size()) + lods.push_back(lods.back()); -void Object::render(const ObjectInstance *inst) const -{ - render(*normal_pass, inst); + return lods[i]; } -void Object::render(const string &pn, const ObjectInstance *inst) const +void Object::set_mesh(unsigned i, const Mesh *m) { - render(get_pass(pn), inst); -} + RefPtr &ptr = get_lod(i, "Object::set_mesh").mesh; + if(i==0 && ptr && lod0_watched) + if(ResourceManager *rm = ptr->get_manager()) + rm->unwatch_resource(*ptr, *this); + ptr = m; + ptr.keep(); + lod0_watched = false; + + if(i==0 && m) + if(ResourceManager *rm = m->get_manager()) + { + rm->watch_resource(*m, *this); + lod0_watched = true; + } -void Object::render(const list &insts) const -{ - render(*normal_pass, insts); + update_bounding_sphere(); } -void Object::render(const string &pn, const list &insts) const +void Object::update_bounding_sphere() { - render(get_pass(pn), insts); -} + vector points; + for(vector::const_iterator i=lods.begin(); i!=lods.end(); ++i) + { + if(!i->mesh || !i->mesh->is_loaded()) + continue; -void Object::setup_render(const ObjectPass &pass) const -{ - if(!meshes[0]) - throw InvalidState("Trying to render Object without mesh"); + const VertexArray &vertices = i->mesh->get_vertices(); - if(pass.shprog) - { - pass.shprog->bind(); - pass.shdata->apply(); - for(unsigned i=0; ibind(); + offset = vertices.get_format().offset(VERTEX2); + three = false; + if(offset<0) + continue; + } + + unsigned n_vertices = vertices.size(); + points.reserve(points.size()+n_vertices); + for(unsigned j=0; jbind(); - if(material) - material->apply(); + /* Don't touch the bounding sphere if we had no vertices to avoid + overwriting a possible hint. */ + if(points.empty()) + return; + + bounding_sphere = Geometry::BoundingSphere::from_point_cloud(points.begin(), points.end()); } -void Object::finish_render(const ObjectPass &pass) const +const Mesh *Object::get_mesh(unsigned i) const { - if(pass.shprog) - Program::unbind(); - for(unsigned i=textures.size(); i--;) - { - TexUnit::activate(i); - Texture::unbind(); - } + if(i>=lods.size()) + return 0; + + return lods[i].mesh.get(); } -void Object::render(const ObjectPass &pass, const ObjectInstance *inst) const +void Object::set_technique(unsigned i, const Technique *t) { - setup_render(pass); + RefPtr &ptr = get_lod(i, "Object::set_technique").technique; + ptr = t; + ptr.keep(); +} - unsigned lod=0; - if(inst) - { - inst->setup_render(pass); - lod=min(inst->get_level_of_detail(), meshes.size()-1); - } +const Technique *Object::get_technique(unsigned i) const +{ + if(i>=lods.size()) + return 0; - meshes[lod]->draw(); + return lods[i].technique.get(); +} - if(inst) - inst->finish_render(pass); +void Object::render(Renderer &renderer, const Tag &tag) const +{ + const RenderPass *pass = get_pass(tag, 0); + if(!pass) + return; + + Renderer::Push push(renderer); + pass->apply(renderer); - finish_render(pass); + setup_render(renderer, tag); + lods.front().mesh->draw(renderer); + finish_render(renderer, tag); } -void Object::render(const ObjectPass &pass, const list &insts) const +void Object::render(Renderer &renderer, const ObjectInstance &inst, const Tag &tag) const { - setup_render(pass); - - for(list::const_iterator i=insts.begin(); i!=insts.end(); ++i) - { - (*i)->setup_render(pass); + unsigned lod = min(inst.get_level_of_detail(renderer), lods.size()-1); + const RenderPass *pass = get_pass(tag, lod); + if(!pass) + return; + + Renderer::Push push(renderer); + pass->apply(renderer); + + setup_render(renderer, tag); + inst.setup_render(renderer, tag); + lods[lod].mesh->draw(renderer); + inst.finish_render(renderer, tag); + finish_render(renderer, tag); +} - unsigned lod=min((*i)->get_level_of_detail(), meshes.size()-1); - meshes[lod]->draw(); +const RenderPass *Object::get_pass(const Tag &tag, unsigned lod) const +{ + const Technique *tech = lods[lod].technique.get(); + if(!tech) + throw logic_error("no technique"); + if(!tech->has_pass(tag)) + return 0; + return &tech->get_pass(tag); +} - (*i)->finish_render(pass); - } +void Object::resource_loaded(Resource &res) +{ + if(&res==lods.front().mesh.get() && bounding_sphere.is_empty()) + update_bounding_sphere(); +} - finish_render(pass); +void Object::resource_removed(Resource &res) +{ + if(&res==lods.front().mesh.get()) + lod0_watched = false; } +Object::Loader::Loader(Object &o): + LodLoader(o, 0, 0) +{ + init(); +} + Object::Loader::Loader(Object &o, Collection &c): - obj(o), - coll(c) + LodLoader(o, 0, &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("texture", &Loader::texture); + init(); } -Object::Loader::~Loader() +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)); - } + add("bounding_sphere_hint", &Loader::bounding_sphere_hint); + add("level_of_detail", &Loader::level_of_detail); } -void Object::Loader::lod_mesh(unsigned l, const string &n) +void Object::Loader::finish() { - obj.meshes.resize(l+1, 0); - obj.meshes[l]=coll.get(n); + obj.update_bounding_sphere(); } -void Object::Loader::material_inline() +void Object::Loader::bounding_sphere_hint(float x, float y, float z, float r) { - throw Exception("material_inline not supported yet"); - /*RefPtr mat=new Material; - load_sub(*mat); - coll.add(format("%p%p", &obj, mat.get()), mat.get()); - obj.material=mat.release();*/ + obj.bounding_sphere = Geometry::BoundingSphere(Vector3(x, y, z), r); } -void Object::Loader::mesh(const string &n) +void Object::Loader::level_of_detail(unsigned i) { - obj.meshes[0]=coll.get(n); + LodLoader ldr(obj, i, coll); + load_sub_with(ldr); } -void Object::Loader::pass(const string &n) + +Object::LodLoader::LodLoader(Object &o, unsigned i, Collection *c): + DataFile::CollectionObjectLoader(o, c), + index(i), + lod(obj.get_lod(index, "Object::LodLoader::LodLoader")) { - if(obj.passes.count(n)) - throw KeyError("Duplicate pass name"); - ObjectPass p; - load_sub(p, coll); - obj.passes[n]=p; + add("mesh", &LodLoader::mesh_inline); + add("mesh", &LodLoader::mesh); + add("technique", &LodLoader::technique_inline); + add("technique", &LodLoader::technique); } -void Object::Loader::shader(const string &n) +void Object::LodLoader::mesh(const string &n) { - Program *shprog=coll.get(n); - if(shprog) // Allow for unsupported shaders - { - RefPtr shdata=new ProgramData; - load_sub(*shdata, *shprog); + obj.set_mesh(index, &get_collection().get(n)); +} - obj.normal_pass->shprog=shprog; - if(obj.normal_pass->shdata) - delete obj.normal_pass->shdata; - obj.normal_pass->shdata=shdata.release(); - } +void Object::LodLoader::mesh_inline() +{ + RefPtr msh = new Mesh; + load_sub(*msh); + lod.mesh = msh; } -void Object::Loader::texture(const string &n) +void Object::LodLoader::technique(const std::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)); - } + obj.set_technique(index, &get_collection().get(n)); +} + +void Object::LodLoader::technique_inline() +{ + 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); + lod.technique = tech; } } // namespace GL