DebugDrawer.h 3.8 KB

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