X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Fobject.cpp;h=a27553a6236e029c27cfe547304b00e84371f5e5;hb=3b159edbe4e80a2bc19c4c2fcd42cb996b9fbfe0;hp=fe1690d267ee3e2a433d832dc388e72ae15f66e9;hpb=5b532dcb2a4a2d15d0b6607374284c979ece36a8;p=libs%2Fgl.git diff --git a/source/object.cpp b/source/object.cpp index fe1690d2..a27553a6 100644 --- a/source/object.cpp +++ b/source/object.cpp @@ -18,10 +18,13 @@ namespace Msp { namespace GL { Object::Object(): - lods(1) + lods(1), + lod0_watched(false) { } -Object::Object(const Mesh *m, const Technique *t) +Object::Object(const Mesh *m, const Technique *t): + lods(1), + lod0_watched(false) { set_mesh(m); set_technique(t); @@ -29,7 +32,7 @@ Object::Object(const Mesh *m, const Technique *t) Object::~Object() { - if(lods[0].mesh) + if(lods[0].mesh && lod0_watched) if(ResourceManager *rm = lods[0].mesh->get_manager()) rm->unwatch_resource(*lods[0].mesh, *this); } @@ -50,15 +53,19 @@ Object::LevelOfDetail &Object::get_lod(unsigned i, const char *caller) void Object::set_mesh(unsigned i, const Mesh *m) { RefPtr &ptr = get_lod(i, "Object::set_mesh").mesh; - if(i==0 && ptr) + 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; + } update_bounding_sphere(); } @@ -68,7 +75,7 @@ void Object::update_bounding_sphere() vector points; for(vector::const_iterator i=lods.begin(); i!=lods.end(); ++i) { - if(!i->mesh) + if(!i->mesh || !i->mesh->is_loaded()) continue; const VertexArray &vertices = i->mesh->get_vertices(); @@ -92,6 +99,11 @@ void Object::update_bounding_sphere() } } + /* 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()); } @@ -180,6 +192,12 @@ void Object::resource_loaded(Resource &res) update_bounding_sphere(); } +void Object::resource_removed(Resource &res) +{ + if(&res==lods.front().mesh.get()) + lod0_watched = false; +} + Object::Loader::Loader(Object &o): LodLoader(o, 0, 0) @@ -195,6 +213,7 @@ Object::Loader::Loader(Object &o, Collection &c): void Object::Loader::init() { + add("bounding_sphere_hint", &Loader::bounding_sphere_hint); add("level_of_detail", &Loader::level_of_detail); } @@ -203,6 +222,11 @@ void Object::Loader::finish() obj.update_bounding_sphere(); } +void Object::Loader::bounding_sphere_hint(float x, float y, float z, float r) +{ + obj.bounding_sphere = Geometry::BoundingSphere(Vector3(x, y, z), r); +} + void Object::Loader::level_of_detail(unsigned i) { LodLoader ldr(obj, i, coll);