DebugDrawer.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. // Copyright (C) 2009-2016, Panagiotis Christopoulos Charitos and contributors.
  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 PortalComponent;
  18. class SectorComponent;
  19. class ReflectionProxyComponent;
  20. /// @addtogroup renderer
  21. /// @{
  22. /// Draws simple primitives
  23. class DebugDrawer
  24. {
  25. public:
  26. DebugDrawer();
  27. ~DebugDrawer();
  28. ANKI_USE_RESULT Error init(Renderer* r);
  29. void drawGrid();
  30. void drawSphere(F32 radius, I complexity = 8);
  31. void drawCube(F32 size = 1.0);
  32. void drawLine(const Vec3& from, const Vec3& to, const Vec4& color);
  33. void prepareFrame(CommandBufferPtr& jobs);
  34. void finishFrame();
  35. /// @name Render functions. Imitate the GL 1.1 immediate mode
  36. /// @{
  37. void begin(PrimitiveTopology topology); ///< Initiates the draw
  38. void end(); ///< Draws
  39. void pushBackVertex(const Vec3& pos); ///< Something like glVertex
  40. /// Something like glColor
  41. void setColor(const Vec3& col)
  42. {
  43. m_crntCol = col;
  44. }
  45. /// Something like glColor
  46. void setColor(const Vec4& col)
  47. {
  48. m_crntCol = col.xyz();
  49. }
  50. void setModelMatrix(const Mat4& m);
  51. void setViewProjectionMatrix(const Mat4& m);
  52. /// @}
  53. void setDepthTestEnabled(Bool enabled)
  54. {
  55. m_depthTestEnabled = enabled;
  56. }
  57. Bool getDepthTestEnabled() const
  58. {
  59. return m_depthTestEnabled;
  60. }
  61. private:
  62. class Vertex
  63. {
  64. public:
  65. Vec4 m_position;
  66. Vec4 m_color;
  67. };
  68. static const U MAX_VERTS_PER_FRAME = 1024 * 1024;
  69. Renderer* m_r;
  70. ShaderResourcePtr m_frag;
  71. ShaderResourcePtr m_vert;
  72. Array2d<PipelinePtr, 2, 2> m_pplines;
  73. Array<ResourceGroupPtr, MAX_FRAMES_IN_FLIGHT> m_rcGroup;
  74. Array<BufferPtr, MAX_FRAMES_IN_FLIGHT> m_vertBuff;
  75. CommandBufferPtr m_cmdb;
  76. WeakArray<Vertex> m_clientVerts;
  77. Mat4 m_mMat;
  78. Mat4 m_vpMat;
  79. Mat4 m_mvpMat; ///< Optimization.
  80. Vec3 m_crntCol = Vec3(1.0, 0.0, 0.0);
  81. PrimitiveTopology m_primitive = PrimitiveTopology::LINES;
  82. U32 m_frameVertCount = 0;
  83. U32 m_crntDrawVertCount = 0;
  84. DynamicArray<Vec3> m_sphereVerts;
  85. Bool8 m_depthTestEnabled = true;
  86. PipelinePtr& getPpline(Bool depth, PrimitiveTopology topology)
  87. {
  88. U i = (depth == false) ? 0 : 1;
  89. U j = (topology == PrimitiveTopology::LINES) ? 0 : 1;
  90. return m_pplines[i][j];
  91. }
  92. void flush();
  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. }
  103. void visit(const LineSegment&);
  104. void visit(const Obb&);
  105. void visit(const Frustum&);
  106. void visit(const Plane&);
  107. void visit(const Sphere&);
  108. void visit(const Aabb&);
  109. void visit(const CompoundShape&);
  110. void visit(const ConvexHullShape&);
  111. private:
  112. DebugDrawer* m_dbg; ///< The debug drawer
  113. };
  114. /// Implement physics debug drawer.
  115. class PhysicsDebugDrawer : public PhysicsDrawer
  116. {
  117. public:
  118. PhysicsDebugDrawer(DebugDrawer* dbg)
  119. : m_dbg(dbg)
  120. {
  121. }
  122. void drawLines(
  123. const Vec3* lines, const U32 linesCount, 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. }
  135. void draw(FrustumComponent& fr) const;
  136. void draw(SpatialComponent& sp) const;
  137. void draw(const PortalComponent& c) const;
  138. void draw(const SectorComponent& c) const;
  139. void drawPath(const Path& path) const;
  140. void draw(const ReflectionProxyComponent& proxy) const;
  141. private:
  142. DebugDrawer* m_dbg;
  143. };
  144. /// @}
  145. } // end namespace anki