DebugDrawer.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. #ifndef ANKI_RENDERER_DEBUG_DRAWER_H
  2. #define ANKI_RENDERER_DEBUG_DRAWER_H
  3. #include "anki/math/Math.h"
  4. #include "anki/gl/Vbo.h"
  5. #include "anki/gl/Vao.h"
  6. #include "anki/resource/Resource.h"
  7. #include "anki/collision/CollisionShape.h"
  8. #include "anki/scene/SceneNode.h"
  9. #include "anki/util/Flags.h"
  10. #include "anki/util/Array.h"
  11. #include <unordered_map>
  12. #include <LinearMath/btIDebugDraw.h>
  13. namespace anki {
  14. /// Draws simple primitives
  15. class DebugDrawer
  16. {
  17. public:
  18. DebugDrawer();
  19. ~DebugDrawer();
  20. void drawGrid();
  21. void drawSphere(F32 radius, int complexity = 4);
  22. void drawCube(F32 size = 1.0);
  23. void drawLine(const Vec3& from, const Vec3& to, const Vec4& color);
  24. /// @name Render functions. Imitate the GL 1.1 immediate mode
  25. /// @{
  26. void begin(); ///< Initiates the draw
  27. void end(); ///< Draws
  28. void pushBackVertex(const Vec3& pos); ///< Something like glVertex
  29. /// Something like glColor
  30. void setColor(const Vec3& col)
  31. {
  32. crntCol = col;
  33. }
  34. /// Something like glColor
  35. void setColor(const Vec4& col)
  36. {
  37. crntCol = Vec3(col);
  38. }
  39. void setModelMatrix(const Mat4& m);
  40. void setViewProjectionMatrix(const Mat4& m);
  41. /// @}
  42. /// This is the function that actualy draws
  43. void flush();
  44. private:
  45. struct Vertex
  46. {
  47. Vec4 position;
  48. Vec4 color;
  49. Mat4 matrix;
  50. };
  51. ShaderProgramResourcePointer prog;
  52. static const U MAX_POINTS_PER_DRAW = 256;
  53. Mat4 mMat;
  54. Mat4 vpMat;
  55. Mat4 mvpMat; ///< Optimization
  56. U vertexPointer;
  57. Vec3 crntCol;
  58. Array<Vertex, MAX_POINTS_PER_DRAW> clientVerts;
  59. Vbo vbo;
  60. Vao vao;
  61. /// This is a container of some precalculated spheres. Its a map that
  62. /// from sphere complexity it returns a vector of lines (Vec3s in
  63. /// pairs)
  64. std::unordered_map<U32, Vector<Vec3>> complexityToPreCalculatedSphere;
  65. };
  66. /// Contains methods to render the collision shapes
  67. class CollisionDebugDrawer: public CollisionShape::ConstVisitor
  68. {
  69. public:
  70. /// Constructor
  71. CollisionDebugDrawer(DebugDrawer* dbg_)
  72. : dbg(dbg_)
  73. {}
  74. void visit(const LineSegment&)
  75. {
  76. /// XXX
  77. ANKI_ASSERT(0 && "ToDo");
  78. }
  79. void visit(const Obb&);
  80. void visit(const Frustum&);
  81. void visit(const Plane&);
  82. void visit(const Ray&)
  83. {
  84. ANKI_ASSERT(0 && "ToDo");
  85. }
  86. void visit(const Sphere&);
  87. void visit(const Aabb&);
  88. private:
  89. DebugDrawer* dbg; ///< The debug drawer
  90. };
  91. /// An implementation of btIDebugDraw used for debugging Bullet. See Bullet
  92. /// docs for details
  93. class PhysicsDebugDrawer: public btIDebugDraw
  94. {
  95. public:
  96. PhysicsDebugDrawer(DebugDrawer* dbg_)
  97. : dbg(dbg_)
  98. {}
  99. void drawLine(const btVector3& from, const btVector3& to,
  100. const btVector3& color);
  101. void drawContactPoint(const btVector3& pointOnB,
  102. const btVector3& normalOnB, btScalar distance, int lifeTime,
  103. const btVector3& color);
  104. void drawSphere(btScalar radius, const btTransform& transform,
  105. const btVector3& color);
  106. void drawBox(const btVector3& bbMin, const btVector3& bbMax,
  107. const btVector3& color);
  108. void drawBox(const btVector3& bbMin, const btVector3& bbMax,
  109. const btTransform& trans, const btVector3& color);
  110. void reportErrorWarning(const char* warningString);
  111. void draw3dText(const btVector3& location, const char* textString);
  112. void setDebugMode(int debugMode_)
  113. {
  114. debugMode = debugMode_;
  115. }
  116. int getDebugMode() const
  117. {
  118. return debugMode;
  119. }
  120. private:
  121. int debugMode;
  122. DebugDrawer* dbg;
  123. };
  124. // Forward
  125. class Octree;
  126. class OctreeNode;
  127. class Renderer;
  128. class Camera;
  129. /// This is a drawer for some scene nodes that need debug
  130. class SceneDebugDrawer: public Flags<U32>
  131. {
  132. public:
  133. enum DebugFlag
  134. {
  135. DF_NONE = 0,
  136. DF_SPATIAL = 1 << 0,
  137. DF_FRUSTUMABLE = 1 << 1
  138. };
  139. SceneDebugDrawer(DebugDrawer* d)
  140. : Flags<U32>(DF_SPATIAL | DF_FRUSTUMABLE), dbg(d)
  141. {}
  142. virtual ~SceneDebugDrawer()
  143. {}
  144. void draw(SceneNode& node);
  145. virtual void draw(const Octree& octree) const;
  146. void setViewProjectionMatrix(const Mat4& m)
  147. {
  148. dbg->setViewProjectionMatrix(m);
  149. }
  150. private:
  151. DebugDrawer* dbg;
  152. virtual void draw(Frustumable& fr) const;
  153. virtual void draw(Spatial& sp) const;
  154. virtual void draw(const OctreeNode& octnode,
  155. U32 depth, const Octree& octree) const;
  156. };
  157. } // end namespace anki
  158. #endif