Show
Ignore:
Timestamp:
2010-03-05 10:12:21 (6 months ago)
Author:
kickvb
Message:

- Made the debug colliders be cleared when toggling mode of the dynamics debugger
- Added method to update the display of the dynamics debugger
- Added display of the static/kinematic colliders
- Added methods to choose the colors of the static/dynamic/kinematic colliders

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • CS/trunk/plugins/physics/dyndebug/dyndebug.cpp

    r33668 r33752  
    8585 
    8686  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)); 
    8996  } 
    9097 
     
    100107 
    101108  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 () 
    102120  { 
    103121    // Check dynsys available 
     
    109127    } 
    110128 
    111     // Check debug material available 
    112     if (!material) 
    113       material = CS::Material::MaterialBuilder::CreateColorMaterial 
    114         (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 changed 
    124     if (debugMode == this->debugMode) 
    125       return; 
    126     this->debugMode = debugMode; 
    127  
    128129    // Find the pointer to the engine plugin 
    129130    csRef<iEngine> engine = csQueryRegistry<iEngine> (manager->object_reg); 
     
    135136    } 
    136137 
    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; 
    138171 
    139172    // Iterate through each rigid body 
     
    144177      iRigidBody* body = system->GetBody (bodyIndex); 
    145178 
     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 
    146195      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) 
    150202      { 
    151203        // 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); 
    188205      } 
    189206 
    190207      // TODO: display the joints too 
    191208      // TODO: use iDynamicsSystemCollider::FillWithColliderGeometry instead? 
    192       // TODO: use specific colors for objects static/dynamic/active/inactive 
     209      // TODO: use specific colors for objects active/inactive 
    193210      // TODO: display collisions 
    194211 
     
    202219          body->GetCollider (colliderIndex); 
    203220 
    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, 
    244336                                    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, 
    258364                                   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; 
    299371      } 
    300     } 
    301  
    302     // TODO: do the same with the static colliders of the dynamic system 
     372 
     373    return mesh; 
    303374  } 
    304375 
     
    510581  } 
    511582 
     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 
    512601} 
    513602CS_PLUGIN_NAMESPACE_END(DebugDynamics)