DebugRenderer.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. // Copyright (c) 2008-2023 the Urho3D project
  2. // License: MIT
  3. #pragma once
  4. #include "../Math/Color.h"
  5. #include "../Math/Frustum.h"
  6. #include "../Scene/Component.h"
  7. namespace Urho3D
  8. {
  9. class BoundingBox;
  10. class Camera;
  11. class Polyhedron;
  12. class Drawable;
  13. class Light;
  14. class Matrix3x4;
  15. class Renderer;
  16. class Skeleton;
  17. class Sphere;
  18. class VertexBuffer;
  19. /// Debug rendering line.
  20. struct DebugLine
  21. {
  22. /// Construct undefined.
  23. DebugLine() = default;
  24. /// Construct with start and end positions and color.
  25. DebugLine(const Vector3& start, const Vector3& end, unsigned color) :
  26. start_(start),
  27. end_(end),
  28. color_(color)
  29. {
  30. }
  31. /// Start position.
  32. Vector3 start_;
  33. /// End position.
  34. Vector3 end_;
  35. /// Color.
  36. unsigned color_{};
  37. };
  38. /// Debug render triangle.
  39. struct DebugTriangle
  40. {
  41. /// Construct undefined.
  42. DebugTriangle() = default;
  43. /// Construct with start and end positions and color.
  44. DebugTriangle(const Vector3& v1, const Vector3& v2, const Vector3& v3, unsigned color) :
  45. v1_(v1),
  46. v2_(v2),
  47. v3_(v3),
  48. color_(color)
  49. {
  50. }
  51. /// Vertex a.
  52. Vector3 v1_;
  53. /// Vertex b.
  54. Vector3 v2_;
  55. /// Vertex c.
  56. Vector3 v3_;
  57. /// Color.
  58. unsigned color_{};
  59. };
  60. /// Debug geometry rendering component. Should be added only to the root scene node.
  61. class URHO3D_API DebugRenderer : public Component
  62. {
  63. URHO3D_OBJECT(DebugRenderer, Component);
  64. public:
  65. /// Construct.
  66. explicit DebugRenderer(Context* context);
  67. /// Destruct.
  68. ~DebugRenderer() override;
  69. /// Register object factory.
  70. /// @nobind
  71. static void RegisterObject(Context* context);
  72. /// Set line antialiasing on/off. Default false.
  73. /// @property
  74. void SetLineAntiAlias(bool enable);
  75. /// Set the camera viewpoint. Call before rendering, or before adding geometry if you want to use culling.
  76. void SetView(Camera* camera);
  77. /// Add a line.
  78. void AddLine(const Vector3& start, const Vector3& end, const Color& color, bool depthTest = true);
  79. /// Add a line with color already converted to unsigned.
  80. void AddLine(const Vector3& start, const Vector3& end, unsigned color, bool depthTest = true);
  81. /// Add a solid triangle.
  82. void AddTriangle(const Vector3& v1, const Vector3& v2, const Vector3& v3, const Color& color, bool depthTest = true);
  83. /// Add a solid triangle with color already converted to unsigned.
  84. void AddTriangle(const Vector3& v1, const Vector3& v2, const Vector3& v3, unsigned color, bool depthTest = true);
  85. /// Add a solid quadrangular polygon.
  86. void AddPolygon(const Vector3& v1, const Vector3& v2, const Vector3& v3, const Vector3& v4, const Color& color, bool depthTest = true);
  87. /// Add a solid quadrangular polygon with color already converted to unsigned.
  88. void AddPolygon(const Vector3& v1, const Vector3& v2, const Vector3& v3, const Vector3& v4, unsigned color, bool depthTest = true);
  89. /// Add a scene node represented as its coordinate axes.
  90. void AddNode(Node* node, float scale = 1.0f, bool depthTest = true);
  91. /// Add a bounding box.
  92. void AddBoundingBox(const BoundingBox& box, const Color& color, bool depthTest = true, bool solid = false);
  93. /// Add a bounding box with transform.
  94. void AddBoundingBox(const BoundingBox& box, const Matrix3x4& transform, const Color& color, bool depthTest = true, bool solid = false);
  95. /// Add a frustum.
  96. void AddFrustum(const Frustum& frustum, const Color& color, bool depthTest = true);
  97. /// Add a polyhedron.
  98. void AddPolyhedron(const Polyhedron& poly, const Color& color, bool depthTest = true);
  99. /// Add a sphere.
  100. void AddSphere(const Sphere& sphere, const Color& color, bool depthTest = true);
  101. /// Add a sphere sector. Angle ranges from 0 to 360. Identity Quaternion yields the filled portion of the sector upwards.
  102. void AddSphereSector(const Sphere& sphere, const Quaternion& rotation, float angle,
  103. bool drawLines, const Color& color, bool depthTest = true);
  104. /// Add a cylinder.
  105. void AddCylinder(const Vector3& position, float radius, float height, const Color& color, bool depthTest = true);
  106. /// Add a skeleton.
  107. void AddSkeleton(const Skeleton& skeleton, const Color& color, bool depthTest = true);
  108. /// Add a triangle mesh.
  109. void AddTriangleMesh(const void* vertexData, unsigned vertexSize, const void* indexData,
  110. unsigned indexSize, unsigned indexStart, unsigned indexCount, const Matrix3x4& transform, const Color& color, bool depthTest = true);
  111. /// Add a triangle mesh.
  112. void AddTriangleMesh(const void* vertexData, unsigned vertexSize, unsigned vertexStart, const void* indexData,
  113. unsigned indexSize, unsigned indexStart, unsigned indexCount, const Matrix3x4& transform, const Color& color, bool depthTest = true);
  114. /// Add a circle.
  115. void AddCircle(const Vector3& center, const Vector3& normal, float radius, const Color& color, int steps = 64, bool depthTest = true);
  116. /// Add a cross.
  117. void AddCross(const Vector3& center, float size, const Color& color, bool depthTest = true);
  118. /// Add a quad on the XZ plane.
  119. void AddQuad(const Vector3& center, float width, float height, const Color& color, bool depthTest = true);
  120. /// Update vertex buffer and render all debug lines. The viewport and rendertarget should be set before.
  121. void Render();
  122. /// Return whether line antialiasing is enabled.
  123. /// @property
  124. bool GetLineAntiAlias() const { return lineAntiAlias_; }
  125. /// Return the view transform.
  126. const Matrix3x4& GetView() const { return view_; }
  127. /// Return the projection transform.
  128. const Matrix4& GetProjection() const { return projection_; }
  129. /// Return the view frustum.
  130. const Frustum& GetFrustum() const { return frustum_; }
  131. /// Check whether a bounding box is inside the view frustum.
  132. bool IsInside(const BoundingBox& box) const;
  133. /// Return whether has something to render.
  134. bool HasContent() const;
  135. private:
  136. /// Handle end of frame. Clear debug geometry.
  137. void HandleEndFrame(StringHash eventType, VariantMap& eventData);
  138. /// Lines rendered with depth test.
  139. Vector<DebugLine> lines_;
  140. /// Lines rendered without depth test.
  141. Vector<DebugLine> noDepthLines_;
  142. /// Triangles rendered with depth test.
  143. Vector<DebugTriangle> triangles_;
  144. /// Triangles rendered without depth test.
  145. Vector<DebugTriangle> noDepthTriangles_;
  146. /// View transform.
  147. Matrix3x4 view_;
  148. /// Projection transform.
  149. Matrix4 projection_;
  150. /// Projection transform in API-specific format.
  151. Matrix4 gpuProjection_;
  152. /// View frustum.
  153. Frustum frustum_;
  154. /// Vertex buffer.
  155. SharedPtr<VertexBuffer> vertexBuffer_;
  156. /// Line antialiasing flag.
  157. bool lineAntiAlias_;
  158. };
  159. }