From e2765175f4a083623c1a9549695140ca902ce0b2 Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Wed, 24 Sep 2014 20:34:24 +0300 Subject: [PATCH] Update Object's bounding sphere when mesh is loaded --- source/object.cpp | 22 +++++++++++++++++++++- source/object.h | 5 ++++- source/resource.h | 1 + 3 files changed, 26 insertions(+), 2 deletions(-) diff --git a/source/object.cpp b/source/object.cpp index 637af62a..efd84db8 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,17 @@ 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); } void Object::update_bounding_sphere() @@ -136,6 +150,12 @@ const RenderPass *Object::get_pass(const Tag &tag) const 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) diff --git a/source/object.h b/source/object.h index 82e12f14..f432177e 100644 --- a/source/object.h +++ b/source/object.h @@ -5,6 +5,7 @@ #include "bindable.h" #include "renderable.h" #include "renderpass.h" +#include "resourcewatcher.h" namespace Msp { namespace GL { @@ -23,7 +24,7 @@ In many cases, it's desirable to include multiple copies of an Object in a Scene, with different model matrices. ObjectInstances can be used to alter the rendering of an object on a per-instance basis. */ -class Object: public Renderable +class Object: public Renderable, private ResourceWatcher { public: class Loader: public DataFile::CollectionObjectLoader @@ -78,6 +79,8 @@ protected: private: const RenderPass *get_pass(const Tag &) const; + + virtual void resource_loaded(Resource &); }; } // namespace GL diff --git a/source/resource.h b/source/resource.h index b3bce17e..0ecba526 100644 --- a/source/resource.h +++ b/source/resource.h @@ -32,6 +32,7 @@ public: virtual ~Resource(); void set_manager(ResourceManager *); + ResourceManager *get_manager() const { return manager; } void *get_manager_data() const { return manager_data; } virtual AsyncLoader *load(IO::Seekable &) = 0; virtual UInt64 get_data_size() const = 0; -- 2.45.2