Imported Upstream version 2.81
[platform/upstream/libbullet.git] / Demos / ConvexDecompositionDemo / ConvexDecompositionDemo.cpp
1 /*
2 Bullet Continuous Collision Detection and Physics Library
3 Copyright (c) 2003-2006 Erwin Coumans  http://continuousphysics.com/Bullet/
4
5 This software is provided 'as-is', without any express or implied warranty.
6 In no event will the authors be held liable for any damages arising from the use of this software.
7 Permission is granted to anyone to use this software for any purpose, 
8 including commercial applications, and to alter it and redistribute it freely, 
9 subject to the following restrictions:
10
11 1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required.
12 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
13 3. This notice may not be removed or altered from any source distribution.
14 */
15
16 #include "hacdCircularList.h"
17 #include "hacdVector.h"
18 #include "hacdICHull.h"
19 #include "hacdGraph.h"
20 #include "hacdHACD.h"
21
22 #include "cd_wavefront.h"
23 #include "ConvexBuilder.h"
24
25 #include "btBulletDynamicsCommon.h"
26
27 #include "LinearMath/btQuickprof.h"
28 #include "LinearMath/btIDebugDraw.h"
29 #include "LinearMath/btGeometryUtil.h"
30 #include "BulletCollision/CollisionShapes/btShapeHull.h"
31
32 //#define TEST_SERIALIZATION
33 //#define NO_OBJ_TO_BULLET
34
35 #ifdef TEST_SERIALIZATION
36 #include "LinearMath/btSerializer.h"
37 #include "btBulletFile.h"
38 #include "btBulletWorldImporter.h"
39 #endif
40
41 //#define USE_PARALLEL_DISPATCHER 1
42 #ifdef USE_PARALLEL_DISPATCHER
43 #include "../../Extras/BulletMultiThreaded/SpuGatheringCollisionDispatcher.h"
44 #include "../../Extras/BulletMultiThreaded/Win32ThreadSupport.h"
45 #include "../../Extras/BulletMultiThreaded/SpuNarrowPhaseCollisionTask/SpuGatheringCollisionTask.h"
46 #endif//USE_PARALLEL_DISPATCHER
47
48
49
50
51
52 #include "GLDebugFont.h"
53 #include <stdio.h> //printf debugging
54
55
56 #include "ConvexDecompositionDemo.h"
57 #include "GL_ShapeDrawer.h"
58
59 #include "GlutStuff.h"
60
61
62 btVector3       centroid=btVector3(0,0,0);
63 btVector3   convexDecompositionObjectOffset(10,0,0);
64
65 #define CUBE_HALF_EXTENTS 4
66
67
68 ////////////////////////////////////
69
70 unsigned int tcount = 0;
71
72
73 void ConvexDecompositionDemo::initPhysics()
74 {
75         initPhysics("file.obj");
76 }
77
78
79
80 ///MyContactCallback is just an example to show how to get access to the child shape that collided
81 bool MyContactCallback (
82     btManifoldPoint& cp,
83     const btCollisionObjectWrapper* colObj0Wrap,
84     int partId0,
85     int index0,
86     const btCollisionObjectWrapper* colObj1Wrap,
87     int partId1,
88     int index1)
89 {
90
91         if (colObj0Wrap->getCollisionObject()->getCollisionShape()->getShapeType()==COMPOUND_SHAPE_PROXYTYPE)
92         {
93                 btCompoundShape* compound = (btCompoundShape*)colObj0Wrap->getCollisionObject()->getCollisionShape();
94                 btCollisionShape* childShape;
95                 childShape = compound->getChildShape(index0);
96         }
97
98         if (colObj1Wrap->getCollisionObject()->getCollisionShape()->getShapeType()==COMPOUND_SHAPE_PROXYTYPE)
99         {
100                 btCompoundShape* compound = (btCompoundShape*)colObj1Wrap->getCollisionObject()->getCollisionShape();
101                 btCollisionShape* childShape;
102                 childShape = compound->getChildShape(index1);
103         }
104
105         return true;
106 }
107
108
109 void ConvexDecompositionDemo::setupEmptyDynamicsWorld()
110 {
111 m_collisionConfiguration = new btDefaultCollisionConfiguration();
112
113 #ifdef USE_PARALLEL_DISPATCHER
114 #ifdef USE_WIN32_THREADING
115
116         int maxNumOutstandingTasks = 4;//number of maximum outstanding tasks
117         Win32ThreadSupport* threadSupport = new Win32ThreadSupport(Win32ThreadSupport::Win32ThreadConstructionInfo(
118                                                                 "collision",
119                                                                 processCollisionTask,
120                                                                 createCollisionLocalStoreMemory,
121                                                                 maxNumOutstandingTasks));
122 #else
123 ///@todo other platform threading
124 ///Playstation 3 SPU (SPURS)  version is available through PS3 Devnet
125 ///Libspe2 SPU support will be available soon
126 ///pthreads version
127 ///you can hook it up to your custom task scheduler by deriving from btThreadSupportInterface
128 #endif
129
130         m_dispatcher = new      SpuGatheringCollisionDispatcher(threadSupport,maxNumOutstandingTasks,m_collisionConfiguration);
131 #else
132         m_dispatcher = new      btCollisionDispatcher(m_collisionConfiguration);
133 #endif//USE_PARALLEL_DISPATCHER
134
135
136         convexDecompositionObjectOffset.setValue(10,0,0);
137
138         btVector3 worldAabbMin(-10000,-10000,-10000);
139         btVector3 worldAabbMax(10000,10000,10000);
140
141         m_broadphase = new btAxisSweep3(worldAabbMin,worldAabbMax);
142         //m_broadphase = new btSimpleBroadphase();
143
144         m_solver = new btSequentialImpulseConstraintSolver();
145         m_dynamicsWorld = new btDiscreteDynamicsWorld(m_dispatcher,m_broadphase,m_solver,m_collisionConfiguration);
146
147 #ifdef USE_PARALLEL_DISPATCHER
148         m_dynamicsWorld->getDispatchInfo().m_enableSPU = true;
149 #endif //USE_PARALLEL_DISPATCHER
150
151 }
152
153 void ConvexDecompositionDemo::initPhysics(const char* filename)
154 {
155
156
157         gContactAddedCallback = &MyContactCallback;
158
159         setupEmptyDynamicsWorld();
160
161         setTexturing(true);
162         setShadows(true);
163
164         setCameraDistance(26.f);
165
166
167 #ifndef NO_OBJ_TO_BULLET
168
169         ConvexDecomposition::WavefrontObj wo;
170
171         tcount = wo.loadObj(filename);
172
173         if (!tcount)
174         {
175                 //when running this app from visual studio, the default starting folder is different, so make a second attempt...
176                 tcount = wo.loadObj("../../file.obj");
177         }
178         if (!tcount)
179         {
180                 //cmake generated msvc files need 4 levels deep back... so make a 3rd attempt...
181                 tcount = wo.loadObj("../../../../file.obj");
182         }
183
184
185         
186         
187         
188         btTransform startTransform;
189         startTransform.setIdentity();
190         startTransform.setOrigin(btVector3(0,-4.5,0));
191
192         btCollisionShape* boxShape = new btBoxShape(btVector3(30,2,30));
193         m_collisionShapes.push_back(boxShape);
194         localCreateRigidBody(0.f,startTransform,boxShape);
195
196         class MyConvexDecomposition : public ConvexDecomposition::ConvexDecompInterface
197         {
198                 ConvexDecompositionDemo*        m_convexDemo;
199                 
200                 public:
201
202                 btAlignedObjectArray<btConvexHullShape*> m_convexShapes;
203                 btAlignedObjectArray<btVector3> m_convexCentroids;
204
205                 MyConvexDecomposition (FILE* outputFile,ConvexDecompositionDemo* demo)
206                         :m_convexDemo(demo),
207                                 mBaseCount(0),
208                         mHullCount(0),
209                         mOutputFile(outputFile)
210
211                 {
212                 }
213                 
214                         virtual void ConvexDecompResult(ConvexDecomposition::ConvexResult &result)
215                         {
216
217                                 btTriangleMesh* trimesh = new btTriangleMesh();
218                                 m_convexDemo->m_trimeshes.push_back(trimesh);
219
220                                 btVector3 localScaling(6.f,6.f,6.f);
221
222                                 //export data to .obj
223                                 printf("ConvexResult. ");
224                                 if (mOutputFile)
225                                 {
226                                         fprintf(mOutputFile,"## Hull Piece %d with %d vertices and %d triangles.\r\n", mHullCount, result.mHullVcount, result.mHullTcount );
227
228                                         fprintf(mOutputFile,"usemtl Material%i\r\n",mBaseCount);
229                                         fprintf(mOutputFile,"o Object%i\r\n",mBaseCount);
230
231                                         for (unsigned int i=0; i<result.mHullVcount; i++)
232                                         {
233                                                 const float *p = &result.mHullVertices[i*3];
234                                                 fprintf(mOutputFile,"v %0.9f %0.9f %0.9f\r\n", p[0], p[1], p[2] );
235                                         }
236
237                                         //calc centroid, to shift vertices around center of mass
238                                         centroid.setValue(0,0,0);
239
240                                         btAlignedObjectArray<btVector3> vertices;
241                                         if ( 1 )
242                                         {
243                                                 //const unsigned int *src = result.mHullIndices;
244                                                 for (unsigned int i=0; i<result.mHullVcount; i++)
245                                                 {
246                                                         btVector3 vertex(result.mHullVertices[i*3],result.mHullVertices[i*3+1],result.mHullVertices[i*3+2]);
247                                                         vertex *= localScaling;
248                                                         centroid += vertex;
249                                                         
250                                                 }
251                                         }
252
253                                         centroid *= 1.f/(float(result.mHullVcount) );
254
255                                         if ( 1 )
256                                         {
257                                                 //const unsigned int *src = result.mHullIndices;
258                                                 for (unsigned int i=0; i<result.mHullVcount; i++)
259                                                 {
260                                                         btVector3 vertex(result.mHullVertices[i*3],result.mHullVertices[i*3+1],result.mHullVertices[i*3+2]);
261                                                         vertex *= localScaling;
262                                                         vertex -= centroid ;
263                                                         vertices.push_back(vertex);
264                                                 }
265                                         }
266                                         
267                         
268
269                                         if ( 1 )
270                                         {
271                                                 const unsigned int *src = result.mHullIndices;
272                                                 for (unsigned int i=0; i<result.mHullTcount; i++)
273                                                 {
274                                                         unsigned int index0 = *src++;
275                                                         unsigned int index1 = *src++;
276                                                         unsigned int index2 = *src++;
277
278
279                                                         btVector3 vertex0(result.mHullVertices[index0*3], result.mHullVertices[index0*3+1],result.mHullVertices[index0*3+2]);
280                                                         btVector3 vertex1(result.mHullVertices[index1*3], result.mHullVertices[index1*3+1],result.mHullVertices[index1*3+2]);
281                                                         btVector3 vertex2(result.mHullVertices[index2*3], result.mHullVertices[index2*3+1],result.mHullVertices[index2*3+2]);
282                                                         vertex0 *= localScaling;
283                                                         vertex1 *= localScaling;
284                                                         vertex2 *= localScaling;
285                                                         
286                                                         vertex0 -= centroid;
287                                                         vertex1 -= centroid;
288                                                         vertex2 -= centroid;
289
290
291                                                         trimesh->addTriangle(vertex0,vertex1,vertex2);
292
293                                                         index0+=mBaseCount;
294                                                         index1+=mBaseCount;
295                                                         index2+=mBaseCount;
296                                                         
297                                                         fprintf(mOutputFile,"f %d %d %d\r\n", index0+1, index1+1, index2+1 );
298                                                 }
299                                         }
300
301                                 //      float mass = 1.f;
302                                         
303
304 //this is a tools issue: due to collision margin, convex objects overlap, compensate for it here:
305 //#define SHRINK_OBJECT_INWARDS 1
306 #ifdef SHRINK_OBJECT_INWARDS
307
308                                         float collisionMargin = 0.01f;
309                                         
310                                         btAlignedObjectArray<btVector3> planeEquations;
311                                         btGeometryUtil::getPlaneEquationsFromVertices(vertices,planeEquations);
312
313                                         btAlignedObjectArray<btVector3> shiftedPlaneEquations;
314                                         for (int p=0;p<planeEquations.size();p++)
315                                         {
316                                                 btVector3 plane = planeEquations[p];
317                                                 plane[3] += collisionMargin;
318                                                 shiftedPlaneEquations.push_back(plane);
319                                         }
320                                         btAlignedObjectArray<btVector3> shiftedVertices;
321                                         btGeometryUtil::getVerticesFromPlaneEquations(shiftedPlaneEquations,shiftedVertices);
322
323                                         
324                                         btConvexHullShape* convexShape = new btConvexHullShape(&(shiftedVertices[0].getX()),shiftedVertices.size());
325                                         
326 #else //SHRINK_OBJECT_INWARDS
327                                         
328                                         btConvexHullShape* convexShape = new btConvexHullShape(&(vertices[0].getX()),vertices.size());
329 #endif 
330
331                                         convexShape->setMargin(0.01f);
332                                         m_convexShapes.push_back(convexShape);
333                                         m_convexCentroids.push_back(centroid);
334                                         m_convexDemo->m_collisionShapes.push_back(convexShape);
335                                         mBaseCount+=result.mHullVcount; // advance the 'base index' counter.
336
337
338                                 }
339                         }
340
341                         int     mBaseCount;
342                         int             mHullCount;
343                         FILE*   mOutputFile;
344
345         };
346
347         if (tcount)
348         {
349                 btTriangleMesh* trimesh = new btTriangleMesh();
350                 m_trimeshes.push_back(trimesh);
351
352                 btVector3 localScaling(6.f,6.f,6.f);
353                 
354                 int i;
355                 for ( i=0;i<wo.mTriCount;i++)
356                 {
357                         int index0 = wo.mIndices[i*3];
358                         int index1 = wo.mIndices[i*3+1];
359                         int index2 = wo.mIndices[i*3+2];
360
361                         btVector3 vertex0(wo.mVertices[index0*3], wo.mVertices[index0*3+1],wo.mVertices[index0*3+2]);
362                         btVector3 vertex1(wo.mVertices[index1*3], wo.mVertices[index1*3+1],wo.mVertices[index1*3+2]);
363                         btVector3 vertex2(wo.mVertices[index2*3], wo.mVertices[index2*3+1],wo.mVertices[index2*3+2]);
364                         
365                         vertex0 *= localScaling;
366                         vertex1 *= localScaling;
367                         vertex2 *= localScaling;
368
369                         trimesh->addTriangle(vertex0,vertex1,vertex2);
370                 }
371
372                 
373                 btConvexShape* tmpConvexShape = new btConvexTriangleMeshShape(trimesh);
374         
375                 printf("old numTriangles= %d\n",wo.mTriCount);
376                 printf("old numIndices = %d\n",wo.mTriCount*3);
377                 printf("old numVertices = %d\n",wo.mVertexCount);
378                 
379                 printf("reducing vertices by creating a convex hull\n");
380
381                 //create a hull approximation
382                 btShapeHull* hull = new btShapeHull(tmpConvexShape);
383                 btScalar margin = tmpConvexShape->getMargin();
384                 hull->buildHull(margin);
385                 tmpConvexShape->setUserPointer(hull);
386                 
387                 
388                 printf("new numTriangles = %d\n", hull->numTriangles ());
389                 printf("new numIndices = %d\n", hull->numIndices ());
390                 printf("new numVertices = %d\n", hull->numVertices ());
391                 
392                 btConvexHullShape* convexShape = new btConvexHullShape();
393                 for (i=0;i<hull->numVertices();i++)
394                 {
395                         convexShape->addPoint(hull->getVertexPointer()[i]);     
396                 }
397
398                 delete tmpConvexShape;
399                 delete hull;
400
401
402
403                 m_collisionShapes.push_back(convexShape);
404
405                 float mass = 1.f;
406                 
407                 btTransform startTransform;
408                 startTransform.setIdentity();
409                 startTransform.setOrigin(btVector3(0,2,14));
410
411                 localCreateRigidBody(mass, startTransform,convexShape);
412                 
413                 bool useQuantization = true;
414                 btCollisionShape* concaveShape = new btBvhTriangleMeshShape(trimesh,useQuantization);
415                 startTransform.setOrigin(convexDecompositionObjectOffset);
416                 localCreateRigidBody(0.f,startTransform,concaveShape);
417
418                 m_collisionShapes.push_back (concaveShape);
419
420         }
421                         
422
423         if (tcount)
424         {
425                 //-----------------------------------
426                 // Bullet Convex Decomposition
427                 //-----------------------------------
428
429                 char outputFileName[512];
430                 strcpy(outputFileName,filename);
431                 char *dot = strstr(outputFileName,".");
432                 if ( dot ) 
433                         *dot = 0;
434                 strcat(outputFileName,"_convex.obj");
435                 FILE* outputFile = fopen(outputFileName,"wb");
436                                 
437                 unsigned int depth = 5;
438                 float cpercent     = 5;
439                 float ppercent     = 15;
440                 unsigned int maxv  = 16;
441                 float skinWidth    = 0.0;
442
443                 printf("WavefrontObj num triangles read %i\n",tcount);
444                 ConvexDecomposition::DecompDesc desc;
445                 desc.mVcount       = wo.mVertexCount;
446                 desc.mVertices     = wo.mVertices;
447                 desc.mTcount       = wo.mTriCount;
448                 desc.mIndices      = (unsigned int *)wo.mIndices;
449                 desc.mDepth        = depth;
450                 desc.mCpercent     = cpercent;
451                 desc.mPpercent     = ppercent;
452                 desc.mMaxVertices  = maxv;
453                 desc.mSkinWidth    = skinWidth;
454
455                 MyConvexDecomposition   convexDecomposition(outputFile,this);
456                 desc.mCallback = &convexDecomposition;
457
458
459                 //-----------------------------------------------
460                 // HACD
461                 //-----------------------------------------------
462
463                 std::vector< HACD::Vec3<HACD::Real> > points;
464                 std::vector< HACD::Vec3<long> > triangles;
465
466                 for(int i=0; i<wo.mVertexCount; i++ ) 
467                 {
468                         int index = i*3;
469                         HACD::Vec3<HACD::Real> vertex(wo.mVertices[index], wo.mVertices[index+1],wo.mVertices[index+2]);
470                         points.push_back(vertex);
471                 }
472
473                 for(int i=0;i<wo.mTriCount;i++)
474                 {
475                         int index = i*3;
476                         HACD::Vec3<long> triangle(wo.mIndices[index], wo.mIndices[index+1], wo.mIndices[index+2]);
477                         triangles.push_back(triangle);
478                 }
479
480
481                 HACD::HACD myHACD;
482                 myHACD.SetPoints(&points[0]);
483                 myHACD.SetNPoints(points.size());
484                 myHACD.SetTriangles(&triangles[0]);
485                 myHACD.SetNTriangles(triangles.size());
486                 myHACD.SetCompacityWeight(0.1);
487                 myHACD.SetVolumeWeight(0.0);
488
489                 // HACD parameters
490                 // Recommended parameters: 2 100 0 0 0 0
491                 size_t nClusters = 2;
492                 double concavity = 100;
493                 bool invert = false;
494                 bool addExtraDistPoints = false;
495                 bool addNeighboursDistPoints = false;
496                 bool addFacesPoints = false;       
497
498                 myHACD.SetNClusters(nClusters);                     // minimum number of clusters
499                 myHACD.SetNVerticesPerCH(100);                      // max of 100 vertices per convex-hull
500                 myHACD.SetConcavity(concavity);                     // maximum concavity
501                 myHACD.SetAddExtraDistPoints(addExtraDistPoints);   
502                 myHACD.SetAddNeighboursDistPoints(addNeighboursDistPoints);   
503                 myHACD.SetAddFacesPoints(addFacesPoints); 
504
505                 myHACD.Compute();
506                 nClusters = myHACD.GetNClusters();      
507
508                 myHACD.Save("output.wrl", false);
509
510
511                 //convexDecomposition.performConvexDecomposition(desc);
512
513 //              ConvexBuilder cb(desc.mCallback);
514 //              cb.process(desc);
515                 //now create some bodies
516                 
517                 if (1)
518                 {
519                         btCompoundShape* compound = new btCompoundShape();
520                         m_collisionShapes.push_back (compound);
521
522                         btTransform trans;
523                         trans.setIdentity();
524
525                         for (int c=0;c<nClusters;c++)
526                         {
527                                 //generate convex result
528                                 size_t nPoints = myHACD.GetNPointsCH(c);
529                                 size_t nTriangles = myHACD.GetNTrianglesCH(c);
530
531                                 float* vertices = new float[nPoints*3];
532                                 unsigned int* triangles = new unsigned int[nTriangles*3];
533                                 
534                                 HACD::Vec3<HACD::Real> * pointsCH = new HACD::Vec3<HACD::Real>[nPoints];
535                                 HACD::Vec3<long> * trianglesCH = new HACD::Vec3<long>[nTriangles];
536                                 myHACD.GetCH(c, pointsCH, trianglesCH);
537
538                                 // points
539                                 for(size_t v = 0; v < nPoints; v++)
540                                 {
541                                         vertices[3*v] = pointsCH[v].X();
542                                         vertices[3*v+1] = pointsCH[v].Y();
543                                         vertices[3*v+2] = pointsCH[v].Z();
544                                 }
545                                 // triangles
546                                 for(size_t f = 0; f < nTriangles; f++)
547                                 {
548                                         triangles[3*f] = trianglesCH[f].X();
549                                         triangles[3*f+1] = trianglesCH[f].Y();
550                                         triangles[3*f+2] = trianglesCH[f].Z();
551                                 }
552
553                                 delete [] pointsCH;
554                                 delete [] trianglesCH;
555
556                                 ConvexResult r(nPoints, vertices, nTriangles, triangles);
557                                 convexDecomposition.ConvexDecompResult(r);
558                         }
559
560                         for (int i=0;i<convexDecomposition.m_convexShapes.size();i++)
561                         {
562                                 btVector3 centroid = convexDecomposition.m_convexCentroids[i];
563                                 trans.setOrigin(centroid);
564                                 btConvexHullShape* convexShape = convexDecomposition.m_convexShapes[i];
565                                 compound->addChildShape(trans,convexShape);
566
567                                 btRigidBody* body;
568                                 body = localCreateRigidBody( 1.0, trans,convexShape);
569                         }
570 /*                      for (int i=0;i<convexDecomposition.m_convexShapes.size();i++)
571                         {
572                                 
573                                 btVector3 centroid = convexDecomposition.m_convexCentroids[i];
574                                 trans.setOrigin(centroid);
575                                 btConvexHullShape* convexShape = convexDecomposition.m_convexShapes[i];
576                                 compound->addChildShape(trans,convexShape);
577
578                                 btRigidBody* body;
579                                 body = localCreateRigidBody( 1.0, trans,convexShape);
580                         }*/
581
582 #if 1
583                         btScalar mass=10.f;
584                         trans.setOrigin(-convexDecompositionObjectOffset);
585                         btRigidBody* body = localCreateRigidBody( mass, trans,compound);
586                         body->setCollisionFlags(body->getCollisionFlags() |   btCollisionObject::CF_CUSTOM_MATERIAL_CALLBACK);
587
588                         convexDecompositionObjectOffset.setZ(6);
589                         trans.setOrigin(-convexDecompositionObjectOffset);
590                         body = localCreateRigidBody( mass, trans,compound);
591                         body->setCollisionFlags(body->getCollisionFlags() |   btCollisionObject::CF_CUSTOM_MATERIAL_CALLBACK);
592
593                         convexDecompositionObjectOffset.setZ(-6);
594                         trans.setOrigin(-convexDecompositionObjectOffset);
595                         body = localCreateRigidBody( mass, trans,compound);
596                         body->setCollisionFlags(body->getCollisionFlags() |   btCollisionObject::CF_CUSTOM_MATERIAL_CALLBACK);
597 #endif
598                 }
599
600                 
601                 if (outputFile)
602                         fclose(outputFile);
603
604
605         }
606
607
608
609 #ifdef TEST_SERIALIZATION
610         //test serializing this 
611
612         int maxSerializeBufferSize = 1024*1024*5;
613
614         btDefaultSerializer*    serializer = new btDefaultSerializer(maxSerializeBufferSize);
615         m_dynamicsWorld->serialize(serializer);
616         
617         FILE* f2 = fopen("testFile.bullet","wb");
618         fwrite(serializer->getBufferPointer(),serializer->getCurrentBufferSize(),1,f2);
619         fclose(f2);
620
621         exitPhysics();
622
623         //now try again from the loaded file
624         setupEmptyDynamicsWorld();
625 #endif //TEST_SERIALIZATION
626
627 #endif //NO_OBJ_TO_BULLET
628
629 #ifdef TEST_SERIALIZATION
630
631         btBulletWorldImporter* fileLoader = new btBulletWorldImporter(m_dynamicsWorld);
632         //fileLoader->setVerboseMode(true);
633
634         fileLoader->loadFile("testFile.bullet");
635         //fileLoader->loadFile("testFile64Double.bullet");
636         //fileLoader->loadFile("testFile64Single.bullet");
637         //fileLoader->loadFile("testFile32Single.bullet");
638         
639
640
641
642 #endif //TEST_SERIALIZATION
643         
644 }
645
646 void ConvexDecompositionDemo::clientMoveAndDisplay()
647 {
648         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 
649
650         float dt = getDeltaTimeMicroseconds() * 0.000001f;
651
652         m_dynamicsWorld->stepSimulation(dt);
653
654         //optional but useful: debug drawing
655         m_dynamicsWorld->debugDrawWorld();
656
657         renderme();
658
659         glFlush();
660         swapBuffers();
661
662 }
663
664
665
666 void ConvexDecompositionDemo::displayCallback(void) {
667
668         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 
669
670
671         if (m_dynamicsWorld)
672                 m_dynamicsWorld->debugDrawWorld();
673
674         renderme();
675
676
677         glFlush();
678         swapBuffers();
679 }
680
681
682
683
684 void    ConvexDecompositionDemo::exitPhysics()
685 {
686
687
688         //cleanup in the reverse order of creation/initialization
689
690         //remove the rigidbodies from the dynamics world and delete them
691         int i;
692         for (i=m_dynamicsWorld->getNumCollisionObjects()-1; i>=0 ;i--)
693         {
694                 btCollisionObject* obj = m_dynamicsWorld->getCollisionObjectArray()[i];
695                 btRigidBody* body = btRigidBody::upcast(obj);
696                 if (body && body->getMotionState())
697                 {
698                         delete body->getMotionState();
699                 }
700                 m_dynamicsWorld->removeCollisionObject( obj );
701                 delete obj;
702         }
703
704         //delete collision shapes
705         for (i=0;i<m_collisionShapes.size();i++)
706         {
707                 btCollisionShape* shape = m_collisionShapes[i];
708                 delete shape;
709         }
710
711         m_collisionShapes.clear();
712
713         for (i=0;i<m_trimeshes.size();i++)
714         {
715                 btTriangleMesh* mesh = m_trimeshes[i];
716                 delete mesh;
717         }
718
719         m_trimeshes.clear();
720
721
722         //delete dynamics world
723         delete m_dynamicsWorld;
724
725         //delete solver
726         delete m_solver;
727
728         //delete broadphase
729         delete m_broadphase;
730
731         //delete dispatcher
732         delete m_dispatcher;
733
734         delete m_collisionConfiguration;
735
736         
737 }
738
739
740
741