DebugDrawer.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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. #pragma once
  6. #include "anki/Math.h"
  7. #include "anki/Gr.h"
  8. #include "anki/collision/CollisionShape.h"
  9. #include "anki/physics/PhysicsDrawer.h"
  10. #include "anki/scene/Forward.h"
  11. #include "anki/resource/ShaderResource.h"
  12. #include "anki/util/Array.h"
  13. namespace anki {
  14. // Forward
  15. class Renderer;
  16. class PortalSectorComponent;
  17. /// @addtogroup renderer
  18. /// @{
  19. /// Draws simple primitives
  20. class DebugDrawer
  21. {
  22. public:
  23. DebugDrawer();
  24. ~DebugDrawer();
  25. ANKI_USE_RESULT Error create(Renderer* r);
  26. void drawGrid();
  27. void drawSphere(F32 radius, I complexity = 8);
  28. void drawCube(F32 size = 1.0);
  29. void drawLine(const Vec3& from, const Vec3& to, const Vec4& color);
  30. void prepareDraw(CommandBufferPtr& jobs)
  31. {
  32. m_cmdb = jobs;
  33. }
  34. void finishDraw()
  35. {
  36. m_cmdb = CommandBufferPtr(); // Release job chain
  37. }
  38. /// @name Render functions. Imitate the GL 1.1 immediate mode
  39. /// @{
  40. void begin(PrimitiveTopology topology); ///< Initiates the draw
  41. void end(); ///< Draws
  42. void pushBackVertex(const Vec3& pos); ///< Something like glVertex
  43. /// Something like glColor
  44. void setColor(const Vec3& col)
  45. {
  46. m_crntCol = col;
  47. }
  48. /// Something like glColor
  49. void setColor(const Vec4& col)
  50. {
  51. m_crntCol = col.xyz();
  52. }
  53. void setModelMatrix(const Mat4& m);
  54. void setViewProjectionMatrix(const Mat4& m);
  55. /// @}
  56. void setDepthTestEnabled(Bool enabled)
  57. {
  58. m_depthTestEnabled = enabled;
  59. }
  60. Bool getDepthTestEnabled() const
  61. {
  62. return m_depthTestEnabled;
  63. }
  64. /// This is the function that actualy draws
  65. ANKI_USE_RESULT Error flush();
  66. private:
  67. class Vertex
  68. {
  69. public:
  70. Vec4 m_position;
  71. Vec4 m_color;
  72. };
  73. ShaderResourcePtr m_frag;
  74. ShaderResourcePtr m_vert;
  75. PipelinePtr m_pplineLinesDepth;
  76. PipelinePtr m_pplineLinesNoDepth;
  77. CommandBufferPtr m_cmdb;
  78. ResourceGroupPtr m_rcGroup;
  79. static const U MAX_POINTS_PER_DRAW = 256;
  80. Mat4 m_mMat;
  81. Mat4 m_vpMat;
  82. Mat4 m_mvpMat; ///< Optimization
  83. U32 m_lineVertCount;
  84. U32 m_triVertCount;
  85. Vec3 m_crntCol;
  86. PrimitiveTopology m_primitive;
  87. BufferPtr m_vertBuff;
  88. Array<Vertex, MAX_POINTS_PER_DRAW> m_clientLineVerts;
  89. Array<Vertex, MAX_POINTS_PER_DRAW> m_clientTriVerts;
  90. DArray<Vec3> m_sphereVerts;
  91. Bool8 m_depthTestEnabled = true;
  92. ANKI_USE_RESULT Error flushInternal(PrimitiveTopology topology);
  93. };
  94. /// Contains methods to render the collision shapes
  95. class CollisionDebugDrawer: public CollisionShape::ConstVisitor
  96. {
  97. public:
  98. /// Constructor
  99. CollisionDebugDrawer(DebugDrawer* dbg)
  100. : m_dbg(dbg)
  101. {}
  102. void visit(const LineSegment&);
  103. void visit(const Obb&);
  104. void visit(const Frustum&);
  105. void visit(const Plane&);
  106. void visit(const Sphere&);
  107. void visit(const Aabb&);
  108. void visit(const CompoundShape&);
  109. void visit(const ConvexHullShape&);
  110. private:
  111. DebugDrawer* m_dbg; ///< The debug drawer
  112. };
  113. /// Implement physics debug drawer.
  114. class PhysicsDebugDrawer: public PhysicsDrawer
  115. {
  116. public:
  117. PhysicsDebugDrawer(DebugDrawer* dbg)
  118. : m_dbg(dbg)
  119. {}
  120. void drawLines(
  121. const Vec3* lines,
  122. const U32 linesCount,
  123. const Vec4& color) final;
  124. private:
  125. DebugDrawer* m_dbg; ///< The debug drawer
  126. };
  127. /// This is a drawer for some scene nodes that need debug
  128. class SceneDebugDrawer
  129. {
  130. public:
  131. SceneDebugDrawer(DebugDrawer* d)
  132. : m_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. m_dbg->setViewProjectionMatrix(m);
  141. }
  142. private:
  143. DebugDrawer* m_dbg;
  144. void draw(FrustumComponent& fr) const;
  145. void draw(SpatialComponent& sp) const;
  146. void draw(const PortalSectorComponent& c) const;
  147. void drawPath(const Path& path) const;
  148. };
  149. /// @}
  150. } // end namespace anki