debugDraw.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  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. }
  109. void DebugDrawer::render()
  110. {
  111. #ifdef ENABLE_DEBUGDRAW
  112. if(!isDrawing)
  113. return;
  114. GFXDEBUGEVENT_SCOPE( DebugDrawer, ColorI::GREEN );
  115. if (!mRenderZOnSB)
  116. {
  117. setupStateBlocks();
  118. String fontCacheDir = Con::getVariable("$GUI::fontCacheDirectory");
  119. mFont = GFont::create("Arial", 12, fontCacheDir);
  120. }
  121. SimTime curTime = Sim::getCurrentTime();
  122. for(DebugPrim **walk = &mHead; *walk; )
  123. {
  124. GFX->setupGenericShaders();
  125. DebugPrim *p = *walk;
  126. // Set up the state block...
  127. GFXStateBlockRef currSB;
  128. if(p->useZ)
  129. currSB = mRenderZOnSB;
  130. else
  131. currSB = mRenderZOffSB;
  132. GFX->setStateBlock( currSB );
  133. Point3F d;
  134. switch(p->type)
  135. {
  136. case DebugPrim::Tri:
  137. PrimBuild::begin( GFXLineStrip, 4);
  138. PrimBuild::color(p->color);
  139. PrimBuild::vertex3fv(p->a);
  140. PrimBuild::vertex3fv(p->b);
  141. PrimBuild::vertex3fv(p->c);
  142. PrimBuild::vertex3fv(p->a);
  143. PrimBuild::end();
  144. break;
  145. case DebugPrim::Box:
  146. d = p->a - p->b;
  147. GFX->getDrawUtil()->drawCube(currSB->getDesc(), d * 0.5, (p->a + p->b) * 0.5, p->color);
  148. break;
  149. case DebugPrim::Line:
  150. PrimBuild::begin( GFXLineStrip, 2);
  151. PrimBuild::color(p->color);
  152. PrimBuild::vertex3fv(p->a);
  153. PrimBuild::vertex3fv(p->b);
  154. PrimBuild::end();
  155. break;
  156. case DebugPrim::Text:
  157. {
  158. GFXTransformSaver saver;
  159. Point3F result;
  160. if (MathUtils::mProjectWorldToScreen(p->a, &result, GFX->getViewport(), GFX->getWorldMatrix(), GFX->getProjectionMatrix()))
  161. {
  162. GFX->setClipRect(GFX->getViewport());
  163. GFX->getDrawUtil()->setBitmapModulation(p->color);
  164. GFX->getDrawUtil()->drawText(mFont, Point2I(result.x, result.y), p->mText);
  165. }
  166. }
  167. break;
  168. }
  169. // Ok, we've got data, now freeze here if needed.
  170. if (shouldToggleFreeze)
  171. {
  172. isFrozen = !isFrozen;
  173. shouldToggleFreeze = false;
  174. }
  175. if(p->dieTime <= curTime && !isFrozen && p->dieTime != U32_MAX)
  176. {
  177. *walk = p->next;
  178. mPrimChunker.free(p);
  179. }
  180. else
  181. walk = &((*walk)->next);
  182. }
  183. #endif
  184. }
  185. void DebugDrawer::drawBox(const Point3F &a, const Point3F &b, const ColorF &color)
  186. {
  187. if(isFrozen || !isDrawing)
  188. return;
  189. DebugPrim *n = mPrimChunker.alloc();
  190. n->useZ = true;
  191. n->dieTime = 0;
  192. n->a = a;
  193. n->b = b;
  194. n->color = color;
  195. n->type = DebugPrim::Box;
  196. n->next = mHead;
  197. mHead = n;
  198. }
  199. void DebugDrawer::drawLine(const Point3F &a, const Point3F &b, const ColorF &color)
  200. {
  201. if(isFrozen || !isDrawing)
  202. return;
  203. DebugPrim *n = mPrimChunker.alloc();
  204. n->useZ = true;
  205. n->dieTime = 0;
  206. n->a = a;
  207. n->b = b;
  208. n->color = color;
  209. n->type = DebugPrim::Line;
  210. n->next = mHead;
  211. mHead = n;
  212. }
  213. void DebugDrawer::drawTri(const Point3F &a, const Point3F &b, const Point3F &c, const ColorF &color)
  214. {
  215. if(isFrozen || !isDrawing)
  216. return;
  217. DebugPrim *n = mPrimChunker.alloc();
  218. n->useZ = true;
  219. n->dieTime = 0;
  220. n->a = a;
  221. n->b = b;
  222. n->c = c;
  223. n->color = color;
  224. n->type = DebugPrim::Tri;
  225. n->next = mHead;
  226. mHead = n;
  227. }
  228. void DebugDrawer::drawPolyhedron( const AnyPolyhedron& polyhedron, const ColorF& color )
  229. {
  230. const PolyhedronData::Edge* edges = polyhedron.getEdges();
  231. const Point3F* points = polyhedron.getPoints();
  232. const U32 numEdges = polyhedron.getNumEdges();
  233. for( U32 i = 0; i < numEdges; ++ i )
  234. {
  235. const PolyhedronData::Edge& edge = edges[ i ];
  236. drawLine( points[ edge.vertex[ 0 ] ], points[ edge.vertex[ 1 ] ], color );
  237. }
  238. }
  239. void DebugDrawer::drawPolyhedronDebugInfo( const AnyPolyhedron& polyhedron, const MatrixF& transform, const Point3F& scale )
  240. {
  241. Point3F center = polyhedron.getCenterPoint();
  242. center.convolve( scale );
  243. transform.mulP( center );
  244. // Render plane indices and normals.
  245. const U32 numPlanes = polyhedron.getNumPlanes();
  246. for( U32 i = 0; i < numPlanes; ++ i )
  247. {
  248. const AnyPolyhedron::PlaneType& plane = polyhedron.getPlanes()[ i ];
  249. Point3F planePos = plane.getPosition();
  250. planePos.convolve( scale );
  251. transform.mulP( planePos );
  252. Point3F normal = plane.getNormal();
  253. transform.mulV( normal );
  254. drawText( planePos, String::ToString( i ), ColorI::BLACK );
  255. drawLine( planePos, planePos + normal, ColorI::GREEN );
  256. }
  257. // Render edge indices and direction indicators.
  258. const U32 numEdges = polyhedron.getNumEdges();
  259. for( U32 i = 0; i < numEdges; ++ i )
  260. {
  261. const AnyPolyhedron::EdgeType& edge = polyhedron.getEdges()[ i ];
  262. Point3F v1 = polyhedron.getPoints()[ edge.vertex[ 0 ] ];
  263. Point3F v2 = polyhedron.getPoints()[ edge.vertex[ 1 ] ];
  264. v1.convolve( scale );
  265. v2.convolve( scale );
  266. transform.mulP( v1 );
  267. transform.mulP( v2 );
  268. const Point3F midPoint = v1 + ( v2 - v1 ) / 2.f;
  269. drawText( midPoint, String::ToString( "%i (%i, %i)", i, edge.face[ 0 ], edge.face[ 1 ] ), ColorI::WHITE );
  270. // Push out the midpoint away from the center to place the direction indicator.
  271. Point3F pushDir = midPoint - center;
  272. pushDir.normalize();
  273. const Point3F dirPoint = midPoint + pushDir;
  274. const Point3F lineDir = ( v2 - v1 ) / 2.f;
  275. drawLine( dirPoint, dirPoint + lineDir, ColorI::RED );
  276. }
  277. // Render point indices and coordinates.
  278. const U32 numPoints = polyhedron.getNumPoints();
  279. for( U32 i = 0; i < numPoints; ++ i )
  280. {
  281. Point3F p = polyhedron.getPoints()[ i ];
  282. p.convolve( scale );
  283. transform.mulP( p );
  284. drawText( p, String::ToString( "%i: (%.2f, %.2f, %.2f)", i, p.x, p.y, p.z ), ColorF::WHITE );
  285. }
  286. }
  287. void DebugDrawer::drawText(const Point3F& pos, const String& text, const ColorF &color)
  288. {
  289. if(isFrozen || !isDrawing)
  290. return;
  291. DebugPrim *n = mPrimChunker.alloc();
  292. n->useZ = false;
  293. n->dieTime = 0;
  294. n->a = pos;
  295. n->color = color;
  296. dStrncpy(n->mText, text.c_str(), 256);
  297. n->type = DebugPrim::Text;
  298. n->next = mHead;
  299. mHead = n;
  300. }
  301. void DebugDrawer::setLastTTL(U32 ms)
  302. {
  303. AssertFatal(mHead, "Tried to set last with nothing in the list!");
  304. if (ms != U32_MAX)
  305. mHead->dieTime = Sim::getCurrentTime() + ms;
  306. else
  307. mHead->dieTime = U32_MAX;
  308. }
  309. void DebugDrawer::setLastZTest(bool enabled)
  310. {
  311. AssertFatal(mHead, "Tried to set last with nothing in the list!");
  312. mHead->useZ = enabled;
  313. }
  314. DefineEngineMethod( DebugDrawer, drawLine, void, ( Point3F a, Point3F b, ColorF color ), ( ColorF::WHITE ),
  315. "Draws a line primitive between two 3d points." )
  316. {
  317. object->drawLine( a, b, color );
  318. }
  319. DefineEngineMethod( DebugDrawer, drawBox, void, ( Point3F a, Point3F b, ColorF color ), ( ColorF::WHITE ),
  320. "Draws an axis aligned box primitive within the two 3d points." )
  321. {
  322. object->drawBox( a, b, color );
  323. }
  324. DefineEngineMethod( DebugDrawer, setLastTTL, void, ( U32 ms ),,
  325. "Sets the \"time to live\" (TTL) for the last rendered primitive." )
  326. {
  327. object->setLastTTL( ms );
  328. }
  329. DefineEngineMethod( DebugDrawer, setLastZTest, void, ( bool enabled ),,
  330. "Sets the z buffer reading state for the last rendered primitive." )
  331. {
  332. object->setLastZTest( enabled );
  333. }
  334. DefineEngineMethod( DebugDrawer, toggleFreeze, void, (),,
  335. "Toggles freeze mode which keeps the currently rendered primitives from expiring." )
  336. {
  337. object->toggleFreeze();
  338. }
  339. DefineEngineMethod( DebugDrawer, toggleDrawing, void, (),,
  340. "Toggles the rendering of DebugDrawer primitives." )
  341. {
  342. object->toggleDrawing();
  343. }