]> git.tdb.fi Git - libs/gl.git/blobdiff - source/object.cpp
Copy ProgramData::uniform_slots in copy constructor and operator=
[libs/gl.git] / source / object.cpp
index ae38af6b4e7f2c9274206561759bfdef4d442987..d48b0843fb9f3b73591588631429ce09e2709a8c 100644 (file)
@@ -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<Vector3> points;
+       for(vector<RefPtr<const Mesh> >::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<n_vertices; ++j)
+               {
+                       const float *v = vertices[j];
+                       points.push_back(Vector3(v[offset], v[offset+1], (three ? v[offset+2] : 0.0f)));
+               }
+       }
+
+       bounding_sphere = Geometry::BoundingSphere<float, 3>::from_point_cloud(points.begin(), points.end());
 }
 
 const Mesh *Object::get_mesh(unsigned i) const
@@ -110,6 +158,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<Object>(o, 0)
@@ -131,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()