From e49bce7b401c1a3fa31823dcc4e3688eaf365f68 Mon Sep 17 00:00:00 2001 From: scrawl Date: Mon, 30 Nov 2015 01:39:41 +0100 Subject: [PATCH] Share the bvh of btBvhTriangleMeshShape's when possible Results in decent cell-loading speed up. (only affects bullet versions < 2.83, since we use btScaledBvhTriangleMeshShape for >=2.83) --- components/resource/bulletshape.cpp | 11 ++++++++++- components/resource/bulletshape.hpp | 4 ++-- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/components/resource/bulletshape.cpp b/components/resource/bulletshape.cpp index 00bcb9e046..968dbe6c67 100644 --- a/components/resource/bulletshape.cpp +++ b/components/resource/bulletshape.cpp @@ -62,7 +62,16 @@ btCollisionShape* BulletShape::duplicateCollisionShape(btCollisionShape *shape) // work around btScaledBvhTriangleMeshShape bug ( https://code.google.com/p/bullet/issues/detail?id=371 ) in older bullet versions btTriangleMesh* oldMesh = static_cast(trishape->getMeshInterface()); btTriangleMesh* newMesh = new btTriangleMesh(*oldMesh); - TriangleMeshShape* newShape = new TriangleMeshShape(newMesh, true); + + // Do not build a new bvh (not needed, since it's the same as the original shape's bvh) + bool buildBvh = true; + if (trishape->getOptimizedBvh()) + buildBvh = false; + TriangleMeshShape* newShape = new TriangleMeshShape(newMesh, true, buildBvh); + // Set original shape's bvh via pointer + // The pointer is safe because the BulletShapeInstance keeps a ref_ptr to the original BulletShape + if (!buildBvh) + newShape->setOptimizedBvh(trishape->getOptimizedBvh()); #endif return newShape; } diff --git a/components/resource/bulletshape.hpp b/components/resource/bulletshape.hpp index 78e509db71..cfae27eac4 100644 --- a/components/resource/bulletshape.hpp +++ b/components/resource/bulletshape.hpp @@ -60,8 +60,8 @@ namespace Resource // Subclass btBhvTriangleMeshShape to auto-delete the meshInterface struct TriangleMeshShape : public btBvhTriangleMeshShape { - TriangleMeshShape(btStridingMeshInterface* meshInterface, bool useQuantizedAabbCompression) - : btBvhTriangleMeshShape(meshInterface, useQuantizedAabbCompression) + TriangleMeshShape(btStridingMeshInterface* meshInterface, bool useQuantizedAabbCompression, bool buildBvh = true) + : btBvhTriangleMeshShape(meshInterface, useQuantizedAabbCompression, buildBvh) { }