debugDraw.h 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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. #ifndef _DEBUGDRAW_H_
  23. #define _DEBUGDRAW_H_
  24. #ifndef _SIMOBJECT_H_
  25. #include "console/simObject.h"
  26. #endif
  27. #ifndef _GFXDEVICE_H_
  28. #include "gfx/gfxDevice.h"
  29. #endif
  30. #ifndef _PRIMBUILDER_H_
  31. #include "gfx/primBuilder.h"
  32. #endif
  33. #ifndef _GFONT_H_
  34. #include "gfx/gFont.h"
  35. #endif
  36. #ifndef _DATACHUNKER_H_
  37. #include "core/dataChunker.h"
  38. #endif
  39. #ifndef _MPOLYHEDRON_H_
  40. #include "math/mPolyhedron.h"
  41. #endif
  42. class GFont;
  43. // We enable the debug drawer for non-shipping
  44. // builds.... you better be using shipping builds
  45. // for your final release.
  46. #ifndef TORQUE_SHIPPING
  47. #define ENABLE_DEBUGDRAW
  48. #endif
  49. /// Debug output class.
  50. ///
  51. /// This class provides you with a flexible means of drawing debug output. It is
  52. /// often useful when debugging collision code or complex 3d algorithms to have
  53. /// them draw debug information, like culling hulls or bounding volumes, normals,
  54. /// simple lines, and so forth. In TGE1.2, which was based directly on a simple
  55. /// OpenGL rendering layer, it was a simple matter to do debug rendering directly
  56. /// inline.
  57. ///
  58. /// Unfortunately, this doesn't hold true with more complex rendering scenarios,
  59. /// where render modes and targets may be in abritrary states. In addition, it is
  60. /// often useful to be able to freeze frame debug information for closer inspection.
  61. ///
  62. /// Therefore, Torque provides a global DebugDrawer instance, called gDebugDraw, which
  63. /// you can use to draw debug information. It exposes a number of methods for drawing
  64. /// a variety of debug primitives, including lines, triangles and boxes.
  65. /// Internally, DebugDrawer maintains a list of active debug primitives, and draws the
  66. /// contents of the list after each frame is done rendering. This way, you can be
  67. /// assured that your debug rendering won't interfere with TSE's various effect
  68. /// rendering passes or render-to-target calls.
  69. ///
  70. /// The DebugDrawer can also be used for more interesting uses, like freezing its
  71. /// primitive list so you can look at a situation more closely, or dumping the
  72. /// primitive list to disk for closer analysis.
  73. ///
  74. /// DebugDrawer is accessible by script under the name DebugDrawer, and by C++ under
  75. /// the symbol gDebugDraw. There are a variety of methods available for drawing
  76. /// different sorts of output; see the class reference for more information.
  77. ///
  78. /// DebugDrawer works solely in worldspace. Primitives are rendered with cull mode of
  79. /// none.
  80. ///
  81. class DebugDrawer : public SimObject
  82. {
  83. public:
  84. DECLARE_CONOBJECT(DebugDrawer);
  85. DebugDrawer();
  86. ~DebugDrawer();
  87. static DebugDrawer* get();
  88. /// Called at engine init to set up the global debug draw object.
  89. static void init();
  90. /// Called globally to render debug draw state. Also does state updates.
  91. void render(bool clear=true);
  92. bool willDraw() { return isDrawing && mHead; }
  93. void toggleFreeze() { shouldToggleFreeze = true; };
  94. void toggleDrawing()
  95. {
  96. #ifdef ENABLE_DEBUGDRAW
  97. isDrawing = !isDrawing;
  98. #endif
  99. };
  100. /// @name ddrawmeth Debug Draw Methods
  101. ///
  102. /// @{
  103. void drawBoxOutline(const Point3F &a, const Point3F &b, const ColorF &color = ColorF(1.0f, 1.0f, 1.0f));
  104. void drawTransformedBoxOutline(const Point3F &a, const Point3F &b, const ColorF &color, const MatrixF& transform);
  105. void drawBox(const Point3F &a, const Point3F &b, const ColorF &color = ColorF(1.0f,1.0f,1.0f));
  106. void drawLine(const Point3F &a, const Point3F &b, const ColorF &color = ColorF(1.0f,1.0f,1.0f));
  107. void drawTri(const Point3F &a, const Point3F &b, const Point3F &c, const ColorF &color = ColorF(1.0f,1.0f,1.0f));
  108. void drawText(const Point3F& pos, const String& text, const ColorF &color = ColorF(1.0f,1.0f,1.0f));
  109. void drawCapsule(const Point3F &a, const F32 &radius, const F32 &height, const ColorF &color = ColorF(1.0f, 1.0f, 1.0f));
  110. void drawDirectionLine(const Point3F &a, const Point3F &b, const ColorF &color = ColorF(1.0f, 1.0f, 1.0f));
  111. void drawOutlinedText(const Point3F& pos, const String& text, const ColorF &color = ColorF(1.0f, 1.0f, 1.0f), const ColorF &colorOutline = ColorF(0.0f, 0.0f, 0.0f));
  112. /// Render a wireframe view of the given polyhedron.
  113. void drawPolyhedron( const AnyPolyhedron& polyhedron, const ColorF& color = ColorF( 1.f, 1.f, 1.f ) );
  114. /// Render the plane indices, edge indices, edge direction indicators, and point coordinates
  115. /// of the given polyhedron for debugging.
  116. ///
  117. /// Green lines are plane normals. Red lines point from edge midpoints along the edge direction (i.e. to the
  118. /// second vertex). This shows if the orientation is correct to yield CW ordering for face[0]. Indices and
  119. /// coordinates of vertices are shown in white. Plane indices are rendered in black. Edge indices and their
  120. /// plane indices are rendered in white.
  121. void drawPolyhedronDebugInfo( const AnyPolyhedron& polyhedron, const MatrixF& transform, const Point3F& scale );
  122. /// Set the TTL for the last item we entered...
  123. ///
  124. /// Primitives default to lasting one frame (ie, ttl=0)
  125. enum : U32
  126. {
  127. DD_INFINITE = U32_MAX
  128. };
  129. // How long should this primitive be draw for, 0 = one frame, DD_INFINITE = draw forever
  130. void setLastTTL(U32 ms);
  131. /// Disable/enable z testing on the last primitive.
  132. ///
  133. /// Primitives default to z testing on.
  134. void setLastZTest(bool enabled);
  135. /// @}
  136. private:
  137. typedef SimObject Parent;
  138. static DebugDrawer* sgDebugDrawer;
  139. struct DebugPrim
  140. {
  141. /// Color used for this primitive.
  142. ColorF color;
  143. ColorF color2;
  144. /// Points used to store positional data. Exact semantics determined by type.
  145. Point3F a, b, c;
  146. enum {
  147. Tri,
  148. Box,
  149. Line,
  150. Text,
  151. DirectionLine,
  152. OutlinedText,
  153. Capsule,
  154. } type; ///< Type of the primitive. The meanings of a,b,c are determined by this.
  155. SimTime dieTime; ///< Time at which we should remove this from the list.
  156. bool useZ; ///< If true, do z-checks for this primitive.
  157. char mText[256]; // Text to display
  158. DebugPrim *next;
  159. };
  160. FreeListChunker<DebugPrim> mPrimChunker;
  161. DebugPrim *mHead;
  162. bool isFrozen;
  163. bool shouldToggleFreeze;
  164. bool isDrawing;
  165. GFXStateBlockRef mRenderZOffSB;
  166. GFXStateBlockRef mRenderZOnSB;
  167. GFXStateBlockRef mRenderAlpha;
  168. Resource<GFont> mFont;
  169. void setupStateBlocks();
  170. };
  171. #endif // _DEBUGDRAW_H_