debugDraw.h

Engine/source/gfx/sim/debugDraw.h

More...

Classes:

class

Debug output class.

Public Defines

Detailed Description

Public Defines

ENABLE_DEBUGDRAW() 
  1
  2//-----------------------------------------------------------------------------
  3// Copyright (c) 2012 GarageGames, LLC
  4//
  5// Permission is hereby granted, free of charge, to any person obtaining a copy
  6// of this software and associated documentation files (the "Software"), to
  7// deal in the Software without restriction, including without limitation the
  8// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  9// sell copies of the Software, and to permit persons to whom the Software is
 10// furnished to do so, subject to the following conditions:
 11//
 12// The above copyright notice and this permission notice shall be included in
 13// all copies or substantial portions of the Software.
 14//
 15// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 16// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 17// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 18// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 19// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 20// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
 21// IN THE SOFTWARE.
 22//-----------------------------------------------------------------------------
 23
 24#ifndef _DEBUGDRAW_H_
 25#define _DEBUGDRAW_H_
 26
 27#ifndef _SIMOBJECT_H_
 28#include "console/simObject.h"
 29#endif
 30
 31#ifndef _GFXDEVICE_H_
 32#include "gfx/gfxDevice.h"
 33#endif
 34
 35#ifndef _PRIMBUILDER_H_
 36#include "gfx/primBuilder.h"
 37#endif
 38
 39#ifndef _GFONT_H_
 40#include "gfx/gFont.h"
 41#endif
 42
 43#ifndef _DATACHUNKER_H_
 44#include "core/dataChunker.h"
 45#endif
 46
 47#ifndef _MPOLYHEDRON_H_
 48#include "math/mPolyhedron.h"
 49#endif
 50
 51
 52class GFont;
 53
 54
 55// We enable the debug drawer for non-shipping
 56// builds.... you better be using shipping builds
 57// for your final release.
 58#ifndef TORQUE_SHIPPING
 59#define ENABLE_DEBUGDRAW
 60#endif
 61
 62
 63/// Debug output class.
 64///
 65/// This class provides you with a flexible means of drawing debug output. It is
 66/// often useful when debugging collision code or complex 3d algorithms to have
 67/// them draw debug information, like culling hulls or bounding volumes, normals,
 68/// simple lines, and so forth. In TGE1.2, which was based directly on a simple
 69/// OpenGL rendering layer, it was a simple matter to do debug rendering directly
 70/// inline.
 71///
 72/// Unfortunately, this doesn't hold true with more complex rendering scenarios,
 73/// where render modes and targets may be in abritrary states. In addition, it is
 74/// often useful to be able to freeze frame debug information for closer inspection.
 75///
 76/// Therefore, Torque provides a global DebugDrawer instance, called gDebugDraw, which
 77/// you can use to draw debug information. It exposes a number of methods for drawing
 78/// a variety of debug primitives, including lines, triangles and boxes.
 79/// Internally, DebugDrawer maintains a list of active debug primitives, and draws the
 80/// contents of the list after each frame is done rendering. This way, you can be
 81/// assured that your debug rendering won't interfere with TSE's various effect
 82/// rendering passes or render-to-target calls.
 83///
 84/// The DebugDrawer can also be used for more interesting uses, like freezing its
 85/// primitive list so you can look at a situation more closely, or dumping the
 86/// primitive list to disk for closer analysis.
 87///
 88/// DebugDrawer is accessible by script under the name DebugDrawer, and by C++ under
 89/// the symbol gDebugDraw. There are a variety of methods available for drawing
 90/// different sorts of output; see the class reference for more information.
 91///
 92/// DebugDrawer works solely in worldspace. Primitives are rendered with cull mode of
 93/// none.
 94///
 95class DebugDrawer : public SimObject
 96{
 97   typedef SimObject Parent;
 98public:
 99   DECLARE_CONOBJECT(DebugDrawer);
100
101   DebugDrawer();
102   ~DebugDrawer();
103
104   static DebugDrawer* get();
105   
106   /// Called at engine init to set up the global debug draw object.
107   static void init();
108
109   /// Called globally to render debug draw state. Also does state updates.
110   void render(bool clear=true);
111
112   bool willDraw() { return isDrawing && mHead; }
113
114   void toggleFreeze()  { shouldToggleFreeze = true; };
115   void toggleDrawing() 
116   {
117#ifdef ENABLE_DEBUGDRAW
118      isDrawing = !isDrawing;
119#endif
120   };
121
122
123   /// @name ddrawmeth Debug Draw Methods
124   ///
125   /// @{
126
127   void drawBoxOutline(const Point3F &a, const Point3F &b, const LinearColorF &color = LinearColorF(1.0f, 1.0f, 1.0f));
128   void drawTransformedBoxOutline(const Point3F &a, const Point3F &b, const LinearColorF &color, const MatrixF& transform);
129
130   void drawBox(const Point3F &a, const Point3F &b, const LinearColorF &color = LinearColorF(1.0f,1.0f,1.0f));
131   void drawLine(const Point3F &a, const Point3F &b, const LinearColorF &color = LinearColorF(1.0f,1.0f,1.0f));
132   void drawTri(const Point3F &a, const Point3F &b, const Point3F &c, const LinearColorF &color = LinearColorF(1.0f,1.0f,1.0f));
133   void drawText(const Point3F& pos, const String& text, const LinearColorF &color = LinearColorF(1.0f,1.0f,1.0f));
134   void drawCapsule(const Point3F &a, const F32 &radius, const F32 &height, const LinearColorF &color = LinearColorF(1.0f, 1.0f, 1.0f));
135   void drawDirectionLine(const Point3F &a, const Point3F &b, const LinearColorF &color = LinearColorF(1.0f, 1.0f, 1.0f));
136   void drawOutlinedText(const Point3F& pos, const String& text, const LinearColorF &color = LinearColorF(1.0f, 1.0f, 1.0f), const LinearColorF &colorOutline = LinearColorF(0.0f, 0.0f, 0.0f));
137   
138   /// Render a wireframe view of the given polyhedron.
139   void drawPolyhedron( const AnyPolyhedron& polyhedron, const LinearColorF& color = LinearColorF( 1.f, 1.f, 1.f ) );
140
141   /// Render the plane indices, edge indices, edge direction indicators, and point coordinates
142   /// of the given polyhedron for debugging.
143   ///
144   /// Green lines are plane normals.  Red lines point from edge midpoints along the edge direction (i.e. to the
145   /// second vertex).  This shows if the orientation is correct to yield CW ordering for face[0].  Indices and
146   /// coordinates of vertices are shown in white.  Plane indices are rendered in black.  Edge indices and their
147   /// plane indices are rendered in white.
148   void drawPolyhedronDebugInfo( const AnyPolyhedron& polyhedron, const MatrixF& transform, const Point3F& scale );
149
150   /// Set the TTL for the last item we entered...
151   ///
152   /// Primitives default to lasting one frame (ie, ttl=0)
153   enum : <a href="/coding/file/types_8h/#types_8h_1ac3df7cf3c8cb172a588adec881447d68">U32</a>
154   {
155      DD_INFINITE = U32_MAX
156   };
157   // How long should this primitive be draw for, 0 = one frame, DD_INFINITE = draw forever
158   void setLastTTL(U32 ms);
159
160   /// Disable/enable z testing on the last primitive.
161   ///
162   /// Primitives default to z testing on.
163   void setLastZTest(bool enabled);
164
165   /// @}
166private:
167   static DebugDrawer* sgDebugDrawer;
168
169   struct DebugPrim
170   {
171      /// Color used for this primitive.
172      LinearColorF color;
173      LinearColorF color2;
174
175      /// Points used to store positional data. Exact semantics determined by type.
176      Point3F a, b, c;
177      enum {
178         Tri,
179         Box,
180         Line,
181         Text,
182         DirectionLine,
183         OutlinedText,
184         Capsule,
185      } type;      ///< Type of the primitive. The meanings of a,b,c are determined by this.
186
187      SimTime dieTime;   ///< Time at which we should remove this from the list.
188      bool useZ; ///< If true, do z-checks for this primitive.      
189      char mText[256];      // Text to display
190
191      DebugPrim *next;
192   };
193
194
195   FreeListChunker<DebugPrim> mPrimChunker;
196   DebugPrim *mHead;
197
198   bool isFrozen;
199   bool shouldToggleFreeze;
200   bool isDrawing;   
201
202   GFXStateBlockRef mRenderZOffSB;
203   GFXStateBlockRef mRenderZOnSB;
204   GFXStateBlockRef mRenderAlpha;
205
206   Resource<GFont> mFont;
207
208   void setupStateBlocks();
209};
210
211#endif // _DEBUGDRAW_H_
212