Physics Draw.cpp 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. /******************************************************************************/
  2. #include "stdafx.h"
  3. namespace EE{
  4. /******************************************************************************/
  5. static void DrawTris(Vec *pos, VecI *tri, Int tris, C Color &color, Bool fill)
  6. {
  7. if(pos && tri && tris)
  8. {
  9. VI.color(color);
  10. if(fill)
  11. {
  12. for(; tris--; )
  13. {
  14. Int *p=(tri++)->c;
  15. VI.tri(pos[p[0]], pos[p[1]], pos[p[2]]);
  16. }
  17. }else
  18. {
  19. for(; tris--; )
  20. {
  21. Int *p=(tri++)->c, p0=p[0], p1=p[1], p2=p[2];
  22. VI.line(pos[p0], pos[p1]);
  23. VI.line(pos[p1], pos[p2]);
  24. VI.line(pos[p2], pos[p0]);
  25. }
  26. }
  27. VI.end();
  28. }
  29. }
  30. static void DrawTris16(Vec *pos, VecUS *tri, Int tris, C Color &color, Bool fill)
  31. {
  32. if(pos && tri && tris)
  33. {
  34. VI.color(color);
  35. if(fill)
  36. {
  37. for(; tris--; )
  38. {
  39. UShort *p=(tri++)->c;
  40. VI.tri(pos[p[0]], pos[p[1]], pos[p[2]]);
  41. }
  42. }else
  43. {
  44. for(; tris--; )
  45. {
  46. UShort *p=(tri++)->c, p0=p[0], p1=p[1], p2=p[2];
  47. VI.line(pos[p0], pos[p1]);
  48. VI.line(pos[p1], pos[p2]);
  49. VI.line(pos[p2], pos[p0]);
  50. }
  51. }
  52. VI.end();
  53. }
  54. }
  55. /******************************************************************************/
  56. #if PHYSX
  57. /******************************************************************************/
  58. void DrawMesh(PxTriangleMesh &mesh, C Color &color, Bool fill)
  59. {
  60. if(mesh.getTriangleMeshFlags()&PxTriangleMeshFlag::e16_BIT_INDICES)DrawTris16((Vec*)mesh.getVertices(), (VecUS*)mesh.getTriangles(), mesh.getNbTriangles(), color, fill);
  61. else DrawTris ((Vec*)mesh.getVertices(), (VecI *)mesh.getTriangles(), mesh.getNbTriangles(), color, fill);
  62. }
  63. void DrawConvex(PxConvexMesh &convex, C Color &color, Bool fill)
  64. {
  65. const PxU8 *ind= convex.getIndexBuffer();
  66. const Vec *vtx=(const Vec*)convex.getVertices ();
  67. PxHullPolygon poly;
  68. VI.color(color);
  69. REP(convex.getNbPolygons())
  70. if(convex.getPolygonData(i, poly))
  71. REP(poly.mNbVerts)
  72. {
  73. VI.line(vtx[ind[poly.mIndexBase+i]], vtx[ind[poly.mIndexBase+(i+1)%poly.mNbVerts]]);
  74. }
  75. VI.end();
  76. }
  77. /******************************************************************************/
  78. static void DrawShape(PxShape *shape, C Color &color, Bool fill, C Matrix &actor_matrix)
  79. {
  80. if(shape)
  81. {
  82. Matrix matrix=Physx.matrix(shape->getLocalPose())*actor_matrix;
  83. switch(shape->getGeometryType())
  84. {
  85. case PxGeometryType::ePLANE : {PxPlaneGeometry plane ; if(shape->getPlaneGeometry (plane )){SetMatrix(matrix); Plane (VecZero, Vec(1,0,0)).drawInfiniteByResolution(color);}} break;
  86. case PxGeometryType::eSPHERE : {PxSphereGeometry sphere ; if(shape->getSphereGeometry (sphere )){SetMatrix(matrix); Ball (sphere.radius).draw(color, fill);}} break;
  87. case PxGeometryType::eCAPSULE : {PxCapsuleGeometry capsule; if(shape->getCapsuleGeometry (capsule)){SetMatrix(matrix); Capsule(capsule.radius, (capsule.halfHeight+capsule.radius)*2, VecZero, Vec(1,0,0)).draw(color, fill);}} break;
  88. case PxGeometryType::eBOX : {PxBoxGeometry box ; if(shape->getBoxGeometry (box )){SetMatrix(matrix); Box (Vec(-box.halfExtents.x, -box.halfExtents.y, -box.halfExtents.z), Vec(box.halfExtents.x, box.halfExtents.y, box.halfExtents.z)).draw(color, fill);}} break;
  89. case PxGeometryType::eCONVEXMESH : {PxConvexMeshGeometry convex ; if(shape->getConvexMeshGeometry (convex )){SetMatrix(Matrix().setScale(Physx.vec(convex.scale.scale))*matrix); DrawConvex(*convex. convexMesh, color, fill);}} break;
  90. case PxGeometryType::eTRIANGLEMESH: {PxTriangleMeshGeometry mesh ; if(shape->getTriangleMeshGeometry(mesh )){SetMatrix(Matrix().setScale(Physx.vec(mesh .scale.scale))*matrix); DrawMesh (*mesh .triangleMesh, color, fill);}} break;
  91. case PxGeometryType::eHEIGHTFIELD : break;
  92. }
  93. }
  94. }
  95. static void ActorDraw(PxActor *actor, C Color &color, Bool fill)
  96. {
  97. if(actor)
  98. if(Frustum(Physx.box(actor->getWorldBounds())))
  99. {
  100. if(PxRigidActor *rigid=actor->is<PxRigidActor>())
  101. {
  102. Matrix matrix=Physx.matrix(rigid->getGlobalPose());
  103. Int shapes=rigid->getNbShapes();
  104. REP(shapes)
  105. {
  106. PxShape *shape=null;
  107. if(rigid->getShapes(&shape, 1, i))DrawShape(shape, color, fill, matrix);
  108. }
  109. }
  110. }
  111. }
  112. void Actor ::draw(C Color &color, Bool fill)C {ActorDraw(_actor, color, fill);}
  113. void PhysicsClass::draw()
  114. {
  115. ReadLock lock(_rws);
  116. if(Physx.world)
  117. {
  118. PxActorTypeFlags types=PxActorTypeFlag::eRIGID_STATIC | PxActorTypeFlag::eRIGID_DYNAMIC;
  119. Int actors=Physx.world->getNbActors(types);
  120. REP(actors)
  121. {
  122. PxActor *actor=null;
  123. if(Physx.world->getActors(types, &actor, 1, i))ActorDraw(actor, WHITE, false);
  124. }
  125. }
  126. }
  127. /******************************************************************************/
  128. #else // BULLET
  129. /******************************************************************************/
  130. void DrawConvex(btConvexHullShape &convex, C Color &color, Bool fill)
  131. {
  132. VI.color(color);
  133. REP(convex.getNumEdges ()){btVector3 a, b; convex.getEdge(i, a, b); VI.line(Bullet.vec(a), Bullet.vec(b));}
  134. //REP(convex.getNumVertices()){btVector3 v; convex.getVertex(i, v ); VI.dot (Bullet.vec(v));}
  135. //REP(convex.getNumPoints ())VI.dot(Bullet.vec(convex.getUnscaledPoints()[i]));
  136. VI.end();
  137. }
  138. void DrawMesh(btBvhTriangleMeshShape &mesh, C Color &color, Bool fill)
  139. {
  140. if(btStridingMeshInterface *smi=mesh.getMeshInterface())
  141. {
  142. Vec *pos;
  143. VecI *ind;
  144. int vtxs, tris, vtx_stride, ind_stride;
  145. PHY_ScalarType vtx_type, ind_type;
  146. int subpart=0;
  147. smi->getLockedReadOnlyVertexIndexBase((const unsigned char**)&pos, vtxs, vtx_type, vtx_stride, (const unsigned char**)&ind, ind_stride, tris, ind_type, subpart);
  148. if(vtx_type==PHY_FLOAT && ind_type==PHY_INTEGER)
  149. {
  150. VI.color(color);
  151. REP(tris)
  152. {
  153. VecI v=ind[i];
  154. if(fill)VI.tri(pos[v.x], pos[v.y], pos[v.z]);else
  155. {
  156. VI.line(pos[v.x], pos[v.y]);
  157. VI.line(pos[v.y], pos[v.z]);
  158. VI.line(pos[v.z], pos[v.x]);
  159. }
  160. }
  161. VI.end();
  162. }
  163. smi->unLockReadOnlyVertexBase(subpart);
  164. }
  165. }
  166. static void Draw(btCollisionShape *shape, C Color &color, Bool fill, C Matrix &matrix)
  167. {
  168. if(shape)
  169. {
  170. if(btCompoundShape *compound=CAST(btCompoundShape , shape)){REP(compound->getNumChildShapes())Draw(compound->getChildShape(i), color, fill, Bullet.matrix(compound->getChildTransform(i))*matrix);}else
  171. if(btUniformScalingShape *uni =CAST(btUniformScalingShape , shape))Draw(uni ->getChildShape(), color, fill, Matrix(matrix).scaleOrn ( uni ->getUniformScalingFactor() ));else
  172. if(btScaledBvhTriangleMeshShape *mesh =CAST(btScaledBvhTriangleMeshShape, shape))Draw(mesh->getChildShape(), color, fill, Matrix(matrix).scaleOrnL(Bullet.vec(mesh->getLocalScaling ())));else
  173. {
  174. SetMatrix(matrix);
  175. if(btBoxShape *box =CAST(btBoxShape , shape)){Vec ext=Bullet.vec(box->getHalfExtentsWithMargin()); Box(-ext, ext).draw(color, fill);}else
  176. if(btSphereShape *ball =CAST(btSphereShape , shape)){Ball(ball->getRadius()).draw(color, fill);}else
  177. if(btCapsuleShape *capsule=CAST(btCapsuleShape , shape)){Flt r=capsule->getRadius(); Capsule(r, (capsule->getHalfHeight()+r)*2).draw(color, fill);}else
  178. if(btCylinderShape *tube =CAST(btCylinderShape , shape)){Tube(tube->getRadius(), tube->getHalfExtentsWithMargin().y()*2).draw(color, fill);}else
  179. if(btStaticPlaneShape *plane =CAST(btStaticPlaneShape , shape)){Vec n=Bullet.vec(plane->getPlaneNormal()); Plane(plane->getPlaneConstant()*n, n).drawInfiniteByResolution(color);}else
  180. if(btConvexHullShape *convex =CAST(btConvexHullShape , shape))DrawConvex(*convex, color, fill);else
  181. if(btBvhTriangleMeshShape *mesh =CAST(btBvhTriangleMeshShape, shape))DrawMesh (*mesh , color, fill);
  182. }
  183. }
  184. }
  185. void Actor ::draw(C Color &color, Bool fill)C {if(_actor)Draw(_actor->getCollisionShape(), color, fill, massCenterMatrix());}
  186. void PhysicsClass::draw( )
  187. {
  188. ReadLock lock(_rws);
  189. if(Bullet.world)
  190. REP(Bullet.world->getNumCollisionObjects())
  191. if(btCollisionObject *obj=Bullet.world->getCollisionObjectArray()[i])
  192. Draw(obj->getCollisionShape(), WHITE, false, Bullet.matrix(obj->getWorldTransform()));
  193. }
  194. #endif
  195. /******************************************************************************/
  196. void DrawPhys(C MeshBase &base, C Color &color, Bool fill)
  197. {
  198. if(C Vec *pos=base.vtx.pos())
  199. {
  200. VI.color(color);
  201. if(fill)
  202. {
  203. REPA(base.tri ){C VecI &ind=base.tri .ind(i); VI.tri (pos[ind.x], pos[ind.y], pos[ind.z]);}
  204. REPA(base.quad){C VecI4 &ind=base.quad.ind(i); VI.quad(pos[ind.x], pos[ind.y], pos[ind.z], pos[ind.w]);}
  205. }else
  206. {
  207. REPA(base.tri ){C VecI &ind=base.tri .ind(i); VI.line(pos[ind.x], pos[ind.y]); VI.line(pos[ind.y], pos[ind.z]); VI.line(pos[ind.z], pos[ind.x]);}
  208. REPA(base.quad){C VecI4 &ind=base.quad.ind(i); VI.line(pos[ind.x], pos[ind.y]); VI.line(pos[ind.y], pos[ind.z]); VI.line(pos[ind.z], pos[ind.w]); VI.line(pos[ind.w], pos[ind.x]);}
  209. }
  210. VI.end();
  211. }
  212. }
  213. /******************************************************************************/
  214. }
  215. /******************************************************************************/