| 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)); |
| 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; |
| 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, |
| 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, |
| 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; |