From 9fa2460933affbd10447932865c27707e9bbf105 Mon Sep 17 00:00:00 2001 From: "erwin.coumans" Date: Mon, 21 Dec 2009 22:55:32 +0000 Subject: [PATCH] Provide verbosity control in BulletColladaConverter, see Issue 228 Thanks SkewMatrix for the patch Fix a crash in BulletColladaConverter, when bodies were created outside of the Collada converter, Thanks Jan Ciger for the patch, see Issue 306 --- .../ColladaConverter.cpp | 144 ++++++++++++------ .../BulletColladaConverter/ColladaConverter.h | 22 ++- 2 files changed, 116 insertions(+), 50 deletions(-) diff --git a/Extras/BulletColladaConverter/ColladaConverter.cpp b/Extras/BulletColladaConverter/ColladaConverter.cpp index f0cb3e7a4..256fdb86d 100644 --- a/Extras/BulletColladaConverter/ColladaConverter.cpp +++ b/Extras/BulletColladaConverter/ColladaConverter.cpp @@ -169,7 +169,8 @@ m_dom(0), m_filename(0), m_unitMeterScaling(1.f), m_use32bitIndices(true), -m_use4componentVertices(true) +m_use4componentVertices(true), +m_verbosity(LOUD) { } @@ -331,22 +332,31 @@ bool ColladaConverter::convert() switch( up->getValue() ) { case UPAXISTYPE_X_UP: - printf(" X is Up Data and Hiearchies must be converted!\n" ); - printf(" Conversion to X axis Up isn't currently supported!\n" ); - printf(" COLLADA_RT defaulting to Y Up \n" ); + if( getVerbosity() > SILENT ) + { + printf(" X is Up Data and Hiearchies must be converted!\n" ); + printf(" Conversion to X axis Up isn't currently supported!\n" ); + printf(" COLLADA_RT defaulting to Y Up \n" ); + } setGravity(btVector3(-10,0,0)); setCameraInfo(btVector3(1,0,0),1); break; case UPAXISTYPE_Y_UP: - printf(" Y Axis is Up for this file \n" ); - printf(" COLLADA_RT set to Y Up \n" ); + if( getVerbosity() > SILENT ) + { + printf(" Y Axis is Up for this file \n" ); + printf(" COLLADA_RT set to Y Up \n" ); + } setGravity(btVector3(0,-10,0)); setCameraInfo(btVector3(0,1,0),0); break; case UPAXISTYPE_Z_UP: - printf(" Z Axis is Up for this file \n" ); - printf(" All Geometry and Hiearchies must be converted!\n" ); + if( getVerbosity() > SILENT ) + { + printf(" Z Axis is Up for this file \n" ); + printf(" All Geometry and Hiearchies must be converted!\n" ); + } setGravity(btVector3(0,0,-10)); break; default: @@ -382,7 +392,8 @@ bool ColladaConverter::convert() { domLibrary_geometriesRef libgeom = m_dom->getLibrary_geometries_array()[i]; - printf(" CrtScene::Reading Geometry Library \n" ); + if( getVerbosity() > SILENT ) + printf(" CrtScene::Reading Geometry Library \n" ); for ( unsigned int i = 0; i < libgeom->getGeometry_array().getCount(); i++) { //ReadGeometry( ); @@ -396,32 +407,39 @@ bool ColladaConverter::convert() int numPolygonGroups = (int)meshElement->getPolygons_array().getCount(); int numPolyList = (int)meshElement->getPolylist_array().getCount(); int totalGroups = numTriangleGroups + numPolygonGroups + numPolyList; - if (totalGroups == 0) - { - printf("No Triangles or Polygons found int Geometry %s \n", lib->getId() ); - } else - { - printf("Found mesh geometry (%s): numTriangleGroups:%i numPolygonGroups :%i numPolyList=%i\n",lib->getId(),numTriangleGroups,numPolygonGroups,numPolyList); - } + if( getVerbosity() > NORMAL ) + { + if (totalGroups == 0) + { + printf("No Triangles or Polygons found int Geometry %s \n", lib->getId() ); + } else + { + printf("Found mesh geometry (%s): numTriangleGroups:%i numPolygonGroups :%i numPolyList=%i\n",lib->getId(),numTriangleGroups,numPolygonGroups,numPolyList); + } + } } domConvex_mesh *convexMeshElement = lib->getConvex_mesh(); if (convexMeshElement) { - printf("found convexmesh element\n"); + if( getVerbosity() > NORMAL ) + printf("found convexmesh element\n"); // Find out how many groups we need to allocate space for int numTriangleGroups = (int)convexMeshElement->getTriangles_array().getCount(); int numPolygonGroups = (int)convexMeshElement->getPolygons_array().getCount(); int totalGroups = numTriangleGroups + numPolygonGroups; - if (totalGroups == 0) - { - printf("No Triangles or Polygons found in ConvexMesh Geometry %s \n", lib->getId() ); - }else - { - printf("Found convexmesh geometry: numTriangleGroups:%i numPolygonGroups:%i\n",numTriangleGroups,numPolygonGroups); - } + if( getVerbosity() > NORMAL ) + { + if (totalGroups == 0) + { + printf("No Triangles or Polygons found in ConvexMesh Geometry %s \n", lib->getId() ); + }else + { + printf("Found convexmesh geometry: numTriangleGroups:%i numPolygonGroups:%i\n",numTriangleGroups,numPolygonGroups); + } + } }//fi }//for each geometry @@ -442,7 +460,8 @@ bool ColladaConverter::convert() if (physicsSceneRef->getTechnique_common()->getGravity()) { const domFloat3 grav = physicsSceneRef->getTechnique_common()->getGravity()->getValue(); - printf("gravity set to %f,%f,%f\n",grav.get(0),grav.get(1),grav.get(2)); + if( getVerbosity() > SILENT ) + printf("gravity set to %f,%f,%f\n",grav.get(0),grav.get(1),grav.get(2)); setGravity(btVector3(grav.get(0),grav.get(1),grav.get(2))); } @@ -510,7 +529,8 @@ bool ColladaConverter::convert() } } - printf("mass = %f, isDynamics %i\n",mass,isDynamics); + if( getVerbosity() > SILENT ) + printf("mass = %f, isDynamics %i\n",mass,isDynamics); if (bodyName && model) { @@ -603,7 +623,8 @@ bool ColladaConverter::convert() } } - printf("mass = %f, isDynamics %i\n",mass,isDynamics); + if( getVerbosity() > SILENT ) + printf("mass = %f, isDynamics %i\n",mass,isDynamics); domRigid_bodyRef savedRbRef = NULL; if (bodyName && model) @@ -628,7 +649,8 @@ bool ColladaConverter::convert() savedRbRef = rigidBodyRef; rbInput.m_instanceRigidBodyRef = instRigidbodyRef; ConvertRigidBodyRef( rbInput , output ); - printf("Found body converting %s\n", bodyName); + if( getVerbosity() > SILENT ) + printf("Found body converting %s\n", bodyName); mass = output.m_mass; isDynamics = output.m_isDynamics; @@ -1006,7 +1028,15 @@ void ColladaConverter::prepareConstraints(ConstraintInput& input) { btRigidBody* body = getRigidBody (i); domRigid_body* domRigidBody = findRigid_body(body); - btAssert(domRigidBody); + + /* + * JC patch. Skip the rigid body if it doesn't have any DOM + * node associated - e.g. because it was created directly, without + * the COLLADA file. + */ + if(!domRigidBody) + continue; + const char* name = domRigidBody->getSid(); if (name) @@ -1265,7 +1295,8 @@ domLibrary_physics_materials* ColladaConverter::getDefaultMaterialsLib () } } } - printf("No library physics materials. Creating one\n"); + if( getVerbosity() > SILENT ) + printf("No library physics materials. Creating one\n"); materialsLib = daeSafeCast (m_dom->createAndPlace (COLLADA_ELEMENT_LIBRARY_PHYSICS_MATERIALS)); materialsLib->setName("Bullet-PhysicsMaterials"); return materialsLib; @@ -1293,14 +1324,16 @@ domPhysics_model* ColladaConverter::getDefaultPhysicsModel () } } } else { - printf("No library physics model. Creating one\n"); + if( getVerbosity() > SILENT ) + printf("No library physics model. Creating one\n"); modelsLib = daeSafeCast (m_dom->createAndPlace (COLLADA_ELEMENT_LIBRARY_PHYSICS_MODELS )); } /* Always create in first physics models library */ modelsLib = m_dom->getLibrary_physics_models_array()[0]; - printf("Could not find physics model. Creating one\n"); + if( getVerbosity() > SILENT ) + printf("Could not find physics model. Creating one\n"); domPhysics_model* physicsModel = daeSafeCast(modelsLib->createAndPlace (COLLADA_ELEMENT_PHYSICS_MODEL)); @@ -1324,7 +1357,8 @@ domInstance_physics_model* ColladaConverter::getDefaultInstancePhysicsModel () return physicsModelInstance; } } - printf("Creating Bullet-PhysicsModel instance\n"); + if( getVerbosity() > SILENT ) + printf("Creating Bullet-PhysicsModel instance\n"); domInstance_physics_model* physicsModelInstance = daeSafeCast(physicsScene->createAndPlace (COLLADA_ELEMENT_INSTANCE_PHYSICS_MODEL)); physicsModelInstance->setUrl ("#Bullet-PhysicsModel"); return physicsModelInstance; @@ -1604,7 +1638,8 @@ void ColladaConverter::addConcaveMeshInternal(btStridingMeshInterface* meshInter geo->setId( nodeName ); geo->setName ( nodeName); - printf("numSubParts = \n",meshInterface->getNumSubParts ()); + if( getVerbosity() > NORMAL ) + printf("numSubParts = \n",meshInterface->getNumSubParts ()); for (int i = 0; i < meshInterface->getNumSubParts (); i++) { @@ -1628,10 +1663,12 @@ void ColladaConverter::addConcaveMeshInternal(btStridingMeshInterface* meshInter meshInterface->getLockedReadOnlyVertexIndexBase (&vertexBase, numVerts, vertexType, vertexStride, &indexBase, indexStride, numFaces, indexType, i); - printf("meshInterface subpart[%d].numVerts = %d\n",i,numVerts); - printf("meshInterface subpart[%d].numFaces = %d\n",i,numFaces); - printf("meshInterface subpart[%d].indexType= %d\n",i,indexType); - + if( getVerbosity() > NORMAL ) + { + printf("meshInterface subpart[%d].numVerts = %d\n",i,numVerts); + printf("meshInterface subpart[%d].numFaces = %d\n",i,numFaces); + printf("meshInterface subpart[%d].indexType= %d\n",i,indexType); + } btAssert (vertexBase); @@ -2521,7 +2558,8 @@ void ColladaConverter::syncOrAddRigidBody (btRigidBody* body) domRigid_body* dRigidBody=NULL; domInstance_rigid_body* dInstanceRigidBody=NULL; - printf("New body\n"); + if( getVerbosity() > SILENT ) + printf("New body\n"); btCollisionShape* shape = body->getCollisionShape (); if (!nodeName) @@ -2541,7 +2579,8 @@ void ColladaConverter::syncOrAddRigidBody (btRigidBody* body) shapeName = &shapeNameGen[0]; } - printf("Adding %s to COLLADA DOM.\n", nodeName); + if( getVerbosity() > SILENT ) + printf("Adding %s to COLLADA DOM.\n", nodeName); switch (shape->getShapeType()) @@ -3199,12 +3238,14 @@ void ColladaConverter::ConvertRigidBodyRef( btRigidBodyInput& rbInput,btRigidBod if (rbOutput.m_isDynamics) { - printf("moving concave added\n"); + if( getVerbosity() > NORMAL ) + printf("moving concave added\n"); //rbOutput.m_colShape = createConvexTriangleMeshShape(trimesh); rbOutput.m_colShape = createGimpactShape(trimesh); } else { - printf("static concave triangle added\n"); + if( getVerbosity() > NORMAL ) + printf("static concave triangle added\n"); rbOutput.m_colShape = createBvhTriangleMeshShape(trimesh); @@ -3276,11 +3317,15 @@ void ColladaConverter::ConvertRigidBodyRef( btRigidBodyInput& rbInput,btRigidBod if ( otherElemRef != NULL ) { domGeometryRef linkedGeom = *(domGeometryRef*)&otherElemRef; - printf( "otherLinked\n"); + if( getVerbosity() > NORMAL ) + printf( "otherLinked\n"); } else { - printf("convexMesh polyCount = %i\n",convexRef->getPolygons_array().getCount()); - printf("convexMesh triCount = %i\n",convexRef->getTriangles_array().getCount()); + if( getVerbosity() > NORMAL ) + { + printf("convexMesh polyCount = %i\n",convexRef->getPolygons_array().getCount()); + printf("convexMesh triCount = %i\n",convexRef->getTriangles_array().getCount()); + } } } @@ -3404,17 +3449,20 @@ void ColladaConverter::ConvertRigidBodyRef( btRigidBodyInput& rbInput,btRigidBod { rbOutput.m_colShape = convexHullShape; //rbOutput.m_colShape->setTypedUserInfo (new btShapeColladaInfo (geom)); - printf("created convexHullShape with %i points\n",convexHullShape->getNumVertices()); + if( getVerbosity() > NORMAL ) + printf("created convexHullShape with %i points\n",convexHullShape->getNumVertices()); } else { deleteShape( convexHullShape); - printf("failed to create convexHullShape\n"); + if( getVerbosity() > NORMAL ) + printf("failed to create convexHullShape\n"); } //domGeometryRef linkedGeom = *(domGeometryRef*)&otherElemRef; - printf("convexmesh\n"); + if( getVerbosity() > NORMAL ) + printf("convexmesh\n"); } } diff --git a/Extras/BulletColladaConverter/ColladaConverter.h b/Extras/BulletColladaConverter/ColladaConverter.h index acd030b25..5da9e4593 100644 --- a/Extras/BulletColladaConverter/ColladaConverter.h +++ b/Extras/BulletColladaConverter/ColladaConverter.h @@ -78,7 +78,23 @@ public: ///ColladaConverter helps converting the physics assets from COLLADA DOM into physics objects class ColladaConverter { - char m_cleaned_filename[513]; +public: + typedef enum { + SILENT, + NORMAL, + LOUD + } VerbosityLevel; + void setVerbosity( VerbosityLevel verbosity ) + { + m_verbosity = verbosity; + } + VerbosityLevel getVerbosity() const + { + return( m_verbosity ); + } + +private: + char m_cleaned_filename[513]; protected: @@ -99,7 +115,9 @@ protected: bool m_use32bitIndices; bool m_use4componentVertices; - void PreparePhysicsObject(struct btRigidBodyInput& input, bool isDynamics, float mass,btCollisionShape* colShape, const btVector3& linearVelocity, const btVector3& angularVelocity); + VerbosityLevel m_verbosity; + + void PreparePhysicsObject(struct btRigidBodyInput& input, bool isDynamics, float mass,btCollisionShape* colShape, const btVector3& linearVelocity, const btVector3& angularVelocity); void prepareConstraints(ConstraintInput& input);