From eeb78b8d4558cc48f03db347335fb4766c314a81 Mon Sep 17 00:00:00 2001 From: ejcoumans Date: Wed, 13 Feb 2008 07:14:19 +0000 Subject: [PATCH] free memory for btShapeHulls, keep track of it in GL_ShapeDrawer. move btShapeHull and btConvexHull into its own library in Extras/ConvexHull (it allocates memory using mem/delete and refactoring into using btAlignedAlloc/Free takes too much time) fix heightfield / btOptimizedBvh for quantization, so that raycast can use quantized aabb (clamp up for maxima and down for minima) work-in-progress (update projectfiles etc) --- Demos/AllBulletDemos/Main.cpp | 7 ++- Demos/CollisionDemo/CollisionDemo.cpp | 4 +- .../CollisionInterfaceDemo.cpp | 2 +- .../ContinuousConvexCollisionDemo.cpp | 16 +++--- .../ConvexDecompositionDemo.cpp | 2 +- Demos/ConvexDecompositionDemo/main.cpp | 54 +++++++++---------- .../DoublePrecisionDemo.cpp | 2 +- Demos/GimpactTestDemo/GimpactTestDemo.cpp | 2 +- .../LinearConvexCastDemo.cpp | 8 +-- .../MovingConcaveDemo/ConcavePhysicsDemo.cpp | 2 +- Demos/OpenGL/DebugCastResult.h | 6 +-- Demos/OpenGL/DemoApplication.cpp | 2 +- Demos/OpenGL/DemoApplication.h | 3 +- Demos/OpenGL/GL_ShapeDrawer.cpp | 29 +++++++++- Demos/OpenGL/GL_ShapeDrawer.h | 16 ++++-- Demos/SimplexDemo/SimplexDemo.cpp | 2 +- Demos/VehicleDemo/VehicleDemo.cpp | 2 +- .../ColladaConverter.cpp | 2 +- .../SpuGatheringCollisionTask.cpp | 4 +- Extras/CMakeLists.txt | 2 +- Extras/ConvexHull/CMakeLists.txt | 10 ++++ Extras/ConvexHull/Jamfile | 17 ++++++ .../ConvexHull}/btConvexHull.cpp | 44 +++++++++------ .../ConvexHull}/btConvexHull.h | 0 .../ConvexHull}/btShapeHull.cpp | 2 +- .../ConvexHull}/btShapeHull.h | 2 +- Extras/Jamfile | 1 + .../btHeightfieldTerrainShape.cpp | 18 +++---- .../btHeightfieldTerrainShape.h | 2 +- .../CollisionShapes/btOptimizedBvh.cpp | 24 ++++----- .../CollisionShapes/btOptimizedBvh.h | 18 +++---- 31 files changed, 188 insertions(+), 117 deletions(-) create mode 100644 Extras/ConvexHull/CMakeLists.txt create mode 100644 Extras/ConvexHull/Jamfile rename {src/LinearMath => Extras/ConvexHull}/btConvexHull.cpp (93%) rename {src/LinearMath => Extras/ConvexHull}/btConvexHull.h (100%) rename {src/BulletCollision/CollisionShapes => Extras/ConvexHull}/btShapeHull.cpp (96%) rename {src/BulletCollision/CollisionShapes => Extras/ConvexHull}/btShapeHull.h (93%) diff --git a/Demos/AllBulletDemos/Main.cpp b/Demos/AllBulletDemos/Main.cpp index 7f448a33c..801600cb4 100644 --- a/Demos/AllBulletDemos/Main.cpp +++ b/Demos/AllBulletDemos/Main.cpp @@ -28,7 +28,6 @@ #include "GLDebugDrawer.h" -static GLDebugDrawer gDebugDrawer; #include "LinearMath/btQuickprof.h" @@ -80,7 +79,7 @@ DemoApplication* CreatDemo(btDemoEntry* entry) btAssert(demo); if (demo->getDynamicsWorld()) { - demo->getDynamicsWorld()->setDebugDrawer(&gDebugDrawer); + demo->getDynamicsWorld()->setDebugDrawer(new GLDebugDrawer()); } #ifndef BT_NO_PROFILE @@ -168,6 +167,8 @@ void SimulationLoop() if (testSelection != testIndex) { testIndex = testSelection; + if (demo->getDynamicsWorld() && demo->getDynamicsWorld()->getDebugDrawer()) + delete demo->getDynamicsWorld()->getDebugDrawer(); delete demo; entry = g_demoEntries + testIndex; demo = CreatDemo(entry); @@ -192,6 +193,8 @@ void Keyboard(unsigned char key, int x, int y) // Press 'r' to reset. case 'r': + if (demo->getDynamicsWorld()->getDebugDrawer()) + delete demo->getDynamicsWorld()->getDebugDrawer(); delete demo; demo = CreatDemo(entry); Resize(width,height); diff --git a/Demos/CollisionDemo/CollisionDemo.cpp b/Demos/CollisionDemo/CollisionDemo.cpp index eff15196a..31f6ce0b1 100644 --- a/Demos/CollisionDemo/CollisionDemo.cpp +++ b/Demos/CollisionDemo/CollisionDemo.cpp @@ -232,7 +232,7 @@ void CollisionDemo::displayCallback(void) { tr[i].getOpenGLMatrix( m ); - GL_ShapeDrawer::drawOpenGL(m,shapePtr[i],btVector3(1,1,1),getDebugMode()); + m_shapeDrawer.drawOpenGL(m,shapePtr[i],btVector3(1,1,1),getDebugMode()); } @@ -248,7 +248,7 @@ void CollisionDemo::displayCallback(void) { btTransform ident; ident.setIdentity(); ident.getOpenGLMatrix(m); - GL_ShapeDrawer::drawOpenGL(m,&simplex,btVector3(1,1,1),getDebugMode()); + m_shapeDrawer.drawOpenGL(m,&simplex,btVector3(1,1,1),getDebugMode()); btQuaternion orn; diff --git a/Demos/CollisionInterfaceDemo/CollisionInterfaceDemo.cpp b/Demos/CollisionInterfaceDemo/CollisionInterfaceDemo.cpp index 756dd72b2..e65ced01d 100644 --- a/Demos/CollisionInterfaceDemo/CollisionInterfaceDemo.cpp +++ b/Demos/CollisionInterfaceDemo/CollisionInterfaceDemo.cpp @@ -164,7 +164,7 @@ void CollisionInterfaceDemo::displayCallback(void) { { objects[i].getWorldTransform().getOpenGLMatrix( m ); - GL_ShapeDrawer::drawOpenGL(m,objects[i].getCollisionShape(),btVector3(1,1,1),getDebugMode()); + m_shapeDrawer.drawOpenGL(m,objects[i].getCollisionShape(),btVector3(1,1,1),getDebugMode()); } diff --git a/Demos/ContinuousConvexCollision/ContinuousConvexCollisionDemo.cpp b/Demos/ContinuousConvexCollision/ContinuousConvexCollisionDemo.cpp index b6af8c93f..b2d8cf331 100644 --- a/Demos/ContinuousConvexCollision/ContinuousConvexCollisionDemo.cpp +++ b/Demos/ContinuousConvexCollision/ContinuousConvexCollisionDemo.cpp @@ -152,7 +152,7 @@ void btContinuousConvexCollisionDemo::displayCallback(void) { /*for (i=0;im_fraction,hitTrans); hitTrans.getOpenGLMatrix(m); - GL_ShapeDrawer::drawOpenGL(m,shapePtr[0],btVector3(0,1,0),getDebugMode()); + m_shapeDrawer.drawOpenGL(m,shapePtr[0],btVector3(0,1,0),getDebugMode()); btTransformUtil::integrateTransform(fromTrans[i],linVels[i],angVels[i],rayResultPtr->m_fraction,hitTrans); hitTrans.getOpenGLMatrix(m); - GL_ShapeDrawer::drawOpenGL(m,shapePtr[i],btVector3(0,1,1),getDebugMode()); + m_shapeDrawer.drawOpenGL(m,shapePtr[i],btVector3(0,1,1),getDebugMode()); } diff --git a/Demos/ConvexDecompositionDemo/ConvexDecompositionDemo.cpp b/Demos/ConvexDecompositionDemo/ConvexDecompositionDemo.cpp index 6ec0f46aa..817ab752e 100644 --- a/Demos/ConvexDecompositionDemo/ConvexDecompositionDemo.cpp +++ b/Demos/ConvexDecompositionDemo/ConvexDecompositionDemo.cpp @@ -21,7 +21,7 @@ subject to the following restrictions: #include "LinearMath/btQuickprof.h" #include "LinearMath/btIDebugDraw.h" #include "LinearMath/btGeometryUtil.h" -#include "BulletCollision/CollisionShapes/btShapeHull.h" +#include "btShapeHull.h" //#define USE_PARALLEL_DISPATCHER 1 #ifdef USE_PARALLEL_DISPATCHER diff --git a/Demos/ConvexDecompositionDemo/main.cpp b/Demos/ConvexDecompositionDemo/main.cpp index e15b8544c..fd6ae8a61 100644 --- a/Demos/ConvexDecompositionDemo/main.cpp +++ b/Demos/ConvexDecompositionDemo/main.cpp @@ -1,28 +1,28 @@ - - -#include "ConvexDecompositionDemo.h" -#include "GlutStuff.h" + + +#include "ConvexDecompositionDemo.h" +#include "GlutStuff.h" #include "GLDebugDrawer.h" -#include "btBulletDynamicsCommon.h" - -GLDebugDrawer gDebugDrawer; - -int main(int argc,char** argv) -{ - const char* filename = "file.obj"; - - - ConvexDecompositionDemo* convexDecompDemo = new ConvexDecompositionDemo(); - - convexDecompDemo->initPhysics(filename); - - convexDecompDemo->getDynamicsWorld()->setDebugDrawer(&gDebugDrawer); - - - convexDecompDemo->clientResetScene(); - - - - return glutmain(argc, argv,640,480,"Bullet Physics Demo. http://www.continuousphysics.com/Bullet/phpBB2/",convexDecompDemo); -} - +#include "btBulletDynamicsCommon.h" + +GLDebugDrawer gDebugDrawer; + +int main(int argc,char** argv) +{ + const char* filename = "file.obj"; + + + ConvexDecompositionDemo* convexDecompDemo = new ConvexDecompositionDemo(); + + convexDecompDemo->initPhysics(filename); + + convexDecompDemo->getDynamicsWorld()->setDebugDrawer(&gDebugDrawer); + + + convexDecompDemo->clientResetScene(); + + + + return glutmain(argc, argv,640,480,"Bullet Physics Demo. http://www.continuousphysics.com/Bullet/phpBB2/",convexDecompDemo); +} + diff --git a/Demos/DoublePrecisionDemo/DoublePrecisionDemo.cpp b/Demos/DoublePrecisionDemo/DoublePrecisionDemo.cpp index 48224e526..46fea51f4 100644 --- a/Demos/DoublePrecisionDemo/DoublePrecisionDemo.cpp +++ b/Demos/DoublePrecisionDemo/DoublePrecisionDemo.cpp @@ -169,7 +169,7 @@ void DoublePrecisionDemo::displayCallback(void) temp = objects[i].getWorldTransform(); temp.setOrigin(temp.getOrigin() - m_cameraPosition); temp.getOpenGLMatrix( m ); - GL_ShapeDrawer::drawOpenGL(m,objects[i].getCollisionShape(),color,getDebugMode()); + m_shapeDrawer.drawOpenGL(m,objects[i].getCollisionShape(),color,getDebugMode()); } objects[1].getWorldTransform().setOrigin(objects[1].getWorldTransform().getOrigin()+btVector3(-VERY_SMALL_INCREMENT,-VERY_SMALL_INCREMENT,0)); diff --git a/Demos/GimpactTestDemo/GimpactTestDemo.cpp b/Demos/GimpactTestDemo/GimpactTestDemo.cpp index c6a07fea9..adee40fd8 100644 --- a/Demos/GimpactTestDemo/GimpactTestDemo.cpp +++ b/Demos/GimpactTestDemo/GimpactTestDemo.cpp @@ -152,7 +152,7 @@ void GimpactConcaveDemo::renderme() } } - GL_ShapeDrawer::drawOpenGL(m,colObj->getCollisionShape(),wireColor,getDebugMode()); + m_shapeDrawer.drawOpenGL(m,colObj->getCollisionShape(),wireColor,getDebugMode()); } diff --git a/Demos/GjkConvexCastDemo/LinearConvexCastDemo.cpp b/Demos/GjkConvexCastDemo/LinearConvexCastDemo.cpp index 82c88ed64..1f2495e9a 100644 --- a/Demos/GjkConvexCastDemo/LinearConvexCastDemo.cpp +++ b/Demos/GjkConvexCastDemo/LinearConvexCastDemo.cpp @@ -137,8 +137,8 @@ void LinearConvexCastDemo::displayCallback(void) tr[ 0 ].getOpenGLMatrix( m1 ); tr[ 1 ].getOpenGLMatrix( m2 ); - GL_ShapeDrawer::drawOpenGL( m1, shapePtr[ 0 ], btVector3( 1, 0, 0 ), getDebugMode() ); - GL_ShapeDrawer::drawOpenGL( m2, shapePtr[ 1 ], btVector3( 1, 0, 0 ), getDebugMode() ); + m_shapeDrawer.drawOpenGL( m1, shapePtr[ 0 ], btVector3( 1, 0, 0 ), getDebugMode() ); + m_shapeDrawer.drawOpenGL( m2, shapePtr[ 1 ], btVector3( 1, 0, 0 ), getDebugMode() ); btVector3 originA, originB; originA.setInterpolate3( tr[ 0 ].getOrigin(), toA.getOrigin(), result.m_fraction ); @@ -153,8 +153,8 @@ void LinearConvexCastDemo::displayCallback(void) A.getOpenGLMatrix( m1 ); B.getOpenGLMatrix( m2 ); - GL_ShapeDrawer::drawOpenGL( m1, shapePtr[ 0 ], btVector3( 1, 1, 0 ), getDebugMode() ); - GL_ShapeDrawer::drawOpenGL( m2, shapePtr[ 1 ], btVector3( 1, 1, 0 ), getDebugMode() ); + m_shapeDrawer.drawOpenGL( m1, shapePtr[ 0 ], btVector3( 1, 1, 0 ), getDebugMode() ); + m_shapeDrawer.drawOpenGL( m2, shapePtr[ 1 ], btVector3( 1, 1, 0 ), getDebugMode() ); glFlush(); glutSwapBuffers(); diff --git a/Demos/MovingConcaveDemo/ConcavePhysicsDemo.cpp b/Demos/MovingConcaveDemo/ConcavePhysicsDemo.cpp index 6b53ebc75..5b7d38117 100644 --- a/Demos/MovingConcaveDemo/ConcavePhysicsDemo.cpp +++ b/Demos/MovingConcaveDemo/ConcavePhysicsDemo.cpp @@ -1535,7 +1535,7 @@ void ConcaveDemo::renderme() } } - GL_ShapeDrawer::drawOpenGL(m,colObj->getCollisionShape(),wireColor,getDebugMode()); + m_shapeDrawer.drawOpenGL(m,colObj->getCollisionShape(),wireColor,getDebugMode()); } diff --git a/Demos/OpenGL/DebugCastResult.h b/Demos/OpenGL/DebugCastResult.h index 05ab92cb5..fc2ba5662 100644 --- a/Demos/OpenGL/DebugCastResult.h +++ b/Demos/OpenGL/DebugCastResult.h @@ -37,9 +37,10 @@ struct btDebugCastResult : public btConvexCast::CastResult const btPolyhedralConvexShape* m_shape; btVector3 m_linVel; btVector3 m_angVel; + GL_ShapeDrawer* m_shapeDrawer; btDebugCastResult(const btTransform& fromTrans,const btPolyhedralConvexShape* shape, - const btVector3& linVel,const btVector3& angVel) + const btVector3& linVel,const btVector3& angVel,GL_ShapeDrawer* drawer) :m_fromTrans(fromTrans), m_shape(shape), m_linVel(linVel), @@ -74,8 +75,7 @@ struct btDebugCastResult : public btConvexCast::CastResult btTransform hitTrans; btTransformUtil::integrateTransform(m_fromTrans,m_linVel,m_angVel,fraction,hitTrans); hitTrans.getOpenGLMatrix(m); - GL_ShapeDrawer::drawOpenGL(m,m_shape,btVector3(1,0,0),btIDebugDraw::DBG_NoDebug); - + m_shapeDrawer->drawOpenGL(m,m_shape,btVector3(1,0,0),btIDebugDraw::DBG_NoDebug); } }; diff --git a/Demos/OpenGL/DemoApplication.cpp b/Demos/OpenGL/DemoApplication.cpp index e0ef07c7a..372239750 100644 --- a/Demos/OpenGL/DemoApplication.cpp +++ b/Demos/OpenGL/DemoApplication.cpp @@ -927,7 +927,7 @@ void DemoApplication::renderme() } } - GL_ShapeDrawer::drawOpenGL(m,colObj->getCollisionShape(),wireColor,getDebugMode()); + m_shapeDrawer.drawOpenGL(m,colObj->getCollisionShape(),wireColor,getDebugMode()); } diff --git a/Demos/OpenGL/DemoApplication.h b/Demos/OpenGL/DemoApplication.h index d6d22d286..04d1129f7 100644 --- a/Demos/OpenGL/DemoApplication.h +++ b/Demos/OpenGL/DemoApplication.h @@ -18,6 +18,7 @@ subject to the following restrictions: #include "GlutStuff.h" +#include "GL_ShapeDrawer.h" #include #include @@ -37,7 +38,6 @@ class btTypedConstraint; - class DemoApplication { void displayProfileString(int xOffset,int yStart,char* message); @@ -81,6 +81,7 @@ class DemoApplication void showProfileInfo(float& xOffset,float& yStart, float yIncr); + GL_ShapeDrawer m_shapeDrawer; public: diff --git a/Demos/OpenGL/GL_ShapeDrawer.cpp b/Demos/OpenGL/GL_ShapeDrawer.cpp index df14dca1f..b44d640d7 100644 --- a/Demos/OpenGL/GL_ShapeDrawer.cpp +++ b/Demos/OpenGL/GL_ShapeDrawer.cpp @@ -40,7 +40,8 @@ subject to the following restrictions: #include "BulletCollision/CollisionShapes/btConvexTriangleMeshShape.h" #include "BulletCollision/CollisionShapes/btUniformScalingShape.h" #include "BulletCollision/CollisionShapes/btStaticPlaneShape.h" -#include "BulletCollision/CollisionShapes/btShapeHull.h" +/// +#include "btShapeHull.h" #include "LinearMath/btTransformUtil.h" @@ -470,7 +471,12 @@ void GL_ShapeDrawer::drawOpenGL(btScalar* m, const btCollisionShape* shape, cons if (!shape->getUserPointer()) { //create a hull approximation - btShapeHull* hull = new btShapeHull(convexShape); + void* mem = btAlignedAlloc(sizeof(btShapeHull),16); + btShapeHull* hull = new(mem) btShapeHull(convexShape); + + ///cleanup memory + m_shapeHulls.push_back(hull); + btScalar margin = shape->getMargin(); hull->buildHull(margin); convexShape->setUserPointer(hull); @@ -655,3 +661,22 @@ void GL_ShapeDrawer::drawOpenGL(btScalar* m, const btCollisionShape* shape, cons glPopMatrix(); } + + +GL_ShapeDrawer::GL_ShapeDrawer() +{ +} + +GL_ShapeDrawer::~GL_ShapeDrawer() +{ + int i; + for (i=0;i~btShapeHull(); + btAlignedFree(hull); + m_shapeHulls[i] = 0; + } + m_shapeHulls.clear(); +} + diff --git a/Demos/OpenGL/GL_ShapeDrawer.h b/Demos/OpenGL/GL_ShapeDrawer.h index 84315d40b..151e4233c 100644 --- a/Demos/OpenGL/GL_ShapeDrawer.h +++ b/Demos/OpenGL/GL_ShapeDrawer.h @@ -16,16 +16,26 @@ subject to the following restrictions: #define GL_SHAPE_DRAWER_H class btCollisionShape; +class btShapeHull; +#include "LinearMath/btAlignedObjectArray.h" #include "LinearMath/btVector3.h" /// OpenGL shape drawing class GL_ShapeDrawer { - public: + //clean-up memory of dynamically created shape hulls + btAlignedObjectArray m_shapeHulls; - static void drawOpenGL(btScalar* m, const btCollisionShape* shape, const btVector3& color,int debugMode); - static void drawCoordSystem(); + public: + GL_ShapeDrawer(); + + virtual ~GL_ShapeDrawer(); + + ///drawOpenGL might allocate temporary memoty, stores pointer in shape userpointer + void drawOpenGL(btScalar* m, const btCollisionShape* shape, const btVector3& color,int debugMode); + static void drawCylinder(float radius,float halfHeight, int upAxis); + static void drawCoordSystem(); }; void OGL_displaylist_register_shape(btCollisionShape * shape); diff --git a/Demos/SimplexDemo/SimplexDemo.cpp b/Demos/SimplexDemo/SimplexDemo.cpp index 2551eb6d5..907f2d265 100644 --- a/Demos/SimplexDemo/SimplexDemo.cpp +++ b/Demos/SimplexDemo/SimplexDemo.cpp @@ -88,7 +88,7 @@ void SimplexDemo::displayCallback() transA.getOpenGLMatrix( m ); /// draw the simplex - GL_ShapeDrawer::drawOpenGL(m,shapePtr[i],btVector3(1,1,1),getDebugMode()); + m_shapeDrawer.drawOpenGL(m,shapePtr[i],btVector3(1,1,1),getDebugMode()); /// calculate closest point from simplex to the origin, and draw this vector simplex.calcClosest(m); diff --git a/Demos/VehicleDemo/VehicleDemo.cpp b/Demos/VehicleDemo/VehicleDemo.cpp index e311d2fcd..f635251c0 100644 --- a/Demos/VehicleDemo/VehicleDemo.cpp +++ b/Demos/VehicleDemo/VehicleDemo.cpp @@ -409,7 +409,7 @@ void VehicleDemo::renderme() m_vehicle->updateWheelTransform(i,true); //draw wheels (cylinders) m_vehicle->getWheelInfo(i).m_worldTransform.getOpenGLMatrix(m); - GL_ShapeDrawer::drawOpenGL(m,&wheelShape,wheelColor,getDebugMode()); + m_shapeDrawer.drawOpenGL(m,&wheelShape,wheelColor,getDebugMode()); } diff --git a/Extras/BulletColladaConverter/ColladaConverter.cpp b/Extras/BulletColladaConverter/ColladaConverter.cpp index 717105fd8..f36bb08fd 100644 --- a/Extras/BulletColladaConverter/ColladaConverter.cpp +++ b/Extras/BulletColladaConverter/ColladaConverter.cpp @@ -30,12 +30,12 @@ subject to the following restrictions: #include "dom/domCOLLADA.h" #include "dae/domAny.h" #include "dom/domConstants.h" +#include "btShapeHull.h" #include "BulletCollision/CollisionShapes/btBoxShape.h" #include "BulletCollision/CollisionShapes/btSphereShape.h" #include "BulletCollision/CollisionShapes/btCylinderShape.h" #include "BulletCollision/CollisionShapes/btConeShape.h" -#include "BulletCollision/CollisionShapes/btShapeHull.h" #include "BulletCollision/CollisionShapes/btStaticPlaneShape.h" #include "BulletCollision/CollisionShapes/btConvexHullShape.h" #include "BulletCollision/CollisionShapes/btTriangleMesh.h" diff --git a/Extras/BulletMultiThreaded/SpuNarrowPhaseCollisionTask/SpuGatheringCollisionTask.cpp b/Extras/BulletMultiThreaded/SpuNarrowPhaseCollisionTask/SpuGatheringCollisionTask.cpp index 0dc3b2bb8..4b293b2d6 100644 --- a/Extras/BulletMultiThreaded/SpuNarrowPhaseCollisionTask/SpuGatheringCollisionTask.cpp +++ b/Extras/BulletMultiThreaded/SpuNarrowPhaseCollisionTask/SpuGatheringCollisionTask.cpp @@ -363,8 +363,8 @@ void ProcessConvexConcaveSpuCollision(SpuCollisionPairInput* wuInput, CollisionT ///quantize query AABB unsigned short int quantizedQueryAabbMin[3]; unsigned short int quantizedQueryAabbMax[3]; - lsMemPtr->bvhShapeData.getOptimizedBvh()->quantizeWithClamp(quantizedQueryAabbMin,aabbMin); - lsMemPtr->bvhShapeData.getOptimizedBvh()->quantizeWithClamp(quantizedQueryAabbMax,aabbMax); + lsMemPtr->bvhShapeData.getOptimizedBvh()->quantizeWithClamp(quantizedQueryAabbMin,aabbMin,0); + lsMemPtr->bvhShapeData.getOptimizedBvh()->quantizeWithClamp(quantizedQueryAabbMax,aabbMax,1); QuantizedNodeArray& nodeArray = lsMemPtr->bvhShapeData.getOptimizedBvh()->getQuantizedNodeArray(); //spu_printf("SPU: numNodes = %d\n",nodeArray.size()); diff --git a/Extras/CMakeLists.txt b/Extras/CMakeLists.txt index ae0d3cddc..cfdbc4779 100644 --- a/Extras/CMakeLists.txt +++ b/Extras/CMakeLists.txt @@ -1 +1 @@ -SUBDIRS( glui ConvexDecomposition BulletMultiThreaded BulletColladaConverter LibXML COLLADA_DOM GIMPACTUtils GIMPACT ) +SUBDIRS( glui ConvexDecomposition ConvexHull BulletMultiThreaded BulletColladaConverter LibXML COLLADA_DOM GIMPACTUtils GIMPACT ) diff --git a/Extras/ConvexHull/CMakeLists.txt b/Extras/ConvexHull/CMakeLists.txt new file mode 100644 index 000000000..56a0517fa --- /dev/null +++ b/Extras/ConvexHull/CMakeLists.txt @@ -0,0 +1,10 @@ +INCLUDE_DIRECTORIES( +${BULLET_PHYSICS_SOURCE_DIR}/src ${BULLET_PHYSICS_SOURCE_DIR}/Extras/ConvexHull +) + +ADD_LIBRARY(LibConvexHull +btConvexHull.h +btConvexHull.cpp +btShapeHull.cpp +btShapeHull.h +) diff --git a/Extras/ConvexHull/Jamfile b/Extras/ConvexHull/Jamfile new file mode 100644 index 000000000..2869eb9cf --- /dev/null +++ b/Extras/ConvexHull/Jamfile @@ -0,0 +1,17 @@ +SubDir TOP Extras ConvexHull ; + + +Library convexhull : [ Wildcard *.h *.cpp ] : noinstall ; +#internal header path to compile convexhull +CFlags convexhull : [ FIncludes $(TOP)/Extras/ConvexHull ] ; + +#expose header include path for apps that depend on ConvexHull +convexhull.CFLAGS = [ FIncludes $(TOP)/Extras/ConvexHull ] ; +#same for msvcgen +MsvcGenConfig convexhull.INCDIRS : $(TOP)/Extras/ConvexHull ; + +#for the include paths +LibDepends convexhull : bulletcollision ; + + +#InstallHeader [ Wildcard *.h ] : ConvexHull ; diff --git a/src/LinearMath/btConvexHull.cpp b/Extras/ConvexHull/btConvexHull.cpp similarity index 93% rename from src/LinearMath/btConvexHull.cpp rename to Extras/ConvexHull/btConvexHull.cpp index a7558a6c9..56773a5ec 100644 --- a/src/LinearMath/btConvexHull.cpp +++ b/Extras/ConvexHull/btConvexHull.cpp @@ -196,6 +196,16 @@ class ConvexH HalfEdge(){} HalfEdge(short _ea,unsigned char _v, unsigned char _p):ea(_ea),v(_v),p(_p){} }; + ConvexH() + { + int i; + i=0; + } + ~ConvexH() + { + int i; + i=0; + } btAlignedObjectArray vertices; btAlignedObjectArray edges; btAlignedObjectArray facets; @@ -415,9 +425,9 @@ ConvexH *ConvexHCrop(ConvexH &convex,const Plane &slice) int i; int vertcountunder=0; int vertcountover =0; - static btAlignedObjectArray vertscoplanar; // existing vertex members of convex that are coplanar + btAlignedObjectArray vertscoplanar; // existing vertex members of convex that are coplanar vertscoplanar.resize(0); - static btAlignedObjectArray edgesplit; // existing edges that members of convex that cross the splitplane + btAlignedObjectArray edgesplit; // existing edges that members of convex that cross the splitplane edgesplit.resize(0); assert(convex.edges.size()<480); @@ -1164,7 +1174,7 @@ int calchull(btVector3 *verts,int verts_count, int *&tris_out, int &tris_count,i } } tris_count = ts.size()/3; - tris_out = (int*)btAlignedAlloc(sizeof(int)*ts.size(),16); + tris_out = (int*)new int[ts.size()]; for (i=0;ifacets.size()+c->edges.size()),16); // new int[1+c->facets.size()+c->edges.size()]; + faces_out = (int*)new int[(1+c->facets.size()+c->edges.size())]; // new int[1+c->facets.size()+c->edges.size()]; faces_count_out=0; i=0; faces_out[faces_count_out++]=-1; @@ -1273,7 +1283,7 @@ void ReleaseHull(PHullResult &result) { if ( result.mIndices ) { - btAlignedFree(result.mIndices); + delete[]result.mIndices; } result.mVcount = 0; @@ -1307,7 +1317,7 @@ HullError HullLibrary::CreateConvexHull(const HullDesc &desc, // unsigned int vcount = desc.mVcount; if ( vcount < 8 ) vcount = 8; - btVector3* vsource = (btVector3*) btAlignedAlloc (sizeof(btVector3)*vcount,16); + btVector3* vsource = new btVector3[vcount]; btVector3 scale; @@ -1336,7 +1346,7 @@ HullError HullLibrary::CreateConvexHull(const HullDesc &desc, // { // re-index triangle mesh so it refers to only used vertices, rebuild a new vertex table. - btVector3 *vscratch = (btVector3 *) btAlignedAlloc( sizeof(btVector3)*hr.mVcount,16); + btVector3 *vscratch = new btVector3[hr.mVcount]; BringOutYourDead(hr.mVertices,hr.mVcount, vscratch, ovcount, hr.mIndices, hr.mIndexCount ); ret = QE_OK; @@ -1345,11 +1355,11 @@ HullError HullLibrary::CreateConvexHull(const HullDesc &desc, // { result.mPolygons = false; result.mNumOutputVertices = ovcount; - result.mOutputVertices = (btVector3 *)btAlignedAlloc( sizeof(btVector3)*ovcount,16); + result.mOutputVertices = new btVector3[ovcount];; result.mNumFaces = hr.mFaceCount; result.mNumIndices = hr.mIndexCount; - result.mIndices = (unsigned int *) btAlignedAlloc( sizeof(unsigned int)*hr.mIndexCount,16); + result.mIndices = new unsigned int[hr.mIndexCount]; memcpy(result.mOutputVertices, vscratch, sizeof(btVector3)*ovcount ); @@ -1378,10 +1388,10 @@ HullError HullLibrary::CreateConvexHull(const HullDesc &desc, // { result.mPolygons = true; result.mNumOutputVertices = ovcount; - result.mOutputVertices = (btVector3 *)btAlignedAlloc( sizeof(btVector3)*ovcount,16); + result.mOutputVertices = new btVector3[ovcount]; result.mNumFaces = hr.mFaceCount; result.mNumIndices = hr.mIndexCount+hr.mFaceCount; - result.mIndices = (unsigned int *) btAlignedAlloc( sizeof(unsigned int)*result.mNumIndices,16); + result.mIndices = new unsigned int[result.mNumIndices]; memcpy(result.mOutputVertices, vscratch, sizeof(btVector3)*ovcount ); if ( 1 ) @@ -1412,14 +1422,14 @@ HullError HullLibrary::CreateConvexHull(const HullDesc &desc, // ReleaseHull(hr); if ( vscratch ) { - btAlignedFree(vscratch); + delete[] vscratch; } } } if ( vsource ) { - btAlignedFree(vsource); + delete[] vsource; } @@ -1432,12 +1442,12 @@ HullError HullLibrary::ReleaseResult(HullResult &result) // release memory alloc { if ( result.mOutputVertices ) { - btAlignedFree(result.mOutputVertices); + delete[] result.mOutputVertices; result.mOutputVertices = 0; } if ( result.mIndices ) { - btAlignedFree(result.mIndices); + delete[] result.mIndices; result.mIndices = 0; } return QE_OK; @@ -1724,7 +1734,7 @@ bool HullLibrary::CleanupVertices(unsigned int svcount, void HullLibrary::BringOutYourDead(const btVector3* verts,unsigned int vcount, btVector3* overts,unsigned int &ocount,unsigned int *indices,unsigned indexcount) { - unsigned int *used = (unsigned int *)btAlignedAlloc(sizeof(unsigned int)*vcount,16); + unsigned int *used = new unsigned int[vcount]; memset(used,0,sizeof(unsigned int)*vcount); ocount = 0; @@ -1756,5 +1766,5 @@ void HullLibrary::BringOutYourDead(const btVector3* verts,unsigned int vcount, b } } - btAlignedFree(used); + delete[] used; } diff --git a/src/LinearMath/btConvexHull.h b/Extras/ConvexHull/btConvexHull.h similarity index 100% rename from src/LinearMath/btConvexHull.h rename to Extras/ConvexHull/btConvexHull.h diff --git a/src/BulletCollision/CollisionShapes/btShapeHull.cpp b/Extras/ConvexHull/btShapeHull.cpp similarity index 96% rename from src/BulletCollision/CollisionShapes/btShapeHull.cpp rename to Extras/ConvexHull/btShapeHull.cpp index 2fc8e3282..ff128c84e 100644 --- a/src/BulletCollision/CollisionShapes/btShapeHull.cpp +++ b/Extras/ConvexHull/btShapeHull.cpp @@ -16,7 +16,7 @@ subject to the following restrictions: */ #include "btShapeHull.h" -#include "LinearMath/btConvexHull.h" +#include "btConvexHull.h" #define NUM_UNITSPHERE_POINTS 42 diff --git a/src/BulletCollision/CollisionShapes/btShapeHull.h b/Extras/ConvexHull/btShapeHull.h similarity index 93% rename from src/BulletCollision/CollisionShapes/btShapeHull.h rename to Extras/ConvexHull/btShapeHull.h index b8b95ba22..4084a332a 100644 --- a/src/BulletCollision/CollisionShapes/btShapeHull.h +++ b/Extras/ConvexHull/btShapeHull.h @@ -19,7 +19,7 @@ subject to the following restrictions: #define _SHAPE_HULL_H #include "LinearMath/btAlignedObjectArray.h" -#include "btConvexShape.h" +#include "BulletCollision/CollisionShapes/btConvexShape.h" ///btShapeHull takes a btConvexShape, builds the convex hull using btConvexHull and provides triangle indices and vertices. diff --git a/Extras/Jamfile b/Extras/Jamfile index e9bfe591d..81addb143 100644 --- a/Extras/Jamfile +++ b/Extras/Jamfile @@ -3,6 +3,7 @@ SubDir TOP Extras ; SubInclude TOP Extras ConvexDecomposition ; SubInclude TOP Extras COLLADA_DOM ; SubInclude TOP Extras glui ; +SubInclude TOP Extras ConvexHull ; SubInclude TOP Extras LibXML ; SubInclude TOP Extras BulletColladaConverter ; SubInclude TOP Extras BulletMultiThreaded ; diff --git a/src/BulletCollision/CollisionShapes/btHeightfieldTerrainShape.cpp b/src/BulletCollision/CollisionShapes/btHeightfieldTerrainShape.cpp index 872041869..855ed6eeb 100644 --- a/src/BulletCollision/CollisionShapes/btHeightfieldTerrainShape.cpp +++ b/src/BulletCollision/CollisionShapes/btHeightfieldTerrainShape.cpp @@ -182,20 +182,16 @@ void btHeightfieldTerrainShape::getVertex(int x,int y,btVector3& vertex) const } -void btHeightfieldTerrainShape::quantizeWithClamp(int* out, const btVector3& point) const +void btHeightfieldTerrainShape::quantizeWithClamp(int* out, const btVector3& point,int isMax) const { - - btVector3 clampedPoint(point); clampedPoint.setMax(m_localAabbMin); clampedPoint.setMin(m_localAabbMax); - btVector3 v = (clampedPoint );// * m_quantization; - - // SMJ - Add 0.5 in the correct direction before doing the int conversion. - out[0] = (int)(v.getX() + v.getX() / btFabs(v.getX())* btScalar(0.5) ); - out[1] = (int)(v.getY() + v.getY() / btFabs(v.getY())* btScalar(0.5) ); - out[2] = (int)(v.getZ() + v.getZ() / btFabs(v.getZ())* btScalar(0.5) ); + btVector3 v = (clampedPoint);// - m_bvhAabbMin) * m_bvhQuantization; + out[0] = (unsigned short)(((unsigned short)v.getX() & 0xffffffe) | isMax); + out[1] = (unsigned short)(((unsigned short)v.getY() & 0xffffffe) | isMax); + out[2] = (unsigned short)(((unsigned short)v.getZ() & 0xffffffe) | isMax); } @@ -214,8 +210,8 @@ void btHeightfieldTerrainShape::processAllTriangles(btTriangleCallback* callback btVector3 localAabbMin = aabbMin*btVector3(1.f/m_localScaling[0],1.f/m_localScaling[1],1.f/m_localScaling[2]); btVector3 localAabbMax = aabbMax*btVector3(1.f/m_localScaling[0],1.f/m_localScaling[1],1.f/m_localScaling[2]); - quantizeWithClamp(quantizedAabbMin, localAabbMin); - quantizeWithClamp(quantizedAabbMax, localAabbMax); + quantizeWithClamp(quantizedAabbMin, localAabbMin,0); + quantizeWithClamp(quantizedAabbMax, localAabbMax,1); diff --git a/src/BulletCollision/CollisionShapes/btHeightfieldTerrainShape.h b/src/BulletCollision/CollisionShapes/btHeightfieldTerrainShape.h index 7e6831756..216e9ee7c 100644 --- a/src/BulletCollision/CollisionShapes/btHeightfieldTerrainShape.h +++ b/src/BulletCollision/CollisionShapes/btHeightfieldTerrainShape.h @@ -47,7 +47,7 @@ protected: btVector3 m_localScaling; virtual btScalar getHeightFieldValue(int x,int y) const; - void quantizeWithClamp(int* out, const btVector3& point) const; + void quantizeWithClamp(int* out, const btVector3& point,int isMax) const; void getVertex(int x,int y,btVector3& vertex) const; inline bool testQuantizedAabbAgainstQuantizedAabb(int* aabbMin1, int* aabbMax1,const int* aabbMin2,const int* aabbMax2) const diff --git a/src/BulletCollision/CollisionShapes/btOptimizedBvh.cpp b/src/BulletCollision/CollisionShapes/btOptimizedBvh.cpp index f9a85119d..4f9a22d7c 100644 --- a/src/BulletCollision/CollisionShapes/btOptimizedBvh.cpp +++ b/src/BulletCollision/CollisionShapes/btOptimizedBvh.cpp @@ -132,8 +132,8 @@ void btOptimizedBvh::build(btStridingMeshInterface* triangles, bool useQuantized aabbMin.setZ(aabbMin.z() - MIN_AABB_HALF_DIMENSION); } - m_optimizedTree->quantizeWithClamp(&node.m_quantizedAabbMin[0],aabbMin); - m_optimizedTree->quantizeWithClamp(&node.m_quantizedAabbMax[0],aabbMax); + m_optimizedTree->quantizeWithClamp(&node.m_quantizedAabbMin[0],aabbMin,0); + m_optimizedTree->quantizeWithClamp(&node.m_quantizedAabbMax[0],aabbMax,1); node.m_escapeIndexOrTriangleIndex = (partId<<(31-MAX_NUM_PARTS_IN_BITS)) | triangleIndex; @@ -221,8 +221,8 @@ void btOptimizedBvh::refitPartial(btStridingMeshInterface* meshInterface,const b unsigned short quantizedQueryAabbMin[3]; unsigned short quantizedQueryAabbMax[3]; - quantizeWithClamp(&quantizedQueryAabbMin[0],aabbMin); - quantizeWithClamp(&quantizedQueryAabbMax[0],aabbMax); + quantizeWithClamp(&quantizedQueryAabbMin[0],aabbMin,0); + quantizeWithClamp(&quantizedQueryAabbMax[0],aabbMax,1); int i; for (i=0;im_SubtreeHeaders.size();i++) @@ -328,8 +328,8 @@ void btOptimizedBvh::updateBvhNodes(btStridingMeshInterface* meshInterface,int f aabbMin.setMin(triangleVerts[2]); aabbMax.setMax(triangleVerts[2]); - quantizeWithClamp(&curNode.m_quantizedAabbMin[0],aabbMin); - quantizeWithClamp(&curNode.m_quantizedAabbMax[0],aabbMax); + quantizeWithClamp(&curNode.m_quantizedAabbMin[0],aabbMin,0); + quantizeWithClamp(&curNode.m_quantizedAabbMax[0],aabbMax,1); } else { @@ -613,8 +613,8 @@ void btOptimizedBvh::reportAabbOverlappingNodex(btNodeOverlapCallback* nodeCallb ///quantize query AABB unsigned short int quantizedQueryAabbMin[3]; unsigned short int quantizedQueryAabbMax[3]; - quantizeWithClamp(quantizedQueryAabbMin,aabbMin); - quantizeWithClamp(quantizedQueryAabbMax,aabbMax); + quantizeWithClamp(quantizedQueryAabbMin,aabbMin,0); + quantizeWithClamp(quantizedQueryAabbMax,aabbMax,1); switch (m_traversalMode) { @@ -783,8 +783,8 @@ void btOptimizedBvh::walkStacklessQuantizedTreeAgainstRay(btNodeOverlapCallback* unsigned short int quantizedQueryAabbMin[3]; unsigned short int quantizedQueryAabbMax[3]; - quantizeWithClamp(quantizedQueryAabbMin,rayAabbMin); - quantizeWithClamp(quantizedQueryAabbMax,rayAabbMax); + quantizeWithClamp(quantizedQueryAabbMin,rayAabbMin,0); + quantizeWithClamp(quantizedQueryAabbMax,rayAabbMax,1); while (curIndex < endNodeIndex) { @@ -833,11 +833,11 @@ void btOptimizedBvh::walkStacklessQuantizedTreeAgainstRay(btNodeOverlapCallback* } #endif #ifdef RAYAABB2 - ///disable this check: need to check division by zero (above) and fix the unQuantize method + ///careful with this check: need to check division by zero (above) and fix the unQuantize method ///thanks Joerg/hiker for the reproduction case! ///http://www.bulletphysics.com/Bullet/phpBB3/viewtopic.php?f=9&t=1858 - rayBoxOverlap = true;//btRayAabb2 (raySource, rayDirection, sign, bounds, param, 0.0, lambda_max); + rayBoxOverlap = btRayAabb2 (raySource, rayDirection, sign, bounds, param, 0.0f, lambda_max); #else rayBoxOverlap = true;//btRayAabb(raySource, rayTarget, bounds[0], bounds[1], param, normal); #endif diff --git a/src/BulletCollision/CollisionShapes/btOptimizedBvh.h b/src/BulletCollision/CollisionShapes/btOptimizedBvh.h index 2d0b8f5b8..0a6ad74cc 100644 --- a/src/BulletCollision/CollisionShapes/btOptimizedBvh.h +++ b/src/BulletCollision/CollisionShapes/btOptimizedBvh.h @@ -191,7 +191,7 @@ protected: { if (m_useQuantization) { - quantizeWithClamp(&m_quantizedContiguousNodes[nodeIndex].m_quantizedAabbMin[0] ,aabbMin); + quantizeWithClamp(&m_quantizedContiguousNodes[nodeIndex].m_quantizedAabbMin[0] ,aabbMin,0); } else { m_contiguousNodes[nodeIndex].m_aabbMinOrg = aabbMin; @@ -202,7 +202,7 @@ protected: { if (m_useQuantization) { - quantizeWithClamp(&m_quantizedContiguousNodes[nodeIndex].m_quantizedAabbMax[0],aabbMax); + quantizeWithClamp(&m_quantizedContiguousNodes[nodeIndex].m_quantizedAabbMax[0],aabbMax,1); } else { m_contiguousNodes[nodeIndex].m_aabbMaxOrg = aabbMax; @@ -251,8 +251,8 @@ protected: { unsigned short int quantizedAabbMin[3]; unsigned short int quantizedAabbMax[3]; - quantizeWithClamp(quantizedAabbMin,newAabbMin); - quantizeWithClamp(quantizedAabbMax,newAabbMax); + quantizeWithClamp(quantizedAabbMin,newAabbMin,0); + quantizeWithClamp(quantizedAabbMax,newAabbMax,1); for (int i=0;i<3;i++) { if (m_quantizedContiguousNodes[nodeIndex].m_quantizedAabbMin[i] > quantizedAabbMin[i]) @@ -333,7 +333,7 @@ public: void reportRayOverlappingNodex (btNodeOverlapCallback* nodeCallback, const btVector3& raySource, const btVector3& rayTarget) const; void reportBoxCastOverlappingNodex(btNodeOverlapCallback* nodeCallback, const btVector3& raySource, const btVector3& rayTarget, const btVector3& aabbMin,const btVector3& aabbMax) const; - SIMD_FORCE_INLINE void quantizeWithClamp(unsigned short* out, const btVector3& point) const + SIMD_FORCE_INLINE void quantizeWithClamp(unsigned short* out, const btVector3& point,int isMax) const { btAssert(m_useQuantization); @@ -341,13 +341,11 @@ public: btVector3 clampedPoint(point); clampedPoint.setMax(m_bvhAabbMin); clampedPoint.setMin(m_bvhAabbMax); - btVector3 v = (clampedPoint - m_bvhAabbMin) * m_bvhQuantization; - out[0] = (unsigned short)(v.getX()+0.5f); - out[1] = (unsigned short)(v.getY()+0.5f); - out[2] = (unsigned short)(v.getZ()+0.5f); + out[0] = (unsigned short)(((unsigned short)v.getX() & 0xfffe) | isMax); + out[1] = (unsigned short)(((unsigned short)v.getY() & 0xfffe) | isMax); + out[2] = (unsigned short)(((unsigned short)v.getZ() & 0xfffe) | isMax); } - SIMD_FORCE_INLINE btVector3 unQuantize(const unsigned short* vecIn) const {