]> git.tdb.fi Git - libs/gl.git/commitdiff
Add an interface for obtaining bounding spheres from renderables
authorMikko Rasa <tdb@tdb.fi>
Wed, 11 Sep 2013 07:53:41 +0000 (10:53 +0300)
committerMikko Rasa <tdb@tdb.fi>
Wed, 11 Sep 2013 07:53:41 +0000 (10:53 +0300)
This can be used for various forms of culling.

source/object.cpp
source/object.h
source/objectinstance.cpp
source/objectinstance.h
source/renderable.h

index ae38af6b4e7f2c9274206561759bfdef4d442987..d35517bf3adeb43773760cc2d6648d65f05a1c06 100644 (file)
@@ -41,6 +41,32 @@ void Object::set_mesh(unsigned i, const Mesh *m)
        meshes[i].keep();
 }
 
+void Object::update_bounding_sphere()
+{
+       vector<Vector3> points;
+       for(vector<RefPtr<const Mesh> >::const_iterator i=meshes.begin(); i!=meshes.end(); ++i)
+       {
+               const VertexArray &vertices = (*i)->get_vertices();
+               int offset = vertices.get_format().offset(VERTEX3);
+               if(offset<0)
+               {
+                       // TODO Handle two-dimensional meshes
+                       bounding_sphere = Geometry::BoundingSphere<float, 3>();
+                       return;
+               }
+
+               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], v[offset+2]));
+               }
+       }
+
+       bounding_sphere = Geometry::BoundingSphere<float, 3>::from_point_cloud(points.begin(), points.end());
+}
+
 const Mesh *Object::get_mesh(unsigned i) const
 {
        if(i>=meshes.size())
@@ -136,6 +162,11 @@ void Object::Loader::init()
        add("lod_mesh", &Loader::mesh_lod);
 }
 
+void Object::Loader::finish()
+{
+       obj.update_bounding_sphere();
+}
+
 void Object::Loader::mesh_inline()
 {
        RefPtr<Mesh> msh = new Mesh;
index 7820d29c366df0b1392471c3e8220ce7f8fe728d..82e12f14461f0b0b54d03fb3b83ab8b4a62c0c28 100644 (file)
@@ -33,8 +33,8 @@ public:
                Loader(Object &, Collection &);
        private:
                void init();
+               virtual void finish();
 
-       private:
                void mesh_inline();
                void mesh_inline_lod(unsigned);
                void mesh(const std::string &);
@@ -46,6 +46,7 @@ public:
 private:
        std::vector<RefPtr<const Mesh> > meshes;
        RefPtr<const Technique> technique;
+       Geometry::BoundingSphere<float, 3> bounding_sphere;
 
 public:
        Object();
@@ -54,10 +55,15 @@ public:
 
        void set_mesh(const Mesh *m) { set_mesh(0, m); }
        void set_mesh(unsigned, const Mesh *);
+private:
+       void update_bounding_sphere();
+public:
        const Mesh *get_mesh(unsigned = 0) const;
        void set_technique(const Technique *);
        const Technique *get_technique() const { return technique.get(); }
 
+       virtual const Geometry::BoundingSphere<float, 3> *get_bounding_sphere() const { return &bounding_sphere; }
+
        virtual void render(const Tag &tag = Tag()) const;
 
        virtual void render(Renderer &, const Tag & = Tag()) const;
index e3bb8f80b0e60f62c45b3926428d258737e52c20..b3fc133d4d78060e0aec5d01a4155f53e557d79c 100644 (file)
@@ -1,4 +1,3 @@
-#include "object.h"
 #include "objectinstance.h"
 #include "renderer.h"
 
index 6094517b6d5c062ca220b14e686e5dade8802335..6a8749766f39ca08223c07d8971435c3abe1eebb 100644 (file)
@@ -2,12 +2,12 @@
 #define MSP_GL_OBJETCINSTANCE_H_
 
 #include <string>
+#include "object.h"
 #include "renderable.h"
 
 namespace Msp {
 namespace GL {
 
-class Object;
 class ProgramData;
 
 /**
@@ -29,6 +29,8 @@ public:
        const Object &get_object() const { return object; }
        virtual long get_instance_key() const { return reinterpret_cast<long>(&object); }
 
+       virtual const Geometry::BoundingSphere<float, 3> *get_bounding_sphere() const { return object.get_bounding_sphere(); }
+
        virtual void render(const Tag &tag = Tag()) const;
        virtual void render(Renderer &, const Tag & = Tag()) const;
 
index b53fa9ac839307347bb614db7052dcfbe5a7a8cb..eae75cce642768f9988adcd853f27d253a695c18 100644 (file)
@@ -2,6 +2,7 @@
 #define MSP_GL_RENDERABLE_H_
 
 #include <string>
+#include <msp/geometry/boundingsphere.h>
 #include "tag.h"
 
 namespace Msp {
@@ -38,6 +39,11 @@ public:
        matrix exists. */
        virtual const Matrix *get_matrix() const { return 0; }
 
+       /** Returns a bounding sphere that completely encloses the Renderable.  The
+       bounding sphere is expressed in the renderable's coordinates.  Null is
+       returned if the bounding sphere cannot be determined. */
+       virtual const Geometry::BoundingSphere<float, 3> *get_bounding_sphere() const { return 0; }
+
        /** Called when starting to render a new frame. */
        virtual void setup_frame() const { }