DebugRenderer.h 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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. void SetLineAntiAlias(bool enable);
  92. /// Set the camera viewpoint. Call before rendering, or before adding geometry if you want to use culling.
  93. void SetView(Camera* camera);
  94. /// Add a line.
  95. void AddLine(const Vector3& start, const Vector3& end, const Color& color, bool depthTest = true);
  96. /// Add a line with color already converted to unsigned.
  97. void AddLine(const Vector3& start, const Vector3& end, unsigned color, bool depthTest = true);
  98. /// Add a solid triangle.
  99. void AddTriangle(const Vector3& v1, const Vector3& v2, const Vector3& v3, const Color& color, bool depthTest = true);
  100. /// Add a solid triangle with color already converted to unsigned.
  101. void AddTriangle(const Vector3& v1, const Vector3& v2, const Vector3& v3, unsigned color, bool depthTest = true);
  102. /// Add a solid quadrangular polygon.
  103. void AddPolygon(const Vector3& v1, const Vector3& v2, const Vector3& v3, const Vector3& v4, const Color& color, bool depthTest = true);
  104. /// Add a solid quadrangular polygon with color already converted to unsigned.
  105. void AddPolygon(const Vector3& v1, const Vector3& v2, const Vector3& v3, const Vector3& v4, unsigned color, bool depthTest = true);
  106. /// Add a scene node represented as its coordinate axes.
  107. void AddNode(Node* node, float scale = 1.0f, bool depthTest = true);
  108. /// Add a bounding box.
  109. void AddBoundingBox(const BoundingBox& box, const Color& color, bool depthTest = true, bool solid = false);
  110. /// Add a bounding box with transform.
  111. void AddBoundingBox(const BoundingBox& box, const Matrix3x4& transform, const Color& color, bool depthTest = true, bool solid = false);
  112. /// Add a frustum.
  113. void AddFrustum(const Frustum& frustum, const Color& color, bool depthTest = true);
  114. /// Add a polyhedron.
  115. void AddPolyhedron(const Polyhedron& poly, const Color& color, bool depthTest = true);
  116. /// Add a sphere.
  117. void AddSphere(const Sphere& sphere, const Color& color, bool depthTest = true);
  118. /// Add a sphere sector. Angle ranges from 0 to 360. Identity Quaternion yields the filled portion of the sector upwards.
  119. void AddSphereSector(const Sphere& sphere, const Quaternion& rotation, float angle,
  120. bool drawLines, const Color& color, bool depthTest = true);
  121. /// Add a cylinder.
  122. void AddCylinder(const Vector3& position, float radius, float height, const Color& color, bool depthTest = true);
  123. /// Add a skeleton.
  124. void AddSkeleton(const Skeleton& skeleton, const Color& color, bool depthTest = true);
  125. /// Add a triangle mesh.
  126. void AddTriangleMesh(const void* vertexData, unsigned vertexSize, const void* indexData,
  127. unsigned indexSize, unsigned indexStart, unsigned indexCount, const Matrix3x4& transform, const Color& color, bool depthTest = true);
  128. /// Add a triangle mesh.
  129. void AddTriangleMesh(const void* vertexData, unsigned vertexSize, unsigned vertexStart, const void* indexData,
  130. unsigned indexSize, unsigned indexStart, unsigned indexCount, const Matrix3x4& transform, const Color& color, bool depthTest = true);
  131. /// Add a circle.
  132. void AddCircle(const Vector3& center, const Vector3& normal, float radius, const Color& color, int steps = 64, bool depthTest = true);
  133. /// Add a cross.
  134. void AddCross(const Vector3& center, float size, const Color& color, bool depthTest = true);
  135. /// Add a quad on the XZ plane.
  136. void AddQuad(const Vector3& center, float width, float height, const Color& color, bool depthTest = true);
  137. /// Update vertex buffer and render all debug lines. The viewport and rendertarget should be set before.
  138. void Render();
  139. /// Return whether line antialiasing is enabled.
  140. bool GetLineAntiAlias() const { return lineAntiAlias_; }
  141. /// Return the view transform.
  142. const Matrix3x4& GetView() const { return view_; }
  143. /// Return the projection transform.
  144. const Matrix4& GetProjection() const { return projection_; }
  145. /// Return the view frustum.
  146. const Frustum& GetFrustum() const { return frustum_; }
  147. /// Check whether a bounding box is inside the view frustum.
  148. bool IsInside(const BoundingBox& box) const;
  149. /// Return whether has something to render.
  150. bool HasContent() const;
  151. private:
  152. /// Handle end of frame. Clear debug geometry.
  153. void HandleEndFrame(StringHash eventType, VariantMap& eventData);
  154. /// Lines rendered with depth test.
  155. PODVector<DebugLine> lines_;
  156. /// Lines rendered without depth test.
  157. PODVector<DebugLine> noDepthLines_;
  158. /// Triangles rendered with depth test.
  159. PODVector<DebugTriangle> triangles_;
  160. /// Triangles rendered without depth test.
  161. PODVector<DebugTriangle> noDepthTriangles_;
  162. /// View transform.
  163. Matrix3x4 view_;
  164. /// Projection transform.
  165. Matrix4 projection_;
  166. /// Projection transform in API-specific format.
  167. Matrix4 gpuProjection_;
  168. /// View frustum.
  169. Frustum frustum_;
  170. /// Vertex buffer.
  171. SharedPtr<VertexBuffer> vertexBuffer_;
  172. /// Line antialiasing flag.
  173. bool lineAntiAlias_;
  174. };
  175. }