DebugDrawer.h 3.8 KB

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