Changeset 33752
- Timestamp:
- 2010-03-05 10:12:21 (5 months ago)
- Location:
- CS/trunk
- Files:
-
- 3 modified
-
include/ivaria/dynamicsdebug.h (modified) (4 diffs)
-
plugins/physics/dyndebug/dyndebug.cpp (modified) (7 diffs)
-
plugins/physics/dyndebug/dyndebug.h (modified) (5 diffs)
Legend:
- Unmodified
- Added
- Removed
-
CS/trunk/include/ivaria/dynamicsdebug.h
r33602 r33752 23 23 24 24 /**\file 25 * Debugging of dynamic ssystems25 * Debugging of dynamic systems 26 26 */ 27 27 … … 31 31 struct iDynamicSystem; 32 32 struct iSector; 33 struct iMaterialWrapper; 34 enum csBulletState; 33 35 34 36 /** … … 51 53 struct iDynamicSystemDebugger : public virtual iBase 52 54 { 53 SCF_INTERFACE(iDynamicSystemDebugger, 1, 0, 0);55 SCF_INTERFACE(iDynamicSystemDebugger, 1, 0, 1); 54 56 55 57 /** … … 74 76 */ 75 77 virtual void SetDebugDisplayMode (bool debugMode) = 0; 78 79 /** 80 * Update the list of colliders that are displayed. Call this when you have 81 * added or removed some dynamic bodies to/from the dynamic system. 82 */ 83 virtual void UpdateDisplay () = 0; 84 85 /** 86 * Set the material to be used for the colliders of the rigid bodies that are 87 * in 'static' state. If 0 is passed then the rigid bodies in 'static' state 88 * won't be displayed. If this method is not used, then a default red colored 89 * material will be used. 90 */ 91 virtual void SetStaticBodyMaterial (iMaterialWrapper* material) = 0; 92 93 /** 94 * Set the material to be used for the colliders of the rigid bodies that are 95 * in 'dynamic' state. If 0 is passed then the rigid bodies in 'dynamic' state 96 * won't be displayed. If this method is not used, then a default green colored 97 * material will be used. 98 */ 99 virtual void SetDynamicBodyMaterial (iMaterialWrapper* material) = 0; 100 101 /** 102 * Set the material to be used for the colliders of the rigid bodies that are 103 * in the given state. If 0 is passed then the rigid bodies in the given state 104 * won't be displayed. If this method is not used, then a default blue colored 105 * material will be used. 106 */ 107 virtual void SetBodyStateMaterial (csBulletState state, 108 iMaterialWrapper* material) = 0; 76 109 }; 77 110 -
CS/trunk/plugins/physics/dyndebug/dyndebug.cpp
r33668 r33752 85 85 86 86 DynamicsDebugger::DynamicsDebugger (DebuggerManager* manager) 87 : scfImplementationType (this), manager (manager) 88 { 87 : scfImplementationType (this), manager (manager), debugMode (false) 88 { 89 // Init debug materials 90 materials[0] = CS::Material::MaterialBuilder::CreateColorMaterial 91 (manager->object_reg, "dyndebug_static", csColor (0, 0, 1)); 92 materials[1] = CS::Material::MaterialBuilder::CreateColorMaterial 93 (manager->object_reg, "dyndebug_dynamic", csColor (0, 1, 0)); 94 materials[2] = CS::Material::MaterialBuilder::CreateColorMaterial 95 (manager->object_reg, "dyndebug_kinematic", csColor (0, 0, 1)); 89 96 } 90 97 … … 100 107 101 108 void DynamicsDebugger::SetDebugDisplayMode (bool debugMode) 109 { 110 // Check display mode has changed 111 if (debugMode == this->debugMode) 112 return; 113 114 this->debugMode = debugMode; 115 116 UpdateDisplay (); 117 } 118 119 void DynamicsDebugger::UpdateDisplay () 102 120 { 103 121 // Check dynsys available … … 109 127 } 110 128 111 // Check debug material available112 if (!material)113 material = CS::Material::MaterialBuilder::CreateColorMaterial114 (manager->object_reg, "dynsysdebug", csColor (1, 0, 0));115 116 if (!material)117 {118 manager->Report (CS_REPORTER_SEVERITY_WARNING,119 "No debug material defined");120 return;121 }122 123 // Check display mode has changed124 if (debugMode == this->debugMode)125 return;126 this->debugMode = debugMode;127 128 129 // Find the pointer to the engine plugin 129 130 csRef<iEngine> engine = csQueryRegistry<iEngine> (manager->object_reg); … … 135 136 } 136 137 137 // TODO: remove all previous debug meshes 138 // Reset all stored meshes 139 for (csList<MeshData>::Iterator it (storedMeshes); it.HasNext (); ) 140 { 141 MeshData &meshData = it.Next (); 142 143 // Remove the debug mesh 144 if (meshData.debugMesh) 145 { 146 engine->RemoveObject (meshData.debugMesh); 147 if (meshData.rigidBody) 148 meshData.rigidBody->AttachMesh (0); 149 } 150 151 // Put back the original mesh 152 if (meshData.originalMesh) 153 { 154 meshData.originalMesh->GetMovable ()->SetSector (sector); 155 meshData.rigidBody->AttachMesh (meshData.originalMesh); 156 } 157 158 // Put back the kinematic callback 159 if (meshData.callback && meshData.rigidBody) 160 { 161 csRef<iBulletRigidBody> bulletBody = 162 scfQueryInterface<iBulletRigidBody> (meshData.rigidBody); 163 bulletBody->SetKinematicCallback (meshData.callback->callback); 164 } 165 } 166 storedMeshes.DeleteAll (); 167 168 // If not in debug mode then it is over 169 if (!debugMode) 170 return; 138 171 139 172 // Iterate through each rigid body … … 144 177 iRigidBody* body = system->GetBody (bodyIndex); 145 178 179 // Search the bullet interface of the rigid body 180 csRef<iBulletRigidBody> bulletBody = 181 scfQueryInterface<iBulletRigidBody> (body); 182 183 // Find the material to be used for this object 184 csBulletState state = BULLET_STATE_DYNAMIC; 185 if (bulletBody) 186 state = bulletBody->GetDynamicState (); 187 else if (body->IsStatic ()) 188 state = BULLET_STATE_STATIC; 189 190 iMaterialWrapper* material = materials[state]; 191 if (!material) 192 continue; 193 194 // Create the MeshData object 146 195 MeshData meshData; 147 148 // If in debug mode, then store the original attached mesh 149 if (debugMode) 196 meshData.rigidBody = body; 197 198 // Store the current mesh attached to the rigid body 199 // TODO: not debug mesh stored twice? 200 meshData.originalMesh = body->GetAttachedMesh (); 201 if (meshData.originalMesh) 150 202 { 151 203 // TODO: store the sector of the mesh 152 csRef<iMeshWrapper> mesh = body->GetAttachedMesh (); 153 if (mesh) 154 { 155 engine->RemoveObject (mesh); 156 meshData.mesh = mesh; 157 } 158 159 storedMeshes.PutUnique (body, meshData); 160 } 161 162 // Else, go back to normal mode 163 else 164 { 165 if (storedMeshes.Contains (body)) 166 { 167 // Remove the debug mesh 168 engine->RemoveObject (body->GetAttachedMesh ()); 169 170 // Put back the original attached mesh 171 MeshData nullData; 172 meshData = storedMeshes.Get (body, nullData); 173 174 if (meshData.mesh) 175 { 176 meshData.mesh->GetMovable ()->SetSector (sector); 177 body->AttachMesh (meshData.mesh); 178 } 179 180 else 181 body->AttachMesh (0); 182 183 // Erase body reference 184 storedMeshes.DeleteAll (body); 185 } 186 187 continue; 204 engine->RemoveObject (meshData.originalMesh); 188 205 } 189 206 190 207 // TODO: display the joints too 191 208 // TODO: use iDynamicsSystemCollider::FillWithColliderGeometry instead? 192 // TODO: use specific colors for objects static/dynamic/active/inactive209 // TODO: use specific colors for objects active/inactive 193 210 // TODO: display collisions 194 211 … … 202 219 body->GetCollider (colliderIndex); 203 220 204 switch (collider->GetGeometryType ()) 205 { 206 case BOX_COLLIDER_GEOMETRY: 207 { 208 // Get box geometry 209 csVector3 boxSize; 210 collider->GetBoxGeometry (boxSize); 211 boxSize /= 2.0; 212 const csBox3 box(-boxSize, boxSize); 213 214 // Create box 215 csRef<iMeshWrapper> mesh = 216 CreateBoxMesh (box, material, collider->GetLocalTransform (), 217 sector); 218 body->AttachMesh (mesh); 219 } 220 break; 221 222 case SPHERE_COLLIDER_GEOMETRY: 223 { 224 // Get sphere geometry 225 csSphere sphere; 226 collider->GetSphereGeometry (sphere); 227 228 // Create sphere 229 csRef<iMeshWrapper> mesh = 230 CreateSphereMesh (sphere, material, sector); 231 body->AttachMesh (mesh); 232 } 233 break; 234 235 case CYLINDER_COLLIDER_GEOMETRY: 236 { 237 // Get cylinder geometry 238 float length, radius; 239 collider->GetCylinderGeometry (length, radius); 240 241 // Create cylinder 242 csRef<iMeshWrapper> mesh = 243 CreateCylinderMesh (length, radius, material, 221 csRef<iMeshWrapper> mesh = CreateColliderMesh (collider, material); 222 223 // Register the new debug mesh 224 if (mesh) 225 { 226 body->AttachMesh (mesh); 227 meshData.debugMesh = mesh; 228 } 229 230 // If the body is kinematic then create a new kinematic callback 231 if (mesh && state == BULLET_STATE_KINEMATIC) 232 { 233 meshData.callback.AttachNew 234 (new BoneKinematicCallback (mesh, bulletBody->GetKinematicCallback ())); 235 bulletBody->SetKinematicCallback (meshData.callback); 236 } 237 } 238 239 // Store the MeshData 240 storedMeshes.PushBack (meshData); 241 } 242 243 // Iterate through each colliders of the dynamic system 244 for (unsigned int colliderIndex = 0; 245 colliderIndex < (unsigned int) system->GetColliderCount (); 246 colliderIndex++) 247 { 248 iDynamicsSystemCollider* collider = system->GetCollider (colliderIndex); 249 250 // Find the material to be used for this object 251 csBulletState state = BULLET_STATE_DYNAMIC; 252 if (collider->IsStatic ()) 253 state = BULLET_STATE_STATIC; 254 255 iMaterialWrapper* material = materials[state]; 256 if (!material) 257 continue; 258 259 // Create the MeshData object 260 MeshData meshData; 261 meshData.debugMesh = CreateColliderMesh (collider, material); 262 263 // Store the MeshData 264 storedMeshes.PushBack (meshData); 265 } 266 } 267 268 void DynamicsDebugger::SetStaticBodyMaterial (iMaterialWrapper* material) 269 { 270 materials[0] = material; 271 } 272 273 void DynamicsDebugger::SetDynamicBodyMaterial (iMaterialWrapper* material) 274 { 275 materials[1] = material; 276 } 277 278 void DynamicsDebugger::SetBodyStateMaterial (csBulletState state, 279 iMaterialWrapper* material) 280 { 281 materials[state] = material; 282 } 283 284 csRef<iMeshWrapper> DynamicsDebugger::CreateColliderMesh 285 (iDynamicsSystemCollider* collider, iMaterialWrapper* material) 286 { 287 csRef<iMeshWrapper> mesh; 288 289 switch (collider->GetGeometryType ()) 290 { 291 case BOX_COLLIDER_GEOMETRY: 292 { 293 // Get box geometry 294 csVector3 boxSize; 295 collider->GetBoxGeometry (boxSize); 296 boxSize /= 2.0; 297 const csBox3 box(-boxSize, boxSize); 298 299 // Create box 300 mesh = CreateBoxMesh (box, material, collider->GetLocalTransform (), 301 sector); 302 } 303 break; 304 305 case SPHERE_COLLIDER_GEOMETRY: 306 { 307 // Get sphere geometry 308 csSphere sphere; 309 collider->GetSphereGeometry (sphere); 310 311 // Create sphere 312 mesh = CreateSphereMesh (sphere, material, sector); 313 } 314 break; 315 316 case CYLINDER_COLLIDER_GEOMETRY: 317 { 318 // Get cylinder geometry 319 float length, radius; 320 collider->GetCylinderGeometry (length, radius); 321 322 // Create cylinder 323 mesh = CreateCylinderMesh (length, radius, material, 324 collider->GetLocalTransform (), sector); 325 } 326 break; 327 328 case CAPSULE_COLLIDER_GEOMETRY: 329 { 330 // Get capsule geometry 331 float length, radius; 332 collider->GetCapsuleGeometry (length, radius); 333 334 // Create capsule 335 mesh = CreateCapsuleMesh (length, radius, material, 244 336 collider->GetLocalTransform (), sector); 245 body->AttachMesh (mesh); 246 } 247 break; 248 249 case CAPSULE_COLLIDER_GEOMETRY: 250 { 251 // Get capsule geometry 252 float length, radius; 253 collider->GetCapsuleGeometry (length, radius); 254 255 // Create capsule 256 csRef<iMeshWrapper> mesh = 257 CreateCapsuleMesh (length, radius, material, 337 } 338 break; 339 340 case TRIMESH_COLLIDER_GEOMETRY: 341 { 342 // Get mesh geometry 343 csVector3* vertices = 0; 344 int* indices = 0; 345 size_t vertexCount, triangleCount; 346 collider->GetMeshGeometry (vertices, vertexCount, indices, triangleCount); 347 348 // Create mesh 349 mesh = CreateCustomMesh (vertices, vertexCount, indices, triangleCount, 350 material, collider->GetLocalTransform (), sector); 351 } 352 break; 353 354 case CONVEXMESH_COLLIDER_GEOMETRY: 355 { 356 // Get mesh geometry 357 csVector3* vertices = 0; 358 int* indices = 0; 359 size_t vertexCount, triangleCount; 360 collider->GetConvexMeshGeometry (vertices, vertexCount, indices, triangleCount); 361 362 // Create mesh 363 mesh = CreateCustomMesh (vertices, vertexCount, indices, triangleCount, material, 258 364 collider->GetLocalTransform (), sector); 259 body->AttachMesh (mesh); 260 } 261 break; 262 263 case TRIMESH_COLLIDER_GEOMETRY: 264 { 265 // Get mesh geometry 266 csVector3* vertices = 0; 267 int* indices = 0; 268 size_t vertexCount, triangleCount; 269 collider->GetMeshGeometry (vertices, vertexCount, indices, triangleCount); 270 271 // Create mesh 272 csRef<iMeshWrapper> mesh = 273 CreateCustomMesh (vertices, vertexCount, indices, triangleCount, material, 274 collider->GetLocalTransform (), sector); 275 body->AttachMesh (mesh); 276 } 277 break; 278 279 case CONVEXMESH_COLLIDER_GEOMETRY: 280 { 281 // Get mesh geometry 282 csVector3* vertices = 0; 283 int* indices = 0; 284 size_t vertexCount, triangleCount; 285 collider->GetConvexMeshGeometry (vertices, vertexCount, indices, triangleCount); 286 287 // Create mesh 288 csRef<iMeshWrapper> mesh = 289 CreateCustomMesh (vertices, vertexCount, indices, triangleCount, material, 290 collider->GetLocalTransform (), sector); 291 body->AttachMesh (mesh); 292 } 293 break; 294 295 default: 296 // TODO: plan meshes 297 break; 298 } 365 } 366 break; 367 368 default: 369 // TODO: plan meshes 370 break; 299 371 } 300 } 301 302 // TODO: do the same with the static colliders of the dynamic system 372 373 return mesh; 303 374 } 304 375 … … 510 581 } 511 582 583 BoneKinematicCallback::BoneKinematicCallback (iMeshWrapper* mesh, 584 iBulletKinematicCallback* callback) 585 : scfImplementationType (this), mesh (mesh), callback (callback) 586 { 587 } 588 589 BoneKinematicCallback::~BoneKinematicCallback () 590 { 591 } 592 593 void BoneKinematicCallback::GetBodyTransform 594 (iRigidBody* body, csOrthoTransform& transform) const 595 { 596 callback->GetBodyTransform (body, transform); 597 mesh->GetMovable ()->SetTransform (transform); 598 mesh->GetMovable ()->UpdateMove (); 599 } 600 512 601 } 513 602 CS_PLUGIN_NAMESPACE_END(DebugDynamics) -
CS/trunk/plugins/physics/dyndebug/dyndebug.h
r33668 r33752 26 26 #include "csutil/leakguard.h" 27 27 #include "csutil/weakref.h" 28 #include "csutil/refarr.h" 28 #include "csutil/list.h" 29 #include "ivaria/bullet.h" 29 30 #include "ivaria/dynamicsdebug.h" 30 31 … … 40 41 { 41 42 class DynamicsDebugger; 43 class BoneKinematicCallback; 42 44 43 45 class DebuggerManager : public scfImplementation2<DebuggerManager, … … 76 78 virtual void SetDynamicSystem (iDynamicSystem* system); 77 79 virtual void SetDebugSector (iSector* sector); 80 78 81 virtual void SetDebugDisplayMode (bool debugMode); 82 virtual void UpdateDisplay (); 83 84 virtual void SetStaticBodyMaterial (iMaterialWrapper* material); 85 virtual void SetDynamicBodyMaterial (iMaterialWrapper* material); 86 virtual void SetBodyStateMaterial (csBulletState state, 87 iMaterialWrapper* material); 79 88 80 89 private: 90 csRef<iMeshWrapper> CreateColliderMesh (iDynamicsSystemCollider* collider, 91 iMaterialWrapper* material); 92 81 93 csRef<iMeshWrapper> CreateBoxMesh (csBox3 box, 82 94 iMaterialWrapper* material, … … 108 120 struct MeshData 109 121 { 110 csRef<iMeshWrapper> mesh; 111 //csOrthoTransform transform; 122 csWeakRef<iRigidBody> rigidBody; 123 csRef<iMeshWrapper> originalMesh; 124 csRef<iMeshWrapper> debugMesh; 125 csRef<BoneKinematicCallback> callback; 112 126 }; 113 127 … … 115 129 csRef<iDynamicSystem> system; 116 130 csRef<iSector> sector; 117 csRef<iMaterialWrapper> material ;131 csRef<iMaterialWrapper> materials[3]; 118 132 bool debugMode; 119 csHash<MeshData, csPtrKey<iRigidBody> > storedMeshes; 133 csList<MeshData> storedMeshes; 134 }; 135 136 137 class BoneKinematicCallback : public scfImplementation1 138 <BoneKinematicCallback, iBulletKinematicCallback> 139 { 140 public: 141 BoneKinematicCallback (iMeshWrapper* mesh, 142 iBulletKinematicCallback* callback); 143 ~BoneKinematicCallback (); 144 145 void GetBodyTransform (iRigidBody* body, csOrthoTransform& transform) const; 146 147 private: 148 csWeakRef<iMeshWrapper> mesh; 149 csRef<iBulletKinematicCallback> callback; 150 151 friend class DynamicsDebugger; 120 152 }; 121 153
