com_jme3_bullet_util_DebugShapeFactory.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /*
  2. * Copyright (c) 2009-2010 jMonkeyEngine
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are
  7. * met:
  8. *
  9. * * Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. *
  12. * * Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. *
  16. * * Neither the name of 'jMonkeyEngine' nor the names of its contributors
  17. * may be used to endorse or promote products derived from this software
  18. * without specific prior written permission.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
  22. * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  23. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  24. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  25. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  26. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  27. * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  28. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  29. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  30. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. */
  32. /**
  33. * Author: Normen Hansen, CJ Hare
  34. */
  35. #include "com_jme3_bullet_util_DebugShapeFactory.h"
  36. #include "jmeBulletUtil.h"
  37. #include "BulletCollision/CollisionShapes/btShapeHull.h"
  38. class DebugCallback : public btTriangleCallback, public btInternalTriangleIndexCallback {
  39. public:
  40. JNIEnv* env;
  41. jobject callback;
  42. DebugCallback(JNIEnv* env, jobject object) {
  43. this->env = env;
  44. this->callback = object;
  45. }
  46. virtual void internalProcessTriangleIndex(btVector3* triangle, int partId, int triangleIndex) {
  47. processTriangle(triangle, partId, triangleIndex);
  48. }
  49. virtual void processTriangle(btVector3* triangle, int partId, int triangleIndex) {
  50. btVector3 vertexA, vertexB, vertexC;
  51. vertexA = triangle[0];
  52. vertexB = triangle[1];
  53. vertexC = triangle[2];
  54. env->CallVoidMethod(callback, jmeClasses::DebugMeshCallback_addVector, vertexA.getX(), vertexA.getY(), vertexA.getZ(), partId, triangleIndex);
  55. if (env->ExceptionCheck()) {
  56. env->Throw(env->ExceptionOccurred());
  57. return;
  58. }
  59. // triangle =
  60. env->CallVoidMethod(callback, jmeClasses::DebugMeshCallback_addVector, vertexB.getX(), vertexB.getY(), vertexB.getZ(), partId, triangleIndex);
  61. if (env->ExceptionCheck()) {
  62. env->Throw(env->ExceptionOccurred());
  63. return;
  64. }
  65. env->CallVoidMethod(callback, jmeClasses::DebugMeshCallback_addVector, vertexC.getX(), vertexC.getY(), vertexC.getZ(), partId, triangleIndex);
  66. if (env->ExceptionCheck()) {
  67. env->Throw(env->ExceptionOccurred());
  68. return;
  69. }
  70. }
  71. };
  72. #ifdef __cplusplus
  73. extern "C" {
  74. #endif
  75. /* Inaccessible static: _00024assertionsDisabled */
  76. /*
  77. * Class: com_jme3_bullet_util_DebugShapeFactory
  78. * Method: getVertices
  79. * Signature: (JLcom/jme3/bullet/util/DebugMeshCallback;)V
  80. */
  81. JNIEXPORT void JNICALL Java_com_jme3_bullet_util_DebugShapeFactory_getVertices
  82. (JNIEnv *env, jclass clazz, jlong shapeId, jobject callback) {
  83. btCollisionShape* shape = reinterpret_cast<btCollisionShape*>(shapeId);
  84. if (shape->isConcave()) {
  85. btConcaveShape* concave = reinterpret_cast<btConcaveShape*>(shape);
  86. DebugCallback* clb = new DebugCallback(env, callback);
  87. btVector3 min = btVector3(-1e30, -1e30, -1e30);
  88. btVector3 max = btVector3(1e30, 1e30, 1e30);
  89. concave->processAllTriangles(clb, min, max);
  90. delete(clb);
  91. } else if (shape->isConvex()) {
  92. btConvexShape* convexShape = reinterpret_cast<btConvexShape*>(shape);
  93. // Check there is a hull shape to render
  94. if (convexShape->getUserPointer() == NULL) {
  95. // create a hull approximation
  96. btShapeHull* hull = new btShapeHull(convexShape);
  97. float margin = convexShape->getMargin();
  98. hull->buildHull(margin);
  99. convexShape->setUserPointer(hull);
  100. }
  101. btShapeHull* hull = (btShapeHull*) convexShape->getUserPointer();
  102. int numberOfTriangles = hull->numTriangles();
  103. int numberOfFloats = 3 * 3 * numberOfTriangles;
  104. int byteBufferSize = numberOfFloats * 4;
  105. // Loop variables
  106. const unsigned int* hullIndices = hull->getIndexPointer();
  107. const btVector3* hullVertices = hull->getVertexPointer();
  108. btVector3 vertexA, vertexB, vertexC;
  109. int index = 0;
  110. for (int i = 0; i < numberOfTriangles; i++) {
  111. // Grab the data for this triangle from the hull
  112. vertexA = hullVertices[hullIndices[index++]];
  113. vertexB = hullVertices[hullIndices[index++]];
  114. vertexC = hullVertices[hullIndices[index++]];
  115. // Put the verticies into the vertex buffer
  116. env->CallVoidMethod(callback, jmeClasses::DebugMeshCallback_addVector, vertexA.getX(), vertexA.getY(), vertexA.getZ());
  117. if (env->ExceptionCheck()) {
  118. env->Throw(env->ExceptionOccurred());
  119. return;
  120. }
  121. env->CallVoidMethod(callback, jmeClasses::DebugMeshCallback_addVector, vertexB.getX(), vertexB.getY(), vertexB.getZ());
  122. if (env->ExceptionCheck()) {
  123. env->Throw(env->ExceptionOccurred());
  124. return;
  125. }
  126. env->CallVoidMethod(callback, jmeClasses::DebugMeshCallback_addVector, vertexC.getX(), vertexC.getY(), vertexC.getZ());
  127. if (env->ExceptionCheck()) {
  128. env->Throw(env->ExceptionOccurred());
  129. return;
  130. }
  131. }
  132. delete hull;
  133. convexShape->setUserPointer(NULL);
  134. }
  135. }
  136. #ifdef __cplusplus
  137. }
  138. #endif