X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Fobject.cpp;h=d48b0843fb9f3b73591588631429ce09e2709a8c;hb=b152e4f63170e8ccd6c9fb9397964c628fb6efeb;hp=6bc872a198553eb38aa65ae67052b32ace758597;hpb=72790b3aba8ddc1a6d020646fb7312959729777b;p=libs%2Fgl.git diff --git a/source/object.cpp b/source/object.cpp index 6bc872a1..d48b0843 100644 --- a/source/object.cpp +++ b/source/object.cpp @@ -7,6 +7,7 @@ #include "program.h" #include "programdata.h" #include "renderer.h" +#include "resourcemanager.h" #include "technique.h" #include "texturing.h" @@ -27,7 +28,11 @@ Object::Object(const Mesh *m, const Technique *t) // Avoid synthesizing ~RefPtr in files including object.h Object::~Object() -{ } +{ + if(meshes[0]) + if(ResourceManager *rm = meshes[0]->get_manager()) + rm->unwatch_resource(*meshes[0], *this); +} void Object::set_mesh(unsigned i, const Mesh *m) { @@ -37,8 +42,51 @@ void Object::set_mesh(unsigned i, const Mesh *m) 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(); + + if(i==0 && m) + if(ResourceManager *rm = m->get_manager()) + rm->watch_resource(*m, *this); + + update_bounding_sphere(); +} + +void Object::update_bounding_sphere() +{ + vector points; + for(vector >::const_iterator i=meshes.begin(); i!=meshes.end(); ++i) + { + if(!*i) + continue; + + const VertexArray &vertices = (*i)->get_vertices(); + + 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; + } + + unsigned n_vertices = vertices.size(); + points.reserve(points.size()+n_vertices); + for(unsigned j=0; j::from_point_cloud(points.begin(), points.end()); } const Mesh *Object::get_mesh(unsigned i) const @@ -103,11 +151,19 @@ void Object::render(Renderer &renderer, const ObjectInstance &inst, const Tag &t const RenderPass *Object::get_pass(const Tag &tag) const { - if(!technique || !technique->has_pass(tag)) + if(!technique) + throw logic_error("!technique"); + if(!technique->has_pass(tag)) return 0; return &technique->get_pass(tag); } +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): DataFile::CollectionObjectLoader(o, 0) @@ -129,9 +185,11 @@ void Object::Loader::init() 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::finish() +{ + obj.update_bounding_sphere(); } void Object::Loader::mesh_inline()