| 1 | /* | 
|---|
| 2 | Copyright (c) 2005 Gino van den Bergen / Erwin Coumans http://continuousphysics.com | 
|---|
| 3 |  | 
|---|
| 4 | Permission is hereby granted, free of charge, to any person or organization | 
|---|
| 5 | obtaining a copy of the software and accompanying documentation covered by | 
|---|
| 6 | this license (the "Software") to use, reproduce, display, distribute, | 
|---|
| 7 | execute, and transmit the Software, and to prepare derivative works of the | 
|---|
| 8 | Software, and to permit third-parties to whom the Software is furnished to | 
|---|
| 9 | do so, all subject to the following: | 
|---|
| 10 |  | 
|---|
| 11 | The copyright notices in the Software and this entire statement, including | 
|---|
| 12 | the above license grant, this restriction and the following disclaimer, | 
|---|
| 13 | must be included in all copies of the Software, in whole or in part, and | 
|---|
| 14 | all derivative works of the Software, unless such copies or derivative | 
|---|
| 15 | works are solely in the form of machine-executable object code generated by | 
|---|
| 16 | a source language processor. | 
|---|
| 17 |  | 
|---|
| 18 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | 
|---|
| 19 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | 
|---|
| 20 | FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT | 
|---|
| 21 | SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE | 
|---|
| 22 | FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, | 
|---|
| 23 | ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER | 
|---|
| 24 | DEALINGS IN THE SOFTWARE. | 
|---|
| 25 | */ | 
|---|
| 26 |  | 
|---|
| 27 |  | 
|---|
| 28 | #ifndef IDEBUG_DRAW__H | 
|---|
| 29 | #define IDEBUG_DRAW__H | 
|---|
| 30 |  | 
|---|
| 31 | #include "btVector3.h" | 
|---|
| 32 | #include "btTransform.h" | 
|---|
| 33 |  | 
|---|
| 34 |  | 
|---|
| 35 | ///The btIDebugDraw interface class allows hooking up a debug renderer to visually debug simulations. | 
|---|
| 36 | ///Typical use case: create a debug drawer object, and assign it to a btCollisionWorld or btDynamicsWorld using setDebugDrawer and call debugDrawWorld. | 
|---|
| 37 | ///A class that implements the btIDebugDraw interface has to implement the drawLine method at a minimum. | 
|---|
| 38 | class   btIDebugDraw | 
|---|
| 39 | { | 
|---|
| 40 |         public: | 
|---|
| 41 |  | 
|---|
| 42 |         enum    DebugDrawModes | 
|---|
| 43 |         { | 
|---|
| 44 |                 DBG_NoDebug=0, | 
|---|
| 45 |                 DBG_DrawWireframe = 1, | 
|---|
| 46 |                 DBG_DrawAabb=2, | 
|---|
| 47 |                 DBG_DrawFeaturesText=4, | 
|---|
| 48 |                 DBG_DrawContactPoints=8, | 
|---|
| 49 |                 DBG_NoDeactivation=16, | 
|---|
| 50 |                 DBG_NoHelpText = 32, | 
|---|
| 51 |                 DBG_DrawText=64, | 
|---|
| 52 |                 DBG_ProfileTimings = 128, | 
|---|
| 53 |                 DBG_EnableSatComparison = 256, | 
|---|
| 54 |                 DBG_DisableBulletLCP = 512, | 
|---|
| 55 |                 DBG_EnableCCD = 1024, | 
|---|
| 56 |                 DBG_DrawConstraints = (1 << 11), | 
|---|
| 57 |                 DBG_DrawConstraintLimits = (1 << 12), | 
|---|
| 58 |                 DBG_MAX_DEBUG_DRAW_MODE | 
|---|
| 59 |         }; | 
|---|
| 60 |  | 
|---|
| 61 |         virtual ~btIDebugDraw() {}; | 
|---|
| 62 |  | 
|---|
| 63 |         virtual void    drawLine(const btVector3& from,const btVector3& to, const btVector3& fromColor, const btVector3& toColor) | 
|---|
| 64 |         { | 
|---|
| 65 |                 drawLine (from, to, fromColor); | 
|---|
| 66 |         } | 
|---|
| 67 |  | 
|---|
| 68 |         virtual void    drawBox (const btVector3& boxMin, const btVector3& boxMax, const btVector3& color, btScalar alpha) | 
|---|
| 69 |         { | 
|---|
| 70 |         } | 
|---|
| 71 |  | 
|---|
| 72 |         virtual void    drawSphere (const btVector3& p, btScalar radius, const btVector3& color) | 
|---|
| 73 |         { | 
|---|
| 74 |         } | 
|---|
| 75 |  | 
|---|
| 76 |         virtual void    drawLine(const btVector3& from,const btVector3& to,const btVector3& color)=0; | 
|---|
| 77 |          | 
|---|
| 78 |         virtual void    drawTriangle(const btVector3& v0,const btVector3& v1,const btVector3& v2,const btVector3& /*n0*/,const btVector3& /*n1*/,const btVector3& /*n2*/,const btVector3& color, btScalar alpha) | 
|---|
| 79 |         { | 
|---|
| 80 |                 drawTriangle(v0,v1,v2,color,alpha); | 
|---|
| 81 |         } | 
|---|
| 82 |         virtual void    drawTriangle(const btVector3& v0,const btVector3& v1,const btVector3& v2,const btVector3& color, btScalar /*alpha*/) | 
|---|
| 83 |         { | 
|---|
| 84 |                 drawLine(v0,v1,color); | 
|---|
| 85 |                 drawLine(v1,v2,color); | 
|---|
| 86 |                 drawLine(v2,v0,color); | 
|---|
| 87 |         } | 
|---|
| 88 |  | 
|---|
| 89 |         virtual void    drawContactPoint(const btVector3& PointOnB,const btVector3& normalOnB,btScalar distance,int lifeTime,const btVector3& color)=0; | 
|---|
| 90 |  | 
|---|
| 91 |         virtual void    reportErrorWarning(const char* warningString) = 0; | 
|---|
| 92 |  | 
|---|
| 93 |         virtual void    draw3dText(const btVector3& location,const char* textString) = 0; | 
|---|
| 94 |          | 
|---|
| 95 |         virtual void    setDebugMode(int debugMode) =0; | 
|---|
| 96 |          | 
|---|
| 97 |         virtual int             getDebugMode() const = 0; | 
|---|
| 98 |  | 
|---|
| 99 |         inline void drawAabb(const btVector3& from,const btVector3& to,const btVector3& color) | 
|---|
| 100 |         { | 
|---|
| 101 |  | 
|---|
| 102 |                 btVector3 halfExtents = (to-from)* 0.5f; | 
|---|
| 103 |                 btVector3 center = (to+from) *0.5f; | 
|---|
| 104 |                 int i,j; | 
|---|
| 105 |  | 
|---|
| 106 |                 btVector3 edgecoord(1.f,1.f,1.f),pa,pb; | 
|---|
| 107 |                 for (i=0;i<4;i++) | 
|---|
| 108 |                 { | 
|---|
| 109 |                         for (j=0;j<3;j++) | 
|---|
| 110 |                         { | 
|---|
| 111 |                                 pa = btVector3(edgecoord[0]*halfExtents[0], edgecoord[1]*halfExtents[1],                 | 
|---|
| 112 |                                         edgecoord[2]*halfExtents[2]); | 
|---|
| 113 |                                 pa+=center; | 
|---|
| 114 |  | 
|---|
| 115 |                                 int othercoord = j%3; | 
|---|
| 116 |                                 edgecoord[othercoord]*=-1.f; | 
|---|
| 117 |                                 pb = btVector3(edgecoord[0]*halfExtents[0], edgecoord[1]*halfExtents[1],         | 
|---|
| 118 |                                         edgecoord[2]*halfExtents[2]); | 
|---|
| 119 |                                 pb+=center; | 
|---|
| 120 |  | 
|---|
| 121 |                                 drawLine(pa,pb,color); | 
|---|
| 122 |                         } | 
|---|
| 123 |                         edgecoord = btVector3(-1.f,-1.f,-1.f); | 
|---|
| 124 |                         if (i<3) | 
|---|
| 125 |                                 edgecoord[i]*=-1.f; | 
|---|
| 126 |                 } | 
|---|
| 127 |         } | 
|---|
| 128 |         void drawTransform(const btTransform& transform, btScalar orthoLen) | 
|---|
| 129 |         { | 
|---|
| 130 |                 btVector3 start = transform.getOrigin(); | 
|---|
| 131 |                 drawLine(start, start+transform.getBasis() * btVector3(orthoLen, 0, 0), btVector3(0.7f,0,0)); | 
|---|
| 132 |                 drawLine(start, start+transform.getBasis() * btVector3(0, orthoLen, 0), btVector3(0,0.7f,0)); | 
|---|
| 133 |                 drawLine(start, start+transform.getBasis() * btVector3(0, 0, orthoLen), btVector3(0,0,0.7f)); | 
|---|
| 134 |         } | 
|---|
| 135 |  | 
|---|
| 136 |         void drawArc(const btVector3& center, const btVector3& normal, const btVector3& axis, btScalar radiusA, btScalar radiusB, btScalar minAngle, btScalar maxAngle,  | 
|---|
| 137 |                                 const btVector3& color, bool drawSect, btScalar stepDegrees = btScalar(10.f)) | 
|---|
| 138 |         { | 
|---|
| 139 |                 const btVector3& vx = axis; | 
|---|
| 140 |                 btVector3 vy = normal.cross(axis); | 
|---|
| 141 |                 btScalar step = stepDegrees * SIMD_RADS_PER_DEG; | 
|---|
| 142 |                 int nSteps = (int)((maxAngle - minAngle) / step); | 
|---|
| 143 |                 if(!nSteps) nSteps = 1; | 
|---|
| 144 |                 btVector3 prev = center + radiusA * vx * btCos(minAngle) + radiusB * vy * btSin(minAngle); | 
|---|
| 145 |                 if(drawSect) | 
|---|
| 146 |                 { | 
|---|
| 147 |                         drawLine(center, prev, color); | 
|---|
| 148 |                 } | 
|---|
| 149 |                 for(int i = 1; i <= nSteps; i++) | 
|---|
| 150 |                 { | 
|---|
| 151 |                         btScalar angle = minAngle + (maxAngle - minAngle) * btScalar(i) / btScalar(nSteps); | 
|---|
| 152 |                         btVector3 next = center + radiusA * vx * btCos(angle) + radiusB * vy * btSin(angle); | 
|---|
| 153 |                         drawLine(prev, next, color); | 
|---|
| 154 |                         prev = next; | 
|---|
| 155 |                 } | 
|---|
| 156 |                 if(drawSect) | 
|---|
| 157 |                 { | 
|---|
| 158 |                         drawLine(center, prev, color); | 
|---|
| 159 |                 } | 
|---|
| 160 |         } | 
|---|
| 161 |         void drawSpherePatch(const btVector3& center, const btVector3& up, const btVector3& axis, btScalar radius,  | 
|---|
| 162 |                 btScalar minTh, btScalar maxTh, btScalar minPs, btScalar maxPs, const btVector3& color, btScalar stepDegrees = btScalar(10.f)) | 
|---|
| 163 |         { | 
|---|
| 164 |                 btVector3 vA[74]; | 
|---|
| 165 |                 btVector3 vB[74]; | 
|---|
| 166 |                 btVector3 *pvA = vA, *pvB = vB, *pT; | 
|---|
| 167 |                 btVector3 npole = center + up * radius; | 
|---|
| 168 |                 btVector3 spole = center - up * radius; | 
|---|
| 169 |                 btVector3 arcStart; | 
|---|
| 170 |                 btScalar step = stepDegrees * SIMD_RADS_PER_DEG; | 
|---|
| 171 |                 const btVector3& kv = up; | 
|---|
| 172 |                 const btVector3& iv = axis; | 
|---|
| 173 |                 btVector3 jv = kv.cross(iv); | 
|---|
| 174 |                 bool drawN = false; | 
|---|
| 175 |                 bool drawS = false; | 
|---|
| 176 |                 if(minTh <= -SIMD_HALF_PI) | 
|---|
| 177 |                 { | 
|---|
| 178 |                         minTh = -SIMD_HALF_PI + step; | 
|---|
| 179 |                         drawN = true; | 
|---|
| 180 |                 } | 
|---|
| 181 |                 if(maxTh >= SIMD_HALF_PI) | 
|---|
| 182 |                 { | 
|---|
| 183 |                         maxTh = SIMD_HALF_PI - step; | 
|---|
| 184 |                         drawS = true; | 
|---|
| 185 |                 } | 
|---|
| 186 |                 if(minTh > maxTh) | 
|---|
| 187 |                 { | 
|---|
| 188 |                         minTh = -SIMD_HALF_PI + step; | 
|---|
| 189 |                         maxTh =  SIMD_HALF_PI - step; | 
|---|
| 190 |                         drawN = drawS = true; | 
|---|
| 191 |                 } | 
|---|
| 192 |                 int n_hor = (int)((maxTh - minTh) / step) + 1; | 
|---|
| 193 |                 if(n_hor < 2) n_hor = 2; | 
|---|
| 194 |                 btScalar step_h = (maxTh - minTh) / btScalar(n_hor - 1); | 
|---|
| 195 |                 bool isClosed = false; | 
|---|
| 196 |                 if(minPs > maxPs) | 
|---|
| 197 |                 { | 
|---|
| 198 |                         minPs = -SIMD_PI + step; | 
|---|
| 199 |                         maxPs =  SIMD_PI; | 
|---|
| 200 |                         isClosed = true; | 
|---|
| 201 |                 } | 
|---|
| 202 |                 else if((maxPs - minPs) >= SIMD_PI * btScalar(2.f)) | 
|---|
| 203 |                 { | 
|---|
| 204 |                         isClosed = true; | 
|---|
| 205 |                 } | 
|---|
| 206 |                 else | 
|---|
| 207 |                 { | 
|---|
| 208 |                         isClosed = false; | 
|---|
| 209 |                 } | 
|---|
| 210 |                 int n_vert = (int)((maxPs - minPs) / step) + 1; | 
|---|
| 211 |                 if(n_vert < 2) n_vert = 2; | 
|---|
| 212 |                 btScalar step_v = (maxPs - minPs) / btScalar(n_vert - 1); | 
|---|
| 213 |                 for(int i = 0; i < n_hor; i++) | 
|---|
| 214 |                 { | 
|---|
| 215 |                         btScalar th = minTh + btScalar(i) * step_h; | 
|---|
| 216 |                         btScalar sth = radius * btSin(th); | 
|---|
| 217 |                         btScalar cth = radius * btCos(th); | 
|---|
| 218 |                         for(int j = 0; j < n_vert; j++) | 
|---|
| 219 |                         { | 
|---|
| 220 |                                 btScalar psi = minPs + btScalar(j) * step_v; | 
|---|
| 221 |                                 btScalar sps = btSin(psi); | 
|---|
| 222 |                                 btScalar cps = btCos(psi); | 
|---|
| 223 |                                 pvB[j] = center + cth * cps * iv + cth * sps * jv + sth * kv; | 
|---|
| 224 |                                 if(i) | 
|---|
| 225 |                                 { | 
|---|
| 226 |                                         drawLine(pvA[j], pvB[j], color); | 
|---|
| 227 |                                 } | 
|---|
| 228 |                                 else if(drawS) | 
|---|
| 229 |                                 { | 
|---|
| 230 |                                         drawLine(spole, pvB[j], color); | 
|---|
| 231 |                                 } | 
|---|
| 232 |                                 if(j) | 
|---|
| 233 |                                 { | 
|---|
| 234 |                                         drawLine(pvB[j-1], pvB[j], color); | 
|---|
| 235 |                                 } | 
|---|
| 236 |                                 else | 
|---|
| 237 |                                 { | 
|---|
| 238 |                                         arcStart = pvB[j]; | 
|---|
| 239 |                                 } | 
|---|
| 240 |                                 if((i == (n_hor - 1)) && drawN) | 
|---|
| 241 |                                 { | 
|---|
| 242 |                                         drawLine(npole, pvB[j], color); | 
|---|
| 243 |                                 } | 
|---|
| 244 |                                 if(isClosed) | 
|---|
| 245 |                                 { | 
|---|
| 246 |                                         if(j == (n_vert-1)) | 
|---|
| 247 |                                         { | 
|---|
| 248 |                                                 drawLine(arcStart, pvB[j], color); | 
|---|
| 249 |                                         } | 
|---|
| 250 |                                 } | 
|---|
| 251 |                                 else | 
|---|
| 252 |                                 { | 
|---|
| 253 |                                         if(((!i) || (i == (n_hor-1))) && ((!j) || (j == (n_vert-1)))) | 
|---|
| 254 |                                         { | 
|---|
| 255 |                                                 drawLine(center, pvB[j], color); | 
|---|
| 256 |                                         } | 
|---|
| 257 |                                 } | 
|---|
| 258 |                         } | 
|---|
| 259 |                         pT = pvA; pvA = pvB; pvB = pT; | 
|---|
| 260 |                 } | 
|---|
| 261 |         } | 
|---|
| 262 |          | 
|---|
| 263 |         void drawBox(const btVector3& bbMin, const btVector3& bbMax, const btVector3& color) | 
|---|
| 264 |         { | 
|---|
| 265 |                 drawLine(btVector3(bbMin[0], bbMin[1], bbMin[2]), btVector3(bbMax[0], bbMin[1], bbMin[2]), color); | 
|---|
| 266 |                 drawLine(btVector3(bbMax[0], bbMin[1], bbMin[2]), btVector3(bbMax[0], bbMax[1], bbMin[2]), color); | 
|---|
| 267 |                 drawLine(btVector3(bbMax[0], bbMax[1], bbMin[2]), btVector3(bbMin[0], bbMax[1], bbMin[2]), color); | 
|---|
| 268 |                 drawLine(btVector3(bbMin[0], bbMax[1], bbMin[2]), btVector3(bbMin[0], bbMin[1], bbMin[2]), color); | 
|---|
| 269 |                 drawLine(btVector3(bbMin[0], bbMin[1], bbMin[2]), btVector3(bbMin[0], bbMin[1], bbMax[2]), color); | 
|---|
| 270 |                 drawLine(btVector3(bbMax[0], bbMin[1], bbMin[2]), btVector3(bbMax[0], bbMin[1], bbMax[2]), color); | 
|---|
| 271 |                 drawLine(btVector3(bbMax[0], bbMax[1], bbMin[2]), btVector3(bbMax[0], bbMax[1], bbMax[2]), color); | 
|---|
| 272 |                 drawLine(btVector3(bbMin[0], bbMax[1], bbMin[2]), btVector3(bbMin[0], bbMax[1], bbMax[2]), color); | 
|---|
| 273 |                 drawLine(btVector3(bbMin[0], bbMin[1], bbMax[2]), btVector3(bbMax[0], bbMin[1], bbMax[2]), color); | 
|---|
| 274 |                 drawLine(btVector3(bbMax[0], bbMin[1], bbMax[2]), btVector3(bbMax[0], bbMax[1], bbMax[2]), color); | 
|---|
| 275 |                 drawLine(btVector3(bbMax[0], bbMax[1], bbMax[2]), btVector3(bbMin[0], bbMax[1], bbMax[2]), color); | 
|---|
| 276 |                 drawLine(btVector3(bbMin[0], bbMax[1], bbMax[2]), btVector3(bbMin[0], bbMin[1], bbMax[2]), color); | 
|---|
| 277 |         } | 
|---|
| 278 |         void drawBox(const btVector3& bbMin, const btVector3& bbMax, const btTransform& trans, const btVector3& color) | 
|---|
| 279 |         { | 
|---|
| 280 |                 drawLine(trans * btVector3(bbMin[0], bbMin[1], bbMin[2]), trans * btVector3(bbMax[0], bbMin[1], bbMin[2]), color); | 
|---|
| 281 |                 drawLine(trans * btVector3(bbMax[0], bbMin[1], bbMin[2]), trans * btVector3(bbMax[0], bbMax[1], bbMin[2]), color); | 
|---|
| 282 |                 drawLine(trans * btVector3(bbMax[0], bbMax[1], bbMin[2]), trans * btVector3(bbMin[0], bbMax[1], bbMin[2]), color); | 
|---|
| 283 |                 drawLine(trans * btVector3(bbMin[0], bbMax[1], bbMin[2]), trans * btVector3(bbMin[0], bbMin[1], bbMin[2]), color); | 
|---|
| 284 |                 drawLine(trans * btVector3(bbMin[0], bbMin[1], bbMin[2]), trans * btVector3(bbMin[0], bbMin[1], bbMax[2]), color); | 
|---|
| 285 |                 drawLine(trans * btVector3(bbMax[0], bbMin[1], bbMin[2]), trans * btVector3(bbMax[0], bbMin[1], bbMax[2]), color); | 
|---|
| 286 |                 drawLine(trans * btVector3(bbMax[0], bbMax[1], bbMin[2]), trans * btVector3(bbMax[0], bbMax[1], bbMax[2]), color); | 
|---|
| 287 |                 drawLine(trans * btVector3(bbMin[0], bbMax[1], bbMin[2]), trans * btVector3(bbMin[0], bbMax[1], bbMax[2]), color); | 
|---|
| 288 |                 drawLine(trans * btVector3(bbMin[0], bbMin[1], bbMax[2]), trans * btVector3(bbMax[0], bbMin[1], bbMax[2]), color); | 
|---|
| 289 |                 drawLine(trans * btVector3(bbMax[0], bbMin[1], bbMax[2]), trans * btVector3(bbMax[0], bbMax[1], bbMax[2]), color); | 
|---|
| 290 |                 drawLine(trans * btVector3(bbMax[0], bbMax[1], bbMax[2]), trans * btVector3(bbMin[0], bbMax[1], bbMax[2]), color); | 
|---|
| 291 |                 drawLine(trans * btVector3(bbMin[0], bbMax[1], bbMax[2]), trans * btVector3(bbMin[0], bbMin[1], bbMax[2]), color); | 
|---|
| 292 |         } | 
|---|
| 293 | }; | 
|---|
| 294 |  | 
|---|
| 295 |  | 
|---|
| 296 | #endif //IDEBUG_DRAW__H | 
|---|
| 297 |  | 
|---|