debugDraw.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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();
  92. void toggleFreeze() { shouldToggleFreeze = true; };
  93. void toggleDrawing()
  94. {
  95. #ifdef ENABLE_DEBUGDRAW
  96. isDrawing = !isDrawing;
  97. #endif
  98. };
  99. /// @name ddrawmeth Debug Draw Methods
  100. ///
  101. /// @{
  102. void drawBox(const Point3F &a, const Point3F &b, const ColorF &color = ColorF(1.0f,1.0f,1.0f));
  103. void drawLine(const Point3F &a, const Point3F &b, const ColorF &color = ColorF(1.0f,1.0f,1.0f));
  104. void drawTri(const Point3F &a, const Point3F &b, const Point3F &c, const ColorF &color = ColorF(1.0f,1.0f,1.0f));
  105. void drawText(const Point3F& pos, const String& text, const ColorF &color = ColorF(1.0f,1.0f,1.0f));
  106. /// Render a wireframe view of the given polyhedron.
  107. void drawPolyhedron( const AnyPolyhedron& polyhedron, const ColorF& color = ColorF( 1.f, 1.f, 1.f ) );
  108. /// Render the plane indices, edge indices, edge direction indicators, and point coordinates
  109. /// of the given polyhedron for debugging.
  110. ///
  111. /// Green lines are plane normals. Red lines point from edge midpoints along the edge direction (i.e. to the
  112. /// second vertex). This shows if the orientation is correct to yield CW ordering for face[0]. Indices and
  113. /// coordinates of vertices are shown in white. Plane indices are rendered in black. Edge indices and their
  114. /// plane indices are rendered in white.
  115. void drawPolyhedronDebugInfo( const AnyPolyhedron& polyhedron, const MatrixF& transform, const Point3F& scale );
  116. /// Set the TTL for the last item we entered...
  117. ///
  118. /// Primitives default to lasting one frame (ie, ttl=0)
  119. enum {
  120. DD_INFINITE = U32_MAX
  121. };
  122. // How long should this primitive be draw for, 0 = one frame, DD_INFINITE = draw forever
  123. void setLastTTL(U32 ms);
  124. /// Disable/enable z testing on the last primitive.
  125. ///
  126. /// Primitives default to z testing on.
  127. void setLastZTest(bool enabled);
  128. /// @}
  129. private:
  130. typedef SimObject Parent;
  131. static DebugDrawer* sgDebugDrawer;
  132. struct DebugPrim
  133. {
  134. /// Color used for this primitive.
  135. ColorF color;
  136. /// Points used to store positional data. Exact semantics determined by type.
  137. Point3F a, b, c;
  138. enum {
  139. Tri,
  140. Box,
  141. Line,
  142. Text
  143. } type; ///< Type of the primitive. The meanings of a,b,c are determined by this.
  144. SimTime dieTime; ///< Time at which we should remove this from the list.
  145. bool useZ; ///< If true, do z-checks for this primitive.
  146. char mText[256]; // Text to display
  147. DebugPrim *next;
  148. };
  149. FreeListChunker<DebugPrim> mPrimChunker;
  150. DebugPrim *mHead;
  151. bool isFrozen;
  152. bool shouldToggleFreeze;
  153. bool isDrawing;
  154. GFXStateBlockRef mRenderZOffSB;
  155. GFXStateBlockRef mRenderZOnSB;
  156. Resource<GFont> mFont;
  157. void setupStateBlocks();
  158. };
  159. #endif // _DEBUGDRAW_H_