debugDraw.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538
  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::render()
  114. {
  115. #ifdef ENABLE_DEBUGDRAW
  116. if(!isDrawing)
  117. return;
  118. GFXDEBUGEVENT_SCOPE( DebugDrawer, ColorI::GREEN );
  119. if (!mRenderZOnSB)
  120. {
  121. setupStateBlocks();
  122. String fontCacheDir = Con::getVariable("$GUI::fontCacheDirectory");
  123. mFont = GFont::create("Arial", 12, fontCacheDir);
  124. }
  125. SimTime curTime = Sim::getCurrentTime();
  126. for(DebugPrim **walk = &mHead; *walk; )
  127. {
  128. GFX->setupGenericShaders();
  129. DebugPrim *p = *walk;
  130. // Set up the state block...
  131. GFXStateBlockRef currSB;
  132. if(p->type==DebugPrim::Capsule){
  133. currSB = mRenderAlpha;
  134. }else if(p->useZ){
  135. currSB = mRenderZOnSB;
  136. }else{
  137. currSB = mRenderZOffSB;
  138. }
  139. GFX->setStateBlock( currSB );
  140. Point3F d;
  141. switch(p->type)
  142. {
  143. case DebugPrim::Tri:
  144. PrimBuild::begin( GFXLineStrip, 4);
  145. PrimBuild::color(p->color);
  146. PrimBuild::vertex3fv(p->a);
  147. PrimBuild::vertex3fv(p->b);
  148. PrimBuild::vertex3fv(p->c);
  149. PrimBuild::vertex3fv(p->a);
  150. PrimBuild::end();
  151. break;
  152. case DebugPrim::DirectionLine:
  153. {
  154. const static F32 ARROW_LENGTH = 0.2f, ARROW_RADIUS = 0.035f, CYLINDER_RADIUS = 0.008f;
  155. Point3F &start = p->a, &end = p->b;
  156. Point3F direction = end - start;
  157. F32 length = direction.len();
  158. if( length>ARROW_LENGTH ){
  159. //cylinder with arrow on end
  160. direction *= (1.0f/length);
  161. Point3F baseArrow = end - (direction*ARROW_LENGTH);
  162. GFX->getDrawUtil()->drawCone(currSB->getDesc(), baseArrow, end, ARROW_RADIUS, p->color);
  163. GFX->getDrawUtil()->drawCylinder(currSB->getDesc(), start, baseArrow, CYLINDER_RADIUS, p->color);
  164. }else if( length>0 ){
  165. //short, so just draw arrow
  166. GFX->getDrawUtil()->drawCone(currSB->getDesc(), start, end, ARROW_RADIUS, p->color);
  167. }
  168. }
  169. break;
  170. case DebugPrim::Capsule:
  171. GFX->getDrawUtil()->drawCapsule(currSB->getDesc(), p->a, p->b.x, p->b.y, p->color);
  172. break;
  173. case DebugPrim::OutlinedText:
  174. {
  175. GFXTransformSaver saver;
  176. Point3F result;
  177. if (MathUtils::mProjectWorldToScreen(p->a, &result, GFX->getViewport(), GFX->getWorldMatrix(), GFX->getProjectionMatrix()))
  178. {
  179. GFX->setClipRect(GFX->getViewport());
  180. Point2I where = Point2I(result.x, result.y);
  181. GFX->getDrawUtil()->setBitmapModulation(p->color2);
  182. GFX->getDrawUtil()->drawText(mFont, Point2I(where.x-1, where.y), p->mText);
  183. GFX->getDrawUtil()->drawText(mFont, Point2I(where.x+1, where.y), p->mText);
  184. GFX->getDrawUtil()->drawText(mFont, Point2I(where.x, where.y-1), p->mText);
  185. GFX->getDrawUtil()->drawText(mFont, Point2I(where.x, where.y+1), p->mText);
  186. GFX->getDrawUtil()->setBitmapModulation(p->color);
  187. GFX->getDrawUtil()->drawText(mFont, where, p->mText);
  188. }
  189. }
  190. break;
  191. case DebugPrim::Box:
  192. d = p->a - p->b;
  193. GFX->getDrawUtil()->drawCube(currSB->getDesc(), d * 0.5, (p->a + p->b) * 0.5, p->color);
  194. break;
  195. case DebugPrim::Line:
  196. PrimBuild::begin( GFXLineStrip, 2);
  197. PrimBuild::color(p->color);
  198. PrimBuild::vertex3fv(p->a);
  199. PrimBuild::vertex3fv(p->b);
  200. PrimBuild::end();
  201. break;
  202. case DebugPrim::Text:
  203. {
  204. GFXTransformSaver saver;
  205. Point3F result;
  206. if (MathUtils::mProjectWorldToScreen(p->a, &result, GFX->getViewport(), GFX->getWorldMatrix(), GFX->getProjectionMatrix()))
  207. {
  208. GFX->setClipRect(GFX->getViewport());
  209. GFX->getDrawUtil()->setBitmapModulation(p->color);
  210. GFX->getDrawUtil()->drawText(mFont, Point2I(result.x, result.y), p->mText);
  211. }
  212. }
  213. break;
  214. }
  215. // Ok, we've got data, now freeze here if needed.
  216. if (shouldToggleFreeze)
  217. {
  218. isFrozen = !isFrozen;
  219. shouldToggleFreeze = false;
  220. }
  221. if(p->dieTime <= curTime && !isFrozen && p->dieTime != U32_MAX)
  222. {
  223. *walk = p->next;
  224. mPrimChunker.free(p);
  225. }
  226. else
  227. walk = &((*walk)->next);
  228. }
  229. #endif
  230. }
  231. void DebugDrawer::drawBox(const Point3F &a, const Point3F &b, const ColorF &color)
  232. {
  233. if(isFrozen || !isDrawing)
  234. return;
  235. DebugPrim *n = mPrimChunker.alloc();
  236. n->useZ = true;
  237. n->dieTime = 0;
  238. n->a = a;
  239. n->b = b;
  240. n->color = color;
  241. n->type = DebugPrim::Box;
  242. n->next = mHead;
  243. mHead = n;
  244. }
  245. void DebugDrawer::drawLine(const Point3F &a, const Point3F &b, const ColorF &color)
  246. {
  247. if(isFrozen || !isDrawing)
  248. return;
  249. DebugPrim *n = mPrimChunker.alloc();
  250. n->useZ = true;
  251. n->dieTime = 0;
  252. n->a = a;
  253. n->b = b;
  254. n->color = color;
  255. n->type = DebugPrim::Line;
  256. n->next = mHead;
  257. mHead = n;
  258. }
  259. void DebugDrawer::drawCapsule(const Point3F &a, const F32 &radius, const F32 &height, const ColorF &color)
  260. {
  261. if(isFrozen || !isDrawing)
  262. return;
  263. DebugPrim *n = mPrimChunker.alloc();
  264. n->useZ = true;
  265. n->dieTime = 0;
  266. n->a = a;
  267. n->b.x = radius;
  268. n->b.y = height;
  269. n->color = color;
  270. n->type = DebugPrim::Capsule;
  271. n->next = mHead;
  272. mHead = n;
  273. }
  274. void DebugDrawer::drawDirectionLine(const Point3F &a, const Point3F &b, const ColorF &color)
  275. {
  276. if(isFrozen || !isDrawing)
  277. return;
  278. DebugPrim *n = mPrimChunker.alloc();
  279. n->useZ = true;
  280. n->dieTime = 0;
  281. n->a = a;
  282. n->b = b;
  283. n->color = color;
  284. n->type = DebugPrim::DirectionLine;
  285. n->next = mHead;
  286. mHead = n;
  287. }
  288. void DebugDrawer::drawOutlinedText(const Point3F& pos, const String& text, const ColorF &color, const ColorF &colorOutline)
  289. {
  290. if(isFrozen || !isDrawing)
  291. return;
  292. DebugPrim *n = mPrimChunker.alloc();
  293. n->useZ = false;
  294. n->dieTime = 0;
  295. n->a = pos;
  296. n->color = color;
  297. n->color2 = colorOutline;
  298. dStrncpy(n->mText, text.c_str(), 256);
  299. n->type = DebugPrim::OutlinedText;
  300. n->next = mHead;
  301. mHead = n;
  302. }
  303. void DebugDrawer::drawTri(const Point3F &a, const Point3F &b, const Point3F &c, const ColorF &color)
  304. {
  305. if(isFrozen || !isDrawing)
  306. return;
  307. DebugPrim *n = mPrimChunker.alloc();
  308. n->useZ = true;
  309. n->dieTime = 0;
  310. n->a = a;
  311. n->b = b;
  312. n->c = c;
  313. n->color = color;
  314. n->type = DebugPrim::Tri;
  315. n->next = mHead;
  316. mHead = n;
  317. }
  318. void DebugDrawer::drawPolyhedron( const AnyPolyhedron& polyhedron, const ColorF& color )
  319. {
  320. const PolyhedronData::Edge* edges = polyhedron.getEdges();
  321. const Point3F* points = polyhedron.getPoints();
  322. const U32 numEdges = polyhedron.getNumEdges();
  323. for( U32 i = 0; i < numEdges; ++ i )
  324. {
  325. const PolyhedronData::Edge& edge = edges[ i ];
  326. drawLine( points[ edge.vertex[ 0 ] ], points[ edge.vertex[ 1 ] ], color );
  327. }
  328. }
  329. void DebugDrawer::drawPolyhedronDebugInfo( const AnyPolyhedron& polyhedron, const MatrixF& transform, const Point3F& scale )
  330. {
  331. Point3F center = polyhedron.getCenterPoint();
  332. center.convolve( scale );
  333. transform.mulP( center );
  334. // Render plane indices and normals.
  335. const U32 numPlanes = polyhedron.getNumPlanes();
  336. for( U32 i = 0; i < numPlanes; ++ i )
  337. {
  338. const AnyPolyhedron::PlaneType& plane = polyhedron.getPlanes()[ i ];
  339. Point3F planePos = plane.getPosition();
  340. planePos.convolve( scale );
  341. transform.mulP( planePos );
  342. Point3F normal = plane.getNormal();
  343. transform.mulV( normal );
  344. drawText( planePos, String::ToString( i ), ColorI::BLACK );
  345. drawLine( planePos, planePos + normal, ColorI::GREEN );
  346. }
  347. // Render edge indices and direction indicators.
  348. const U32 numEdges = polyhedron.getNumEdges();
  349. for( U32 i = 0; i < numEdges; ++ i )
  350. {
  351. const AnyPolyhedron::EdgeType& edge = polyhedron.getEdges()[ i ];
  352. Point3F v1 = polyhedron.getPoints()[ edge.vertex[ 0 ] ];
  353. Point3F v2 = polyhedron.getPoints()[ edge.vertex[ 1 ] ];
  354. v1.convolve( scale );
  355. v2.convolve( scale );
  356. transform.mulP( v1 );
  357. transform.mulP( v2 );
  358. const Point3F midPoint = v1 + ( v2 - v1 ) / 2.f;
  359. drawText( midPoint, String::ToString( "%i (%i, %i)", i, edge.face[ 0 ], edge.face[ 1 ] ), ColorI::WHITE );
  360. // Push out the midpoint away from the center to place the direction indicator.
  361. Point3F pushDir = midPoint - center;
  362. pushDir.normalize();
  363. const Point3F dirPoint = midPoint + pushDir;
  364. const Point3F lineDir = ( v2 - v1 ) / 2.f;
  365. drawLine( dirPoint, dirPoint + lineDir, ColorI::RED );
  366. }
  367. // Render point indices and coordinates.
  368. const U32 numPoints = polyhedron.getNumPoints();
  369. for( U32 i = 0; i < numPoints; ++ i )
  370. {
  371. Point3F p = polyhedron.getPoints()[ i ];
  372. p.convolve( scale );
  373. transform.mulP( p );
  374. drawText( p, String::ToString( "%i: (%.2f, %.2f, %.2f)", i, p.x, p.y, p.z ), ColorF::WHITE );
  375. }
  376. }
  377. void DebugDrawer::drawText(const Point3F& pos, const String& text, const ColorF &color)
  378. {
  379. if(isFrozen || !isDrawing)
  380. return;
  381. DebugPrim *n = mPrimChunker.alloc();
  382. n->useZ = false;
  383. n->dieTime = 0;
  384. n->a = pos;
  385. n->color = color;
  386. dStrncpy(n->mText, text.c_str(), 256);
  387. n->type = DebugPrim::Text;
  388. n->next = mHead;
  389. mHead = n;
  390. }
  391. void DebugDrawer::setLastTTL(U32 ms)
  392. {
  393. AssertFatal(mHead, "Tried to set last with nothing in the list!");
  394. if (ms != U32_MAX)
  395. mHead->dieTime = Sim::getCurrentTime() + ms;
  396. else
  397. mHead->dieTime = U32_MAX;
  398. }
  399. void DebugDrawer::setLastZTest(bool enabled)
  400. {
  401. AssertFatal(mHead, "Tried to set last with nothing in the list!");
  402. mHead->useZ = enabled;
  403. }
  404. DefineEngineMethod( DebugDrawer, drawLine, void, ( Point3F a, Point3F b, ColorF color ), ( ColorF::WHITE ),
  405. "Draws a line primitive between two 3d points." )
  406. {
  407. object->drawLine( a, b, color );
  408. }
  409. DefineEngineMethod( DebugDrawer, drawBox, void, ( Point3F a, Point3F b, ColorF color ), ( ColorF::WHITE ),
  410. "Draws an axis aligned box primitive within the two 3d points." )
  411. {
  412. object->drawBox( a, b, color );
  413. }
  414. DefineEngineMethod( DebugDrawer, setLastTTL, void, ( U32 ms ),,
  415. "Sets the \"time to live\" (TTL) for the last rendered primitive." )
  416. {
  417. object->setLastTTL( ms );
  418. }
  419. DefineEngineMethod( DebugDrawer, setLastZTest, void, ( bool enabled ),,
  420. "Sets the z buffer reading state for the last rendered primitive." )
  421. {
  422. object->setLastZTest( enabled );
  423. }
  424. DefineEngineMethod( DebugDrawer, toggleFreeze, void, (),,
  425. "Toggles freeze mode which keeps the currently rendered primitives from expiring." )
  426. {
  427. object->toggleFreeze();
  428. }
  429. DefineEngineMethod( DebugDrawer, toggleDrawing, void, (),,
  430. "Toggles the rendering of DebugDrawer primitives." )
  431. {
  432. object->toggleDrawing();
  433. }