]> git.tdb.fi Git - libs/gl.git/commitdiff
Allow bounding sphere hints for objects and export such from Blender
authorMikko Rasa <tdb@tdb.fi>
Wed, 10 Dec 2014 18:34:41 +0000 (20:34 +0200)
committerMikko Rasa <tdb@tdb.fi>
Wed, 10 Dec 2014 18:36:04 +0000 (20:36 +0200)
blender/io_mspgl/export_object.py
source/object.cpp
source/object.h

index b910add12fad3ed86ef397861de92fc8bb6a50b9..b911ed362bb242bc84f7f040a7ac25b90ac3e99a 100644 (file)
@@ -1,4 +1,5 @@
 import os
+import mathutils
 
 def linear_to_srgb(l):
        if l<0.0031308:
@@ -48,6 +49,18 @@ class ObjectExporter:
                from .outfile import open_output
                out_file = open_output(out_file)
 
+               p1 = max(((v.co, v.co.length) for v in obj.data.vertices), key=lambda x:x[1])[0]
+               p2 = max(((v.co, (v.co-p1).length) for v in obj.data.vertices), key=lambda x:x[1])[0]
+               center = (p1+p2)/2
+               radius = (p1-p2).length/2
+               for v in obj.data.vertices:
+                       d = v.co-center
+                       if d.length>radius:
+                               center += d*(1-radius/d.length)/2
+                               radius = (radius+d.length)/2
+
+               out_file.write("bounding_sphere_hint", center[0], center[1], center[2], radius)
+
                prev_mesh = None
                prev_tech = (None, None)
                for i, l in enumerate(lods):
index fe1690d267ee3e2a433d832dc388e72ae15f66e9..283894501e29a0aec9b3694d47b9730154d56c60 100644 (file)
@@ -68,7 +68,7 @@ void Object::update_bounding_sphere()
        vector<Vector3> points;
        for(vector<LevelOfDetail>::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 +92,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<float, 3>::from_point_cloud(points.begin(), points.end());
 }
 
@@ -195,6 +200,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 +209,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<float, 3>(Vector3(x, y, z), r);
+}
+
 void Object::Loader::level_of_detail(unsigned i)
 {
        LodLoader ldr(obj, i, coll);
index 192cf069bd8f4c8bf1b8a9ca801f5682e7fe4876..a11bfd9b654d0ffa98887fefcb2c886a360e1d98 100644 (file)
@@ -59,6 +59,7 @@ public:
                void init();
                virtual void finish();
 
+               void bounding_sphere_hint(float, float, float, float);
                void level_of_detail(unsigned);
        };