DebugDrawer.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. // Copyright (C) 2009-2015, Panagiotis Christopoulos Charitos.
  2. // All rights reserved.
  3. // Code licensed under the BSD License.
  4. // http://www.anki3d.org/LICENSE
  5. #ifndef ANKI_RENDERER_DEBUG_DRAWER_H
  6. #define ANKI_RENDERER_DEBUG_DRAWER_H
  7. #include "anki/Math.h"
  8. #include "anki/Gl.h"
  9. #include "anki/resource/Resource.h"
  10. #include "anki/collision/CollisionShape.h"
  11. #include "anki/physics/PhysicsDrawer.h"
  12. #include "anki/scene/Forward.h"
  13. #include "anki/util/Array.h"
  14. #include <unordered_map>
  15. namespace anki {
  16. // Forward
  17. class Renderer;
  18. /// @addtogroup renderer
  19. /// @{
  20. /// Draws simple primitives
  21. class DebugDrawer
  22. {
  23. public:
  24. DebugDrawer();
  25. ~DebugDrawer();
  26. ANKI_USE_RESULT Error create(Renderer* r);
  27. void drawGrid();
  28. void drawSphere(F32 radius, I complexity = 8);
  29. void drawCube(F32 size = 1.0);
  30. void drawLine(const Vec3& from, const Vec3& to, const Vec4& color);
  31. void prepareDraw(GlCommandBufferHandle& jobs)
  32. {
  33. m_jobs = jobs;
  34. }
  35. void finishDraw()
  36. {
  37. m_jobs = GlCommandBufferHandle(); // Release job chain
  38. }
  39. /// @name Render functions. Imitate the GL 1.1 immediate mode
  40. /// @{
  41. void begin(GLenum primitive); ///< Initiates the draw
  42. void end(); ///< Draws
  43. void pushBackVertex(const Vec3& pos); ///< Something like glVertex
  44. /// Something like glColor
  45. void setColor(const Vec3& col)
  46. {
  47. m_crntCol = col;
  48. }
  49. /// Something like glColor
  50. void setColor(const Vec4& col)
  51. {
  52. m_crntCol = col.xyz();
  53. }
  54. void setModelMatrix(const Mat4& m);
  55. void setViewProjectionMatrix(const Mat4& m);
  56. /// @}
  57. /// This is the function that actualy draws
  58. ANKI_USE_RESULT Error flush();
  59. private:
  60. class Vertex
  61. {
  62. public:
  63. Vec4 m_position;
  64. Vec4 m_color;
  65. };
  66. ProgramResourcePointer m_frag;
  67. ProgramResourcePointer m_vert;
  68. GlPipelineHandle m_ppline;
  69. GlCommandBufferHandle m_jobs;
  70. static const U MAX_POINTS_PER_DRAW = 256;
  71. Mat4 m_mMat;
  72. Mat4 m_vpMat;
  73. Mat4 m_mvpMat; ///< Optimization
  74. U32 m_lineVertCount;
  75. U32 m_triVertCount;
  76. Vec3 m_crntCol;
  77. GLenum m_primitive;
  78. GlBufferHandle m_vertBuff;
  79. Array<Vertex, MAX_POINTS_PER_DRAW> m_clientLineVerts;
  80. Array<Vertex, MAX_POINTS_PER_DRAW> m_clientTriVerts;
  81. DArray<Vec3> m_sphereVerts;
  82. ANKI_USE_RESULT Error flushInternal(GLenum primitive);
  83. };
  84. /// Contains methods to render the collision shapes
  85. class CollisionDebugDrawer: public CollisionShape::ConstVisitor
  86. {
  87. public:
  88. /// Constructor
  89. CollisionDebugDrawer(DebugDrawer* dbg)
  90. : m_dbg(dbg)
  91. {}
  92. void visit(const LineSegment&);
  93. void visit(const Obb&);
  94. void visit(const Frustum&);
  95. void visit(const Plane&);
  96. void visit(const Sphere&);
  97. void visit(const Aabb&);
  98. void visit(const CompoundShape&);
  99. void visit(const ConvexHullShape&);
  100. private:
  101. DebugDrawer* m_dbg; ///< The debug drawer
  102. };
  103. /// Implement physics debug drawer.
  104. class PhysicsDebugDrawer: public PhysicsDrawer
  105. {
  106. public:
  107. PhysicsDebugDrawer(DebugDrawer* dbg)
  108. : m_dbg(dbg)
  109. {}
  110. void drawLines(
  111. const Vec3* lines,
  112. const U32 linesCount,
  113. const Vec4& color) final;
  114. private:
  115. DebugDrawer* m_dbg; ///< The debug drawer
  116. };
  117. /// This is a drawer for some scene nodes that need debug
  118. class SceneDebugDrawer
  119. {
  120. public:
  121. SceneDebugDrawer(DebugDrawer* d)
  122. : m_dbg(d)
  123. {}
  124. ~SceneDebugDrawer()
  125. {}
  126. void draw(SceneNode& node);
  127. //void draw(const Sector& sector);
  128. void setViewProjectionMatrix(const Mat4& m)
  129. {
  130. m_dbg->setViewProjectionMatrix(m);
  131. }
  132. private:
  133. DebugDrawer* m_dbg;
  134. void draw(FrustumComponent& fr) const;
  135. void draw(SpatialComponent& sp) const;
  136. void drawPath(const Path& path) const;
  137. };
  138. /// @}
  139. } // end namespace anki
  140. #endif