btIDebugDraw.h

Go to the documentation of this file.
00001 /*
00002 Bullet Continuous Collision Detection and Physics Library
00003 Copyright (c) 2003-2009 Erwin Coumans  http://bulletphysics.org
00004 
00005 This software is provided 'as-is', without any express or implied warranty.
00006 In no event will the authors be held liable for any damages arising from the use of this software.
00007 Permission is granted to anyone to use this software for any purpose, 
00008 including commercial applications, and to alter it and redistribute it freely, 
00009 subject to the following restrictions:
00010 
00011 1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required.
00012 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
00013 3. This notice may not be removed or altered from any source distribution.
00014 */
00015 
00016 
00017 #ifndef BT_IDEBUG_DRAW__H
00018 #define BT_IDEBUG_DRAW__H
00019 
00020 #include "btVector3.h"
00021 #include "btTransform.h"
00022 
00023 
00028 class   btIDebugDraw
00029 {
00030         public:
00031 
00032         enum    DebugDrawModes
00033         {
00034                 DBG_NoDebug=0,
00035                 DBG_DrawWireframe = 1,
00036                 DBG_DrawAabb=2,
00037                 DBG_DrawFeaturesText=4,
00038                 DBG_DrawContactPoints=8,
00039                 DBG_NoDeactivation=16,
00040                 DBG_NoHelpText = 32,
00041                 DBG_DrawText=64,
00042                 DBG_ProfileTimings = 128,
00043                 DBG_EnableSatComparison = 256,
00044                 DBG_DisableBulletLCP = 512,
00045                 DBG_EnableCCD = 1024,
00046                 DBG_DrawConstraints = (1 << 11),
00047                 DBG_DrawConstraintLimits = (1 << 12),
00048                 DBG_FastWireframe = (1<<13),
00049         DBG_DrawNormals = (1<<14),
00050                 DBG_MAX_DEBUG_DRAW_MODE
00051         };
00052 
00053         virtual ~btIDebugDraw() {};
00054 
00055         virtual void    drawLine(const btVector3& from,const btVector3& to,const btVector3& color)=0;
00056                 
00057         virtual void    drawLine(const btVector3& from,const btVector3& to, const btVector3& fromColor, const btVector3& toColor)
00058         {
00059         (void) toColor;
00060                 drawLine (from, to, fromColor);
00061         }
00062 
00063         virtual void    drawSphere(btScalar radius, const btTransform& transform, const btVector3& color)
00064         {
00065                 btVector3 start = transform.getOrigin();
00066 
00067                 const btVector3 xoffs = transform.getBasis() * btVector3(radius,0,0);
00068                 const btVector3 yoffs = transform.getBasis() * btVector3(0,radius,0);
00069                 const btVector3 zoffs = transform.getBasis() * btVector3(0,0,radius);
00070 
00071                 // XY 
00072                 drawLine(start-xoffs, start+yoffs, color);
00073                 drawLine(start+yoffs, start+xoffs, color);
00074                 drawLine(start+xoffs, start-yoffs, color);
00075                 drawLine(start-yoffs, start-xoffs, color);
00076 
00077                 // XZ
00078                 drawLine(start-xoffs, start+zoffs, color);
00079                 drawLine(start+zoffs, start+xoffs, color);
00080                 drawLine(start+xoffs, start-zoffs, color);
00081                 drawLine(start-zoffs, start-xoffs, color);
00082 
00083                 // YZ
00084                 drawLine(start-yoffs, start+zoffs, color);
00085                 drawLine(start+zoffs, start+yoffs, color);
00086                 drawLine(start+yoffs, start-zoffs, color);
00087                 drawLine(start-zoffs, start-yoffs, color);
00088         }
00089         
00090         virtual void    drawSphere (const btVector3& p, btScalar radius, const btVector3& color)
00091         {
00092                 btTransform tr;
00093                 tr.setIdentity();
00094                 tr.setOrigin(p);
00095                 drawSphere(radius,tr,color);
00096         }
00097         
00098         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)
00099         {
00100                 drawTriangle(v0,v1,v2,color,alpha);
00101         }
00102         virtual void    drawTriangle(const btVector3& v0,const btVector3& v1,const btVector3& v2,const btVector3& color, btScalar /*alpha*/)
00103         {
00104                 drawLine(v0,v1,color);
00105                 drawLine(v1,v2,color);
00106                 drawLine(v2,v0,color);
00107         }
00108 
00109         virtual void    drawContactPoint(const btVector3& PointOnB,const btVector3& normalOnB,btScalar distance,int lifeTime,const btVector3& color)=0;
00110 
00111         virtual void    reportErrorWarning(const char* warningString) = 0;
00112 
00113         virtual void    draw3dText(const btVector3& location,const char* textString) = 0;
00114         
00115         virtual void    setDebugMode(int debugMode) =0;
00116         
00117         virtual int             getDebugMode() const = 0;
00118 
00119         virtual void drawAabb(const btVector3& from,const btVector3& to,const btVector3& color)
00120         {
00121 
00122                 btVector3 halfExtents = (to-from)* 0.5f;
00123                 btVector3 center = (to+from) *0.5f;
00124                 int i,j;
00125 
00126                 btVector3 edgecoord(1.f,1.f,1.f),pa,pb;
00127                 for (i=0;i<4;i++)
00128                 {
00129                         for (j=0;j<3;j++)
00130                         {
00131                                 pa = btVector3(edgecoord[0]*halfExtents[0], edgecoord[1]*halfExtents[1],                
00132                                         edgecoord[2]*halfExtents[2]);
00133                                 pa+=center;
00134 
00135                                 int othercoord = j%3;
00136                                 edgecoord[othercoord]*=-1.f;
00137                                 pb = btVector3(edgecoord[0]*halfExtents[0], edgecoord[1]*halfExtents[1],        
00138                                         edgecoord[2]*halfExtents[2]);
00139                                 pb+=center;
00140 
00141                                 drawLine(pa,pb,color);
00142                         }
00143                         edgecoord = btVector3(-1.f,-1.f,-1.f);
00144                         if (i<3)
00145                                 edgecoord[i]*=-1.f;
00146                 }
00147         }
00148         virtual void drawTransform(const btTransform& transform, btScalar orthoLen)
00149         {
00150                 btVector3 start = transform.getOrigin();
00151                 drawLine(start, start+transform.getBasis() * btVector3(orthoLen, 0, 0), btVector3(0.7f,0,0));
00152                 drawLine(start, start+transform.getBasis() * btVector3(0, orthoLen, 0), btVector3(0,0.7f,0));
00153                 drawLine(start, start+transform.getBasis() * btVector3(0, 0, orthoLen), btVector3(0,0,0.7f));
00154         }
00155 
00156         virtual void drawArc(const btVector3& center, const btVector3& normal, const btVector3& axis, btScalar radiusA, btScalar radiusB, btScalar minAngle, btScalar maxAngle, 
00157                                 const btVector3& color, bool drawSect, btScalar stepDegrees = btScalar(10.f))
00158         {
00159                 const btVector3& vx = axis;
00160                 btVector3 vy = normal.cross(axis);
00161                 btScalar step = stepDegrees * SIMD_RADS_PER_DEG;
00162                 int nSteps = (int)((maxAngle - minAngle) / step);
00163                 if(!nSteps) nSteps = 1;
00164                 btVector3 prev = center + radiusA * vx * btCos(minAngle) + radiusB * vy * btSin(minAngle);
00165                 if(drawSect)
00166                 {
00167                         drawLine(center, prev, color);
00168                 }
00169                 for(int i = 1; i <= nSteps; i++)
00170                 {
00171                         btScalar angle = minAngle + (maxAngle - minAngle) * btScalar(i) / btScalar(nSteps);
00172                         btVector3 next = center + radiusA * vx * btCos(angle) + radiusB * vy * btSin(angle);
00173                         drawLine(prev, next, color);
00174                         prev = next;
00175                 }
00176                 if(drawSect)
00177                 {
00178                         drawLine(center, prev, color);
00179                 }
00180         }
00181         virtual void drawSpherePatch(const btVector3& center, const btVector3& up, const btVector3& axis, btScalar radius, 
00182                 btScalar minTh, btScalar maxTh, btScalar minPs, btScalar maxPs, const btVector3& color, btScalar stepDegrees = btScalar(10.f))
00183         {
00184                 btVector3 vA[74];
00185                 btVector3 vB[74];
00186                 btVector3 *pvA = vA, *pvB = vB, *pT;
00187                 btVector3 npole = center + up * radius;
00188                 btVector3 spole = center - up * radius;
00189                 btVector3 arcStart;
00190                 btScalar step = stepDegrees * SIMD_RADS_PER_DEG;
00191                 const btVector3& kv = up;
00192                 const btVector3& iv = axis;
00193                 btVector3 jv = kv.cross(iv);
00194                 bool drawN = false;
00195                 bool drawS = false;
00196                 if(minTh <= -SIMD_HALF_PI)
00197                 {
00198                         minTh = -SIMD_HALF_PI + step;
00199                         drawN = true;
00200                 }
00201                 if(maxTh >= SIMD_HALF_PI)
00202                 {
00203                         maxTh = SIMD_HALF_PI - step;
00204                         drawS = true;
00205                 }
00206                 if(minTh > maxTh)
00207                 {
00208                         minTh = -SIMD_HALF_PI + step;
00209                         maxTh =  SIMD_HALF_PI - step;
00210                         drawN = drawS = true;
00211                 }
00212                 int n_hor = (int)((maxTh - minTh) / step) + 1;
00213                 if(n_hor < 2) n_hor = 2;
00214                 btScalar step_h = (maxTh - minTh) / btScalar(n_hor - 1);
00215                 bool isClosed = false;
00216                 if(minPs > maxPs)
00217                 {
00218                         minPs = -SIMD_PI + step;
00219                         maxPs =  SIMD_PI;
00220                         isClosed = true;
00221                 }
00222                 else if((maxPs - minPs) >= SIMD_PI * btScalar(2.f))
00223                 {
00224                         isClosed = true;
00225                 }
00226                 else
00227                 {
00228                         isClosed = false;
00229                 }
00230                 int n_vert = (int)((maxPs - minPs) / step) + 1;
00231                 if(n_vert < 2) n_vert = 2;
00232                 btScalar step_v = (maxPs - minPs) / btScalar(n_vert - 1);
00233                 for(int i = 0; i < n_hor; i++)
00234                 {
00235                         btScalar th = minTh + btScalar(i) * step_h;
00236                         btScalar sth = radius * btSin(th);
00237                         btScalar cth = radius * btCos(th);
00238                         for(int j = 0; j < n_vert; j++)
00239                         {
00240                                 btScalar psi = minPs + btScalar(j) * step_v;
00241                                 btScalar sps = btSin(psi);
00242                                 btScalar cps = btCos(psi);
00243                                 pvB[j] = center + cth * cps * iv + cth * sps * jv + sth * kv;
00244                                 if(i)
00245                                 {
00246                                         drawLine(pvA[j], pvB[j], color);
00247                                 }
00248                                 else if(drawS)
00249                                 {
00250                                         drawLine(spole, pvB[j], color);
00251                                 }
00252                                 if(j)
00253                                 {
00254                                         drawLine(pvB[j-1], pvB[j], color);
00255                                 }
00256                                 else
00257                                 {
00258                                         arcStart = pvB[j];
00259                                 }
00260                                 if((i == (n_hor - 1)) && drawN)
00261                                 {
00262                                         drawLine(npole, pvB[j], color);
00263                                 }
00264                                 if(isClosed)
00265                                 {
00266                                         if(j == (n_vert-1))
00267                                         {
00268                                                 drawLine(arcStart, pvB[j], color);
00269                                         }
00270                                 }
00271                                 else
00272                                 {
00273                                         if(((!i) || (i == (n_hor-1))) && ((!j) || (j == (n_vert-1))))
00274                                         {
00275                                                 drawLine(center, pvB[j], color);
00276                                         }
00277                                 }
00278                         }
00279                         pT = pvA; pvA = pvB; pvB = pT;
00280                 }
00281         }
00282         
00283   
00284         virtual void drawBox(const btVector3& bbMin, const btVector3& bbMax, const btVector3& color)
00285         {
00286                 drawLine(btVector3(bbMin[0], bbMin[1], bbMin[2]), btVector3(bbMax[0], bbMin[1], bbMin[2]), color);
00287                 drawLine(btVector3(bbMax[0], bbMin[1], bbMin[2]), btVector3(bbMax[0], bbMax[1], bbMin[2]), color);
00288                 drawLine(btVector3(bbMax[0], bbMax[1], bbMin[2]), btVector3(bbMin[0], bbMax[1], bbMin[2]), color);
00289                 drawLine(btVector3(bbMin[0], bbMax[1], bbMin[2]), btVector3(bbMin[0], bbMin[1], bbMin[2]), color);
00290                 drawLine(btVector3(bbMin[0], bbMin[1], bbMin[2]), btVector3(bbMin[0], bbMin[1], bbMax[2]), color);
00291                 drawLine(btVector3(bbMax[0], bbMin[1], bbMin[2]), btVector3(bbMax[0], bbMin[1], bbMax[2]), color);
00292                 drawLine(btVector3(bbMax[0], bbMax[1], bbMin[2]), btVector3(bbMax[0], bbMax[1], bbMax[2]), color);
00293                 drawLine(btVector3(bbMin[0], bbMax[1], bbMin[2]), btVector3(bbMin[0], bbMax[1], bbMax[2]), color);
00294                 drawLine(btVector3(bbMin[0], bbMin[1], bbMax[2]), btVector3(bbMax[0], bbMin[1], bbMax[2]), color);
00295                 drawLine(btVector3(bbMax[0], bbMin[1], bbMax[2]), btVector3(bbMax[0], bbMax[1], bbMax[2]), color);
00296                 drawLine(btVector3(bbMax[0], bbMax[1], bbMax[2]), btVector3(bbMin[0], bbMax[1], bbMax[2]), color);
00297                 drawLine(btVector3(bbMin[0], bbMax[1], bbMax[2]), btVector3(bbMin[0], bbMin[1], bbMax[2]), color);
00298         }
00299         virtual void drawBox(const btVector3& bbMin, const btVector3& bbMax, const btTransform& trans, const btVector3& color)
00300         {
00301                 drawLine(trans * btVector3(bbMin[0], bbMin[1], bbMin[2]), trans * btVector3(bbMax[0], bbMin[1], bbMin[2]), color);
00302                 drawLine(trans * btVector3(bbMax[0], bbMin[1], bbMin[2]), trans * btVector3(bbMax[0], bbMax[1], bbMin[2]), color);
00303                 drawLine(trans * btVector3(bbMax[0], bbMax[1], bbMin[2]), trans * btVector3(bbMin[0], bbMax[1], bbMin[2]), color);
00304                 drawLine(trans * btVector3(bbMin[0], bbMax[1], bbMin[2]), trans * btVector3(bbMin[0], bbMin[1], bbMin[2]), color);
00305                 drawLine(trans * btVector3(bbMin[0], bbMin[1], bbMin[2]), trans * btVector3(bbMin[0], bbMin[1], bbMax[2]), color);
00306                 drawLine(trans * btVector3(bbMax[0], bbMin[1], bbMin[2]), trans * btVector3(bbMax[0], bbMin[1], bbMax[2]), color);
00307                 drawLine(trans * btVector3(bbMax[0], bbMax[1], bbMin[2]), trans * btVector3(bbMax[0], bbMax[1], bbMax[2]), color);
00308                 drawLine(trans * btVector3(bbMin[0], bbMax[1], bbMin[2]), trans * btVector3(bbMin[0], bbMax[1], bbMax[2]), color);
00309                 drawLine(trans * btVector3(bbMin[0], bbMin[1], bbMax[2]), trans * btVector3(bbMax[0], bbMin[1], bbMax[2]), color);
00310                 drawLine(trans * btVector3(bbMax[0], bbMin[1], bbMax[2]), trans * btVector3(bbMax[0], bbMax[1], bbMax[2]), color);
00311                 drawLine(trans * btVector3(bbMax[0], bbMax[1], bbMax[2]), trans * btVector3(bbMin[0], bbMax[1], bbMax[2]), color);
00312                 drawLine(trans * btVector3(bbMin[0], bbMax[1], bbMax[2]), trans * btVector3(bbMin[0], bbMin[1], bbMax[2]), color);
00313         }
00314 
00315         virtual void drawCapsule(btScalar radius, btScalar halfHeight, int upAxis, const btTransform& transform, const btVector3& color)
00316         {
00317                 btVector3 capStart(0.f,0.f,0.f);
00318                 capStart[upAxis] = -halfHeight;
00319 
00320                 btVector3 capEnd(0.f,0.f,0.f);
00321                 capEnd[upAxis] = halfHeight;
00322 
00323                 // Draw the ends
00324                 {
00325 
00326                         btTransform childTransform = transform;
00327                         childTransform.getOrigin() = transform * capStart;
00328                         drawSphere(radius, childTransform, color);
00329                 }
00330 
00331                 {
00332                         btTransform childTransform = transform;
00333                         childTransform.getOrigin() = transform * capEnd;
00334                         drawSphere(radius, childTransform, color);
00335                 }
00336 
00337                 // Draw some additional lines
00338                 btVector3 start = transform.getOrigin();
00339 
00340                 capStart[(upAxis+1)%3] = radius;
00341                 capEnd[(upAxis+1)%3] = radius;
00342                 drawLine(start+transform.getBasis() * capStart,start+transform.getBasis() * capEnd, color);
00343                 capStart[(upAxis+1)%3] = -radius;
00344                 capEnd[(upAxis+1)%3] = -radius;
00345                 drawLine(start+transform.getBasis() * capStart,start+transform.getBasis() * capEnd, color);
00346 
00347                 capStart[(upAxis+1)%3] = 0.f;
00348                 capEnd[(upAxis+1)%3] = 0.f;
00349 
00350                 capStart[(upAxis+2)%3] = radius;
00351                 capEnd[(upAxis+2)%3] = radius;
00352                 drawLine(start+transform.getBasis() * capStart,start+transform.getBasis() * capEnd, color);
00353                 capStart[(upAxis+2)%3] = -radius;
00354                 capEnd[(upAxis+2)%3] = -radius;
00355                 drawLine(start+transform.getBasis() * capStart,start+transform.getBasis() * capEnd, color);
00356         }
00357 
00358         virtual void drawCylinder(btScalar radius, btScalar halfHeight, int upAxis, const btTransform& transform, const btVector3& color)
00359         {
00360                 btVector3 start = transform.getOrigin();
00361                 btVector3       offsetHeight(0,0,0);
00362                 offsetHeight[upAxis] = halfHeight;
00363                 btVector3       offsetRadius(0,0,0);
00364                 offsetRadius[(upAxis+1)%3] = radius;
00365                 drawLine(start+transform.getBasis() * (offsetHeight+offsetRadius),start+transform.getBasis() * (-offsetHeight+offsetRadius),color);
00366                 drawLine(start+transform.getBasis() * (offsetHeight-offsetRadius),start+transform.getBasis() * (-offsetHeight-offsetRadius),color);
00367 
00368                 // Drawing top and bottom caps of the cylinder
00369                 btVector3 yaxis(0,0,0);
00370                 yaxis[upAxis] = btScalar(1.0);
00371                 btVector3 xaxis(0,0,0);
00372                 xaxis[(upAxis+1)%3] = btScalar(1.0);
00373                 drawArc(start-transform.getBasis()*(offsetHeight),transform.getBasis()*yaxis,transform.getBasis()*xaxis,radius,radius,0,SIMD_2_PI,color,false,btScalar(10.0));
00374                 drawArc(start+transform.getBasis()*(offsetHeight),transform.getBasis()*yaxis,transform.getBasis()*xaxis,radius,radius,0,SIMD_2_PI,color,false,btScalar(10.0));
00375         }
00376 
00377         virtual void drawCone(btScalar radius, btScalar height, int upAxis, const btTransform& transform, const btVector3& color)
00378         {
00379 
00380                 btVector3 start = transform.getOrigin();
00381 
00382                 btVector3       offsetHeight(0,0,0);
00383                 offsetHeight[upAxis] = height * btScalar(0.5);
00384                 btVector3       offsetRadius(0,0,0);
00385                 offsetRadius[(upAxis+1)%3] = radius;
00386                 btVector3       offset2Radius(0,0,0);
00387                 offset2Radius[(upAxis+2)%3] = radius;
00388 
00389                 drawLine(start+transform.getBasis() * (offsetHeight),start+transform.getBasis() * (-offsetHeight+offsetRadius),color);
00390                 drawLine(start+transform.getBasis() * (offsetHeight),start+transform.getBasis() * (-offsetHeight-offsetRadius),color);
00391                 drawLine(start+transform.getBasis() * (offsetHeight),start+transform.getBasis() * (-offsetHeight+offset2Radius),color);
00392                 drawLine(start+transform.getBasis() * (offsetHeight),start+transform.getBasis() * (-offsetHeight-offset2Radius),color);
00393 
00394                 // Drawing the base of the cone
00395                 btVector3 yaxis(0,0,0);
00396                 yaxis[upAxis] = btScalar(1.0);
00397                 btVector3 xaxis(0,0,0);
00398                 xaxis[(upAxis+1)%3] = btScalar(1.0);
00399                 drawArc(start-transform.getBasis()*(offsetHeight),transform.getBasis()*yaxis,transform.getBasis()*xaxis,radius,radius,0,SIMD_2_PI,color,false,10.0);
00400         }
00401 
00402         virtual void drawPlane(const btVector3& planeNormal, btScalar planeConst, const btTransform& transform, const btVector3& color)
00403         {
00404                 btVector3 planeOrigin = planeNormal * planeConst;
00405                 btVector3 vec0,vec1;
00406                 btPlaneSpace1(planeNormal,vec0,vec1);
00407                 btScalar vecLen = 100.f;
00408                 btVector3 pt0 = planeOrigin + vec0*vecLen;
00409                 btVector3 pt1 = planeOrigin - vec0*vecLen;
00410                 btVector3 pt2 = planeOrigin + vec1*vecLen;
00411                 btVector3 pt3 = planeOrigin - vec1*vecLen;
00412                 drawLine(transform*pt0,transform*pt1,color);
00413                 drawLine(transform*pt2,transform*pt3,color);
00414         }
00415 };
00416 
00417 
00418 #endif //BT_IDEBUG_DRAW__H
00419