debugDraw.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #include "platform/platform.h"
  23. #include "gfx/sim/debugDraw.h"
  24. #include "gfx/gFont.h"
  25. #include "gfx/gfxDrawUtil.h"
  26. #include "gfx/gfxTransformSaver.h"
  27. #include "gfx/gfxDebugEvent.h"
  28. #include "math/mathUtils.h"
  29. #include "math/util/frustum.h"
  30. #include "console/console.h"
  31. #include "scene/sceneManager.h"
  32. #include "core/module.h"
  33. #include "console/engineAPI.h"
  34. #include "math/mPolyhedron.impl.h"
  35. MODULE_BEGIN( DebugDrawer )
  36. MODULE_INIT_AFTER( Sim )
  37. MODULE_INIT_AFTER( GFX )
  38. // DebugDrawer will register itself as a SimObject and
  39. // thus get automatically shut down with Sim.
  40. MODULE_INIT
  41. {
  42. DebugDrawer::init();
  43. }
  44. MODULE_END;
  45. DebugDrawer* DebugDrawer::sgDebugDrawer = NULL;
  46. IMPLEMENT_CONOBJECT(DebugDrawer);
  47. ConsoleDocClass( DebugDrawer,
  48. "@brief A debug helper for rendering debug primitives to the scene.\n\n"
  49. "The DebugDrawer is used to render debug primitives to the scene for testing. It is "
  50. "often useful when debugging collision code or complex 3d algorithms to have "
  51. "them draw debug information, like culling hulls or bounding volumes, normals, "
  52. "simple lines, and so forth.\n\n"
  53. "A key feature of the DebugDrawer is that each primitive gets a \"time to live\" (TTL) "
  54. "which allows them to continue to render to the scene for a fixed period of time. You "
  55. "can freeze or resume the system at any time to allow you to examine the output.\n"
  56. "@tsexample\n"
  57. "DebugDraw.drawLine( %player.getMuzzlePoint( 0 ), %hitPoint );\n"
  58. "DebugDraw.setLastTTL( 5000 ); // 5 seconds.\n"
  59. "@endtsexample\n"
  60. "The DebugDrawer renders solely in world space and all primitives are rendered with the "
  61. "cull mode disabled.\n"
  62. "@note This feature can easily be used to cheat in online games, so you should be sure "
  63. "it is disabled in your shipping game. By default the DebugDrawer is disabled in all "
  64. "TORQUE_SHIPPING builds.\n"
  65. "@ingroup GFX\n" );
  66. DebugDrawer::DebugDrawer()
  67. {
  68. mHead = NULL;
  69. isFrozen = false;
  70. shouldToggleFreeze = false;
  71. #ifdef ENABLE_DEBUGDRAW
  72. isDrawing = true;
  73. #else
  74. isDrawing = false;
  75. #endif
  76. }
  77. DebugDrawer::~DebugDrawer()
  78. {
  79. if( sgDebugDrawer == this )
  80. sgDebugDrawer = NULL;
  81. }
  82. DebugDrawer* DebugDrawer::get()
  83. {
  84. if (sgDebugDrawer)
  85. {
  86. return sgDebugDrawer;
  87. } else {
  88. DebugDrawer::init();
  89. return sgDebugDrawer;
  90. }
  91. }
  92. void DebugDrawer::init()
  93. {
  94. #ifdef ENABLE_DEBUGDRAW
  95. sgDebugDrawer = new DebugDrawer();
  96. sgDebugDrawer->registerObject("DebugDraw");
  97. Sim::getRootGroup()->addObject( sgDebugDrawer );
  98. Con::warnf( "DebugDrawer Enabled!" );
  99. #endif
  100. }
  101. void DebugDrawer::setupStateBlocks()
  102. {
  103. GFXStateBlockDesc d;
  104. d.setCullMode(GFXCullNone);
  105. mRenderZOnSB = GFX->createStateBlock(d);
  106. d.setZReadWrite(false);
  107. mRenderZOffSB = GFX->createStateBlock(d);
  108. d.setCullMode(GFXCullCCW);
  109. d.setZReadWrite(true, false);
  110. d.setBlend(true);
  111. mRenderAlpha = GFX->createStateBlock(d);
  112. }
  113. void DebugDrawer::drawBoxOutline(const Point3F &a, const Point3F &b, const ColorF &color)
  114. {
  115. Point3F point0(a.x, a.y, a.z);
  116. Point3F point1(a.x, b.y, a.z);
  117. Point3F point2(b.x, b.y, a.z);
  118. Point3F point3(b.x, a.y, a.z);
  119. Point3F point4(a.x, a.y, b.z);
  120. Point3F point5(a.x, b.y, b.z);
  121. Point3F point6(b.x, b.y, b.z);
  122. Point3F point7(b.x, a.y, b.z);
  123. // Draw one plane
  124. drawLine(point0, point1, color);
  125. drawLine(point1, point2, color);
  126. drawLine(point2, point3, color);
  127. drawLine(point3, point0, color);
  128. // Draw the other plane
  129. drawLine(point4, point5, color);
  130. drawLine(point5, point6, color);
  131. drawLine(point6, point7, color);
  132. drawLine(point7, point4, color);
  133. // Draw the connecting corners
  134. drawLine(point0, point4, color);
  135. drawLine(point1, point5, color);
  136. drawLine(point2, point6, color);
  137. drawLine(point3, point7, color);
  138. }
  139. void DebugDrawer::drawTransformedBoxOutline(const Point3F &a, const Point3F &b, const ColorF &color, const MatrixF& transform)
  140. {
  141. Point3F point0(a.x, a.y, a.z);
  142. Point3F point1(a.x, b.y, a.z);
  143. Point3F point2(b.x, b.y, a.z);
  144. Point3F point3(b.x, a.y, a.z);
  145. Point3F point4(a.x, a.y, b.z);
  146. Point3F point5(a.x, b.y, b.z);
  147. Point3F point6(b.x, b.y, b.z);
  148. Point3F point7(b.x, a.y, b.z);
  149. transform.mulP(point0);
  150. transform.mulP(point1);
  151. transform.mulP(point2);
  152. transform.mulP(point3);
  153. transform.mulP(point4);
  154. transform.mulP(point5);
  155. transform.mulP(point6);
  156. transform.mulP(point7);
  157. // Draw one plane
  158. drawLine(point0, point1, color);
  159. drawLine(point1, point2, color);
  160. drawLine(point2, point3, color);
  161. drawLine(point3, point0, color);
  162. // Draw the other plane
  163. drawLine(point4, point5, color);
  164. drawLine(point5, point6, color);
  165. drawLine(point6, point7, color);
  166. drawLine(point7, point4, color);
  167. // Draw the connecting corners
  168. drawLine(point0, point4, color);
  169. drawLine(point1, point5, color);
  170. drawLine(point2, point6, color);
  171. drawLine(point3, point7, color);
  172. }
  173. void DebugDrawer::render()
  174. {
  175. #ifdef ENABLE_DEBUGDRAW
  176. if(!isDrawing)
  177. return;
  178. GFXDEBUGEVENT_SCOPE( DebugDrawer, ColorI::GREEN );
  179. if (!mRenderZOnSB)
  180. {
  181. setupStateBlocks();
  182. String fontCacheDir = Con::getVariable("$GUI::fontCacheDirectory");
  183. mFont = GFont::create("Arial", 12, fontCacheDir);
  184. }
  185. SimTime curTime = Sim::getCurrentTime();
  186. for(DebugPrim **walk = &mHead; *walk; )
  187. {
  188. GFX->setupGenericShaders();
  189. DebugPrim *p = *walk;
  190. // Set up the state block...
  191. GFXStateBlockRef currSB;
  192. if(p->type==DebugPrim::Capsule){
  193. currSB = mRenderAlpha;
  194. }else if(p->useZ){
  195. currSB = mRenderZOnSB;
  196. }else{
  197. currSB = mRenderZOffSB;
  198. }
  199. GFX->setStateBlock( currSB );
  200. Point3F d;
  201. switch(p->type)
  202. {
  203. case DebugPrim::Tri:
  204. PrimBuild::begin( GFXLineStrip, 4);
  205. PrimBuild::color(p->color);
  206. PrimBuild::vertex3fv(p->a);
  207. PrimBuild::vertex3fv(p->b);
  208. PrimBuild::vertex3fv(p->c);
  209. PrimBuild::vertex3fv(p->a);
  210. PrimBuild::end();
  211. break;
  212. case DebugPrim::DirectionLine:
  213. {
  214. const static F32 ARROW_LENGTH = 0.2f, ARROW_RADIUS = 0.035f, CYLINDER_RADIUS = 0.008f;
  215. Point3F &start = p->a, &end = p->b;
  216. Point3F direction = end - start;
  217. F32 length = direction.len();
  218. if( length>ARROW_LENGTH ){
  219. //cylinder with arrow on end
  220. direction *= (1.0f/length);
  221. Point3F baseArrow = end - (direction*ARROW_LENGTH);
  222. GFX->getDrawUtil()->drawCone(currSB->getDesc(), baseArrow, end, ARROW_RADIUS, p->color);
  223. GFX->getDrawUtil()->drawCylinder(currSB->getDesc(), start, baseArrow, CYLINDER_RADIUS, p->color);
  224. }else if( length>0 ){
  225. //short, so just draw arrow
  226. GFX->getDrawUtil()->drawCone(currSB->getDesc(), start, end, ARROW_RADIUS, p->color);
  227. }
  228. }
  229. break;
  230. case DebugPrim::Capsule:
  231. GFX->getDrawUtil()->drawCapsule(currSB->getDesc(), p->a, p->b.x, p->b.y, p->color);
  232. break;
  233. case DebugPrim::OutlinedText:
  234. {
  235. GFXTransformSaver saver;
  236. Point3F result;
  237. if (MathUtils::mProjectWorldToScreen(p->a, &result, GFX->getViewport(), GFX->getWorldMatrix(), GFX->getProjectionMatrix()))
  238. {
  239. GFX->setClipRect(GFX->getViewport());
  240. Point2I where = Point2I(result.x, result.y);
  241. GFX->getDrawUtil()->setBitmapModulation(p->color2);
  242. GFX->getDrawUtil()->drawText(mFont, Point2I(where.x-1, where.y), p->mText);
  243. GFX->getDrawUtil()->drawText(mFont, Point2I(where.x+1, where.y), p->mText);
  244. GFX->getDrawUtil()->drawText(mFont, Point2I(where.x, where.y-1), p->mText);
  245. GFX->getDrawUtil()->drawText(mFont, Point2I(where.x, where.y+1), p->mText);
  246. GFX->getDrawUtil()->setBitmapModulation(p->color);
  247. GFX->getDrawUtil()->drawText(mFont, where, p->mText);
  248. }
  249. }
  250. break;
  251. case DebugPrim::Box:
  252. d = p->a - p->b;
  253. GFX->getDrawUtil()->drawCube(currSB->getDesc(), d * 0.5, (p->a + p->b) * 0.5, p->color);
  254. break;
  255. case DebugPrim::Line:
  256. PrimBuild::begin( GFXLineStrip, 2);
  257. PrimBuild::color(p->color);
  258. PrimBuild::vertex3fv(p->a);
  259. PrimBuild::vertex3fv(p->b);
  260. PrimBuild::end();
  261. break;
  262. case DebugPrim::Text:
  263. {
  264. GFXTransformSaver saver;
  265. Point3F result;
  266. if (MathUtils::mProjectWorldToScreen(p->a, &result, GFX->getViewport(), GFX->getWorldMatrix(), GFX->getProjectionMatrix()))
  267. {
  268. GFX->setClipRect(GFX->getViewport());
  269. GFX->getDrawUtil()->setBitmapModulation(p->color);
  270. GFX->getDrawUtil()->drawText(mFont, Point2I(result.x, result.y), p->mText);
  271. }
  272. }
  273. break;
  274. }
  275. // Ok, we've got data, now freeze here if needed.
  276. if (shouldToggleFreeze)
  277. {
  278. isFrozen = !isFrozen;
  279. shouldToggleFreeze = false;
  280. }
  281. if(p->dieTime <= curTime && !isFrozen && p->dieTime != U32_MAX)
  282. {
  283. *walk = p->next;
  284. mPrimChunker.free(p);
  285. }
  286. else
  287. walk = &((*walk)->next);
  288. }
  289. #endif
  290. }
  291. void DebugDrawer::drawBox(const Point3F &a, const Point3F &b, const ColorF &color)
  292. {
  293. if(isFrozen || !isDrawing)
  294. return;
  295. DebugPrim *n = mPrimChunker.alloc();
  296. n->useZ = true;
  297. n->dieTime = 0;
  298. n->a = a;
  299. n->b = b;
  300. n->color = color;
  301. n->type = DebugPrim::Box;
  302. n->next = mHead;
  303. mHead = n;
  304. }
  305. void DebugDrawer::drawLine(const Point3F &a, const Point3F &b, const ColorF &color)
  306. {
  307. if(isFrozen || !isDrawing)
  308. return;
  309. DebugPrim *n = mPrimChunker.alloc();
  310. n->useZ = true;
  311. n->dieTime = 0;
  312. n->a = a;
  313. n->b = b;
  314. n->color = color;
  315. n->type = DebugPrim::Line;
  316. n->next = mHead;
  317. mHead = n;
  318. }
  319. void DebugDrawer::drawCapsule(const Point3F &a, const F32 &radius, const F32 &height, const ColorF &color)
  320. {
  321. if(isFrozen || !isDrawing)
  322. return;
  323. DebugPrim *n = mPrimChunker.alloc();
  324. n->useZ = true;
  325. n->dieTime = 0;
  326. n->a = a;
  327. n->b.x = radius;
  328. n->b.y = height;
  329. n->color = color;
  330. n->type = DebugPrim::Capsule;
  331. n->next = mHead;
  332. mHead = n;
  333. }
  334. void DebugDrawer::drawDirectionLine(const Point3F &a, const Point3F &b, const ColorF &color)
  335. {
  336. if(isFrozen || !isDrawing)
  337. return;
  338. DebugPrim *n = mPrimChunker.alloc();
  339. n->useZ = true;
  340. n->dieTime = 0;
  341. n->a = a;
  342. n->b = b;
  343. n->color = color;
  344. n->type = DebugPrim::DirectionLine;
  345. n->next = mHead;
  346. mHead = n;
  347. }
  348. void DebugDrawer::drawOutlinedText(const Point3F& pos, const String& text, const ColorF &color, const ColorF &colorOutline)
  349. {
  350. if(isFrozen || !isDrawing)
  351. return;
  352. DebugPrim *n = mPrimChunker.alloc();
  353. n->useZ = false;
  354. n->dieTime = 0;
  355. n->a = pos;
  356. n->color = color;
  357. n->color2 = colorOutline;
  358. dStrncpy(n->mText, text.c_str(), 256);
  359. n->type = DebugPrim::OutlinedText;
  360. n->next = mHead;
  361. mHead = n;
  362. }
  363. void DebugDrawer::drawTri(const Point3F &a, const Point3F &b, const Point3F &c, const ColorF &color)
  364. {
  365. if(isFrozen || !isDrawing)
  366. return;
  367. DebugPrim *n = mPrimChunker.alloc();
  368. n->useZ = true;
  369. n->dieTime = 0;
  370. n->a = a;
  371. n->b = b;
  372. n->c = c;
  373. n->color = color;
  374. n->type = DebugPrim::Tri;
  375. n->next = mHead;
  376. mHead = n;
  377. }
  378. void DebugDrawer::drawPolyhedron( const AnyPolyhedron& polyhedron, const ColorF& color )
  379. {
  380. const PolyhedronData::Edge* edges = polyhedron.getEdges();
  381. const Point3F* points = polyhedron.getPoints();
  382. const U32 numEdges = polyhedron.getNumEdges();
  383. for( U32 i = 0; i < numEdges; ++ i )
  384. {
  385. const PolyhedronData::Edge& edge = edges[ i ];
  386. drawLine( points[ edge.vertex[ 0 ] ], points[ edge.vertex[ 1 ] ], color );
  387. }
  388. }
  389. void DebugDrawer::drawPolyhedronDebugInfo( const AnyPolyhedron& polyhedron, const MatrixF& transform, const Point3F& scale )
  390. {
  391. Point3F center = polyhedron.getCenterPoint();
  392. center.convolve( scale );
  393. transform.mulP( center );
  394. // Render plane indices and normals.
  395. const U32 numPlanes = polyhedron.getNumPlanes();
  396. for( U32 i = 0; i < numPlanes; ++ i )
  397. {
  398. const AnyPolyhedron::PlaneType& plane = polyhedron.getPlanes()[ i ];
  399. Point3F planePos = plane.getPosition();
  400. planePos.convolve( scale );
  401. transform.mulP( planePos );
  402. Point3F normal = plane.getNormal();
  403. transform.mulV( normal );
  404. drawText( planePos, String::ToString( i ), ColorI::BLACK );
  405. drawLine( planePos, planePos + normal, ColorI::GREEN );
  406. }
  407. // Render edge indices and direction indicators.
  408. const U32 numEdges = polyhedron.getNumEdges();
  409. for( U32 i = 0; i < numEdges; ++ i )
  410. {
  411. const AnyPolyhedron::EdgeType& edge = polyhedron.getEdges()[ i ];
  412. Point3F v1 = polyhedron.getPoints()[ edge.vertex[ 0 ] ];
  413. Point3F v2 = polyhedron.getPoints()[ edge.vertex[ 1 ] ];
  414. v1.convolve( scale );
  415. v2.convolve( scale );
  416. transform.mulP( v1 );
  417. transform.mulP( v2 );
  418. const Point3F midPoint = v1 + ( v2 - v1 ) / 2.f;
  419. drawText( midPoint, String::ToString( "%i (%i, %i)", i, edge.face[ 0 ], edge.face[ 1 ] ), ColorI::WHITE );
  420. // Push out the midpoint away from the center to place the direction indicator.
  421. Point3F pushDir = midPoint - center;
  422. pushDir.normalize();
  423. const Point3F dirPoint = midPoint + pushDir;
  424. const Point3F lineDir = ( v2 - v1 ) / 2.f;
  425. drawLine( dirPoint, dirPoint + lineDir, ColorI::RED );
  426. }
  427. // Render point indices and coordinates.
  428. const U32 numPoints = polyhedron.getNumPoints();
  429. for( U32 i = 0; i < numPoints; ++ i )
  430. {
  431. Point3F p = polyhedron.getPoints()[ i ];
  432. p.convolve( scale );
  433. transform.mulP( p );
  434. drawText( p, String::ToString( "%i: (%.2f, %.2f, %.2f)", i, p.x, p.y, p.z ), ColorF::WHITE );
  435. }
  436. }
  437. void DebugDrawer::drawText(const Point3F& pos, const String& text, const ColorF &color)
  438. {
  439. if(isFrozen || !isDrawing)
  440. return;
  441. DebugPrim *n = mPrimChunker.alloc();
  442. n->useZ = false;
  443. n->dieTime = 0;
  444. n->a = pos;
  445. n->color = color;
  446. dStrncpy(n->mText, text.c_str(), 256);
  447. n->type = DebugPrim::Text;
  448. n->next = mHead;
  449. mHead = n;
  450. }
  451. void DebugDrawer::setLastTTL(U32 ms)
  452. {
  453. AssertFatal(mHead, "Tried to set last with nothing in the list!");
  454. if (ms != U32_MAX)
  455. mHead->dieTime = Sim::getCurrentTime() + ms;
  456. else
  457. mHead->dieTime = U32_MAX;
  458. }
  459. void DebugDrawer::setLastZTest(bool enabled)
  460. {
  461. AssertFatal(mHead, "Tried to set last with nothing in the list!");
  462. mHead->useZ = enabled;
  463. }
  464. DefineEngineMethod( DebugDrawer, drawLine, void, ( Point3F a, Point3F b, ColorF color ), ( ColorF::WHITE ),
  465. "Draws a line primitive between two 3d points." )
  466. {
  467. object->drawLine( a, b, color );
  468. }
  469. DefineEngineMethod( DebugDrawer, drawBox, void, ( Point3F a, Point3F b, ColorF color ), ( ColorF::WHITE ),
  470. "Draws an axis aligned box primitive within the two 3d points." )
  471. {
  472. object->drawBox( a, b, color );
  473. }
  474. DefineEngineMethod( DebugDrawer, setLastTTL, void, ( U32 ms ),,
  475. "Sets the \"time to live\" (TTL) for the last rendered primitive." )
  476. {
  477. object->setLastTTL( ms );
  478. }
  479. DefineEngineMethod( DebugDrawer, setLastZTest, void, ( bool enabled ),,
  480. "Sets the z buffer reading state for the last rendered primitive." )
  481. {
  482. object->setLastZTest( enabled );
  483. }
  484. DefineEngineMethod( DebugDrawer, toggleFreeze, void, (),,
  485. "Toggles freeze mode which keeps the currently rendered primitives from expiring." )
  486. {
  487. object->toggleFreeze();
  488. }
  489. DefineEngineMethod( DebugDrawer, toggleDrawing, void, (),,
  490. "Toggles the rendering of DebugDrawer primitives." )
  491. {
  492. object->toggleDrawing();
  493. }