X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Fobject.cpp;h=9aba5466e3e1cf548674b61f2a4b70f156eb8a93;hb=922fac753e31d97fc88daa166e93e4c5572bd2ba;hp=3ba2d64c378862ea435f6d371cc4e5ec122a5579;hpb=c6aea1bc1586ffef132e6fffdf99343cb56617db;p=libs%2Fgl.git diff --git a/source/object.cpp b/source/object.cpp index 3ba2d64c..9aba5466 100644 --- a/source/object.cpp +++ b/source/object.cpp @@ -1,21 +1,15 @@ -/* $Id$ - -This file is part of libmspgl -Copyright © 2007 Mikko Rasa, Mikkosoft Productions -Distributed under the LGPL -*/ - -#include -#include "except.h" +#include +#include #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; @@ -23,197 +17,221 @@ namespace Msp { namespace GL { Object::Object(): - meshes(1, static_cast(0)), - material(0) + meshes(1) +{ } + +Object::Object(const Mesh *m, const Technique *t) { - normal_pass=&passes[0]; + set_mesh(m); + set_technique(t); } +// Avoid synthesizing ~RefPtr in files including object.h Object::~Object() { - for(map::iterator i=passes.begin(); i!=passes.end(); ++i) - delete i->second.shdata; + if(meshes[0]) + if(ResourceManager *rm = meshes[0]->get_manager()) + rm->unwatch_resource(*meshes[0], *this); } -bool Object::has_pass(const Tag &tag) const +void Object::set_mesh(unsigned i, const Mesh *m) { - return passes.count(tag.id); -} + if(i>meshes.size()) + throw out_of_range("Object::set_mesh"); -const ObjectPass &Object::get_pass(const Tag &tag) const -{ - map::const_iterator i=passes.find(tag.id); - if(i==passes.end()) - throw KeyError("Unknown pass"); - return i->second; -} + if(i==meshes.size()) + meshes.push_back(m); + else + { + if(i==0 && meshes[i]) + if(ResourceManager *rm = meshes[i]->get_manager()) + rm->unwatch_resource(*meshes[i], *this); + meshes[i] = m; + } + meshes[i].keep(); -void Object::render(const Tag &tag) const -{ - render(get_pass(tag), 0); -} + if(i==0 && m) + if(ResourceManager *rm = m->get_manager()) + rm->watch_resource(*m, *this); -void Object::render(const ObjectInstance &inst, const Tag &tag) const -{ - render(get_pass(tag), &inst); + update_bounding_sphere(); } -void Object::render(const list &insts, const Tag &tag) const +void Object::update_bounding_sphere() { - render(get_pass(tag), insts); -} + vector points; + for(vector >::const_iterator i=meshes.begin(); i!=meshes.end(); ++i) + { + const VertexArray &vertices = (*i)->get_vertices(); -void Object::setup_render(const ObjectPass &pass) const -{ - if(!meshes[0]) - throw InvalidState("Trying to render Object without mesh"); + int offset = vertices.get_format().offset(VERTEX3); + bool three = true; + if(offset<0) + { + offset = vertices.get_format().offset(VERTEX2); + three = false; + if(offset<0) + continue; + } - if(pass.shprog) - { - pass.shprog->bind(); - pass.shdata->apply(); - for(unsigned i=0; ibind(); + const float *v = vertices[j]; + points.push_back(Vector3(v[offset], v[offset+1], (three ? v[offset+2] : 0.0f))); } } - else if(!textures.empty()) - textures.front()->bind(); - if(material) - material->apply(); + 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>=meshes.size()) + return 0; + + return meshes[i].get(); } -void Object::render(const ObjectPass &pass, const ObjectInstance *inst) const +void Object::set_technique(const Technique *t) { - setup_render(pass); + technique = t; + technique.keep(); +} - unsigned lod=0; - if(inst) - { - inst->setup_render(pass); - lod=min(inst->get_level_of_detail(), meshes.size()-1); - } +void Object::render(const Tag &tag) const +{ + 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[lod]->draw(); + meshes.front()->draw(); +} + +void Object::render(Renderer &renderer, const Tag &tag) const +{ + const RenderPass *pass = get_pass(tag); + if(!pass) + return; - if(inst) - inst->finish_render(pass); + Renderer::Push push(renderer); + pass->apply(renderer); - finish_render(pass); + setup_render(renderer, tag); + meshes.front()->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); + const RenderPass *pass = get_pass(tag); + if(!pass) + return; - for(list::const_iterator i=insts.begin(); i!=insts.end(); ++i) - { - (*i)->setup_render(pass); + Renderer::Push push(renderer); + pass->apply(renderer); - unsigned lod=min((*i)->get_level_of_detail(), meshes.size()-1); - meshes[lod]->draw(); + setup_render(renderer, tag); + inst.setup_render(renderer, tag); + unsigned lod = min(inst.get_level_of_detail(renderer), meshes.size()-1); + meshes[lod]->draw(renderer); + inst.finish_render(renderer, tag); + finish_render(renderer, tag); +} - (*i)->finish_render(pass); - } +const RenderPass *Object::get_pass(const Tag &tag) const +{ + if(!technique) + throw logic_error("!technique"); + if(!technique->has_pass(tag)) + return 0; + return &technique->get_pass(tag); +} - finish_render(pass); +void Object::resource_loaded(Resource &res) +{ + if(!meshes.empty() && &res==meshes.front().get() && bounding_sphere.is_empty()) + update_bounding_sphere(); } -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("pass", &Loader::pass); - add("shader", &Loader::shader); - add("texture", &Loader::texture); + init(); } -Object::Loader::~Loader() +Object::Loader::Loader(Object &o, Collection &c): + DataFile::CollectionObjectLoader(o, &c) { - 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)); - } + 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); + 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); } -void Object::Loader::material_inline() +void Object::Loader::finish() { - 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.update_bounding_sphere(); } -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::pass(const string &n) +void Object::Loader::mesh_inline_lod(unsigned l) { - unsigned id=Tag(n).id; - if(obj.passes.count(id)) - throw KeyError("Duplicate pass name"); - ObjectPass p; - load_sub(p, coll); - obj.passes[id]=p; + if(l>obj.meshes.size()) + throw out_of_range("Object::Loader::mesh_inline_lod"); + + RefPtr msh = new Mesh; + load_sub(*msh); + if(l==obj.meshes.size()) + obj.meshes.push_back(msh); + else + obj.meshes[l] = msh; } -void Object::Loader::shader(const string &n) +void Object::Loader::mesh(const std::string &n) { - Program *shprog=coll.get(n); - if(shprog) // Allow for unsupported shaders - { - RefPtr shdata=new ProgramData; - load_sub(*shdata, *shprog); + obj.set_mesh(&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::Loader::mesh_lod(unsigned l, const string &n) +{ + 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