DebugRenderer.h 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. //
  2. // Copyright (c) 2008-2020 the Urho3D project.
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #pragma once
  23. #include "../Math/Color.h"
  24. #include "../Math/Frustum.h"
  25. #include "../Scene/Component.h"
  26. namespace Urho3D
  27. {
  28. class BoundingBox;
  29. class Camera;
  30. class Polyhedron;
  31. class Drawable;
  32. class Light;
  33. class Matrix3x4;
  34. class Renderer;
  35. class Skeleton;
  36. class Sphere;
  37. class VertexBuffer;
  38. /// Debug rendering line.
  39. struct DebugLine
  40. {
  41. /// Construct undefined.
  42. DebugLine() = default;
  43. /// Construct with start and end positions and color.
  44. DebugLine(const Vector3& start, const Vector3& end, unsigned color) :
  45. start_(start),
  46. end_(end),
  47. color_(color)
  48. {
  49. }
  50. /// Start position.
  51. Vector3 start_;
  52. /// End position.
  53. Vector3 end_;
  54. /// Color.
  55. unsigned color_{};
  56. };
  57. /// Debug render triangle.
  58. struct DebugTriangle
  59. {
  60. /// Construct undefined.
  61. DebugTriangle() = default;
  62. /// Construct with start and end positions and color.
  63. DebugTriangle(const Vector3& v1, const Vector3& v2, const Vector3& v3, unsigned color) :
  64. v1_(v1),
  65. v2_(v2),
  66. v3_(v3),
  67. color_(color)
  68. {
  69. }
  70. /// Vertex a.
  71. Vector3 v1_;
  72. /// Vertex b.
  73. Vector3 v2_;
  74. /// Vertex c.
  75. Vector3 v3_;
  76. /// Color.
  77. unsigned color_{};
  78. };
  79. /// Debug geometry rendering component. Should be added only to the root scene node.
  80. class URHO3D_API DebugRenderer : public Component
  81. {
  82. URHO3D_OBJECT(DebugRenderer, Component);
  83. public:
  84. /// Construct.
  85. explicit DebugRenderer(Context* context);
  86. /// Destruct.
  87. ~DebugRenderer() override;
  88. /// Register object factory.
  89. static void RegisterObject(Context* context);
  90. /// Set line antialiasing on/off. Default false.
  91. /// @property
  92. void SetLineAntiAlias(bool enable);
  93. /// Set the camera viewpoint. Call before rendering, or before adding geometry if you want to use culling.
  94. void SetView(Camera* camera);
  95. /// Add a line.
  96. void AddLine(const Vector3& start, const Vector3& end, const Color& color, bool depthTest = true);
  97. /// Add a line with color already converted to unsigned.
  98. void AddLine(const Vector3& start, const Vector3& end, unsigned color, bool depthTest = true);
  99. /// Add a solid triangle.
  100. void AddTriangle(const Vector3& v1, const Vector3& v2, const Vector3& v3, const Color& color, bool depthTest = true);
  101. /// Add a solid triangle with color already converted to unsigned.
  102. void AddTriangle(const Vector3& v1, const Vector3& v2, const Vector3& v3, unsigned color, bool depthTest = true);
  103. /// Add a solid quadrangular polygon.
  104. void AddPolygon(const Vector3& v1, const Vector3& v2, const Vector3& v3, const Vector3& v4, const Color& color, bool depthTest = true);
  105. /// Add a solid quadrangular polygon with color already converted to unsigned.
  106. void AddPolygon(const Vector3& v1, const Vector3& v2, const Vector3& v3, const Vector3& v4, unsigned color, bool depthTest = true);
  107. /// Add a scene node represented as its coordinate axes.
  108. void AddNode(Node* node, float scale = 1.0f, bool depthTest = true);
  109. /// Add a bounding box.
  110. void AddBoundingBox(const BoundingBox& box, const Color& color, bool depthTest = true, bool solid = false);
  111. /// Add a bounding box with transform.
  112. void AddBoundingBox(const BoundingBox& box, const Matrix3x4& transform, const Color& color, bool depthTest = true, bool solid = false);
  113. /// Add a frustum.
  114. void AddFrustum(const Frustum& frustum, const Color& color, bool depthTest = true);
  115. /// Add a polyhedron.
  116. void AddPolyhedron(const Polyhedron& poly, const Color& color, bool depthTest = true);
  117. /// Add a sphere.
  118. void AddSphere(const Sphere& sphere, const Color& color, bool depthTest = true);
  119. /// Add a sphere sector. Angle ranges from 0 to 360. Identity Quaternion yields the filled portion of the sector upwards.
  120. void AddSphereSector(const Sphere& sphere, const Quaternion& rotation, float angle,
  121. bool drawLines, const Color& color, bool depthTest = true);
  122. /// Add a cylinder.
  123. void AddCylinder(const Vector3& position, float radius, float height, const Color& color, bool depthTest = true);
  124. /// Add a skeleton.
  125. void AddSkeleton(const Skeleton& skeleton, const Color& color, bool depthTest = true);
  126. /// Add a triangle mesh.
  127. void AddTriangleMesh(const void* vertexData, unsigned vertexSize, const void* indexData,
  128. unsigned indexSize, unsigned indexStart, unsigned indexCount, const Matrix3x4& transform, const Color& color, bool depthTest = true);
  129. /// Add a triangle mesh.
  130. void AddTriangleMesh(const void* vertexData, unsigned vertexSize, unsigned vertexStart, const void* indexData,
  131. unsigned indexSize, unsigned indexStart, unsigned indexCount, const Matrix3x4& transform, const Color& color, bool depthTest = true);
  132. /// Add a circle.
  133. void AddCircle(const Vector3& center, const Vector3& normal, float radius, const Color& color, int steps = 64, bool depthTest = true);
  134. /// Add a cross.
  135. void AddCross(const Vector3& center, float size, const Color& color, bool depthTest = true);
  136. /// Add a quad on the XZ plane.
  137. void AddQuad(const Vector3& center, float width, float height, const Color& color, bool depthTest = true);
  138. /// Update vertex buffer and render all debug lines. The viewport and rendertarget should be set before.
  139. void Render();
  140. /// Return whether line antialiasing is enabled.
  141. /// @property
  142. bool GetLineAntiAlias() const { return lineAntiAlias_; }
  143. /// Return the view transform.
  144. const Matrix3x4& GetView() const { return view_; }
  145. /// Return the projection transform.
  146. const Matrix4& GetProjection() const { return projection_; }
  147. /// Return the view frustum.
  148. const Frustum& GetFrustum() const { return frustum_; }
  149. /// Check whether a bounding box is inside the view frustum.
  150. bool IsInside(const BoundingBox& box) const;
  151. /// Return whether has something to render.
  152. bool HasContent() const;
  153. private:
  154. /// Handle end of frame. Clear debug geometry.
  155. void HandleEndFrame(StringHash eventType, VariantMap& eventData);
  156. /// Lines rendered with depth test.
  157. PODVector<DebugLine> lines_;
  158. /// Lines rendered without depth test.
  159. PODVector<DebugLine> noDepthLines_;
  160. /// Triangles rendered with depth test.
  161. PODVector<DebugTriangle> triangles_;
  162. /// Triangles rendered without depth test.
  163. PODVector<DebugTriangle> noDepthTriangles_;
  164. /// View transform.
  165. Matrix3x4 view_;
  166. /// Projection transform.
  167. Matrix4 projection_;
  168. /// Projection transform in API-specific format.
  169. Matrix4 gpuProjection_;
  170. /// View frustum.
  171. Frustum frustum_;
  172. /// Vertex buffer.
  173. SharedPtr<VertexBuffer> vertexBuffer_;
  174. /// Line antialiasing flag.
  175. bool lineAntiAlias_;
  176. };
  177. }