DebugRenderer.h 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. //
  2. // Copyright (c) 2008-2017 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 Atomic
  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()
  43. {
  44. }
  45. /// Construct with start and end positions and color.
  46. DebugLine(const Vector3& start, const Vector3& end, unsigned color) :
  47. start_(start),
  48. end_(end),
  49. color_(color)
  50. {
  51. }
  52. /// Start position.
  53. Vector3 start_;
  54. /// End position.
  55. Vector3 end_;
  56. /// Color.
  57. unsigned color_;
  58. };
  59. /// Debug render triangle.
  60. struct DebugTriangle
  61. {
  62. /// Construct undefined.
  63. DebugTriangle()
  64. {
  65. }
  66. /// Construct with start and end positions and color.
  67. DebugTriangle(const Vector3& v1, const Vector3& v2, const Vector3& v3, unsigned color) :
  68. v1_(v1),
  69. v2_(v2),
  70. v3_(v3),
  71. color_(color)
  72. {
  73. }
  74. /// Vertex a.
  75. Vector3 v1_;
  76. /// Vertex b.
  77. Vector3 v2_;
  78. /// Vertex c.
  79. Vector3 v3_;
  80. /// Color.
  81. unsigned color_;
  82. };
  83. /// Debug geometry rendering component. Should be added only to the root scene node.
  84. class ATOMIC_API DebugRenderer : public Component
  85. {
  86. ATOMIC_OBJECT(DebugRenderer, Component);
  87. public:
  88. /// Construct.
  89. DebugRenderer(Context* context);
  90. /// Destruct.
  91. virtual ~DebugRenderer();
  92. /// Register object factory.
  93. static void RegisterObject(Context* context);
  94. /// Set line antialiasing on/off. Default false.
  95. void SetLineAntiAlias(bool enable);
  96. /// Set the camera viewpoint. Call before rendering, or before adding geometry if you want to use culling.
  97. void SetView(Camera* camera);
  98. /// Add a line.
  99. void AddLine(const Vector3& start, const Vector3& end, const Color& color, bool depthTest = true);
  100. /// Add a line with color already converted to unsigned.
  101. void AddLine(const Vector3& start, const Vector3& end, unsigned color, bool depthTest = true);
  102. /// Add a solid triangle.
  103. void AddTriangle(const Vector3& v1, const Vector3& v2, const Vector3& v3, const Color& color, bool depthTest = true);
  104. /// Add a solid triangle with color already converted to unsigned.
  105. void AddTriangle(const Vector3& v1, const Vector3& v2, const Vector3& v3, unsigned color, bool depthTest = true);
  106. /// Add a solid quadrangular polygon.
  107. void AddPolygon(const Vector3& v1, const Vector3& v2, const Vector3& v3, const Vector3& v4, const Color& color, bool depthTest = true);
  108. /// Add a solid quadrangular polygon with color already converted to unsigned.
  109. void AddPolygon(const Vector3& v1, const Vector3& v2, const Vector3& v3, const Vector3& v4, unsigned color, bool depthTest = true);
  110. /// Add a scene node represented as its coordinate axes.
  111. void AddNode(Node* node, float scale = 1.0f, bool depthTest = true);
  112. /// Add a bounding box.
  113. void AddBoundingBox(const BoundingBox& box, const Color& color, bool depthTest = true, bool solid = false);
  114. /// Add a bounding box with transform.
  115. void AddBoundingBox(const BoundingBox& box, const Matrix3x4& transform, const Color& color, bool depthTest = true, bool solid = false);
  116. /// Add a frustum.
  117. void AddFrustum(const Frustum& frustum, const Color& color, bool depthTest = true);
  118. /// Add a polyhedron.
  119. void AddPolyhedron(const Polyhedron& poly, const Color& color, bool depthTest = true);
  120. /// Add a sphere.
  121. void AddSphere(const Sphere& sphere, const Color& color, bool depthTest = true);
  122. /// Add a sphere sector.
  123. void AddSphereSector(const Sphere& sphere, const Quaternion& rotation, float angle,
  124. bool drawLines, const Color& color, bool depthTest = true);
  125. /// Add a cylinder
  126. void AddCylinder(const Vector3& position, float radius, float height, const Color& color, bool depthTest = true);
  127. /// Add a skeleton.
  128. void AddSkeleton(const Skeleton& skeleton, const Color& color, bool depthTest = true);
  129. /// Add a triangle mesh.
  130. void AddTriangleMesh
  131. (const void* vertexData, unsigned vertexSize, const void* indexData, unsigned indexSize, unsigned indexStart,
  132. unsigned indexCount, const Matrix3x4& transform, const Color& color, bool depthTest = true);
  133. /// Add a circle.
  134. void AddCircle(const Vector3& center, const Vector3& normal, float radius, const Color& color, int steps = 64, bool depthTest = true);
  135. /// Add a cross.
  136. void AddCross(const Vector3& center, float size, const Color& color, bool depthTest = true);
  137. /// Add a quad on the XZ plane.
  138. void AddQuad(const Vector3& center, float width, float height, const Color& color, bool depthTest = true);
  139. /// Update vertex buffer and render all debug lines. The viewport and rendertarget should be set before.
  140. void Render();
  141. /// Return whether line antialiasing is enabled.
  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. // ATOMIC BEGIN
  154. /// Creates a grid on all axis
  155. void CreateGrid(const Color& grid, bool depthTest, Vector3 position);
  156. void CreateXAxisLines(unsigned gridColor, bool depthTest, int x, int y, int z);
  157. void CreateZAxisLines(unsigned gridColor, bool depthTest, int x, int y, int z);
  158. // ATOMIC END
  159. private:
  160. /// Handle end of frame. Clear debug geometry.
  161. void HandleEndFrame(StringHash eventType, VariantMap& eventData);
  162. /// Lines rendered with depth test.
  163. PODVector<DebugLine> lines_;
  164. /// Lines rendered without depth test.
  165. PODVector<DebugLine> noDepthLines_;
  166. /// Triangles rendered with depth test.
  167. PODVector<DebugTriangle> triangles_;
  168. /// Triangles rendered without depth test.
  169. PODVector<DebugTriangle> noDepthTriangles_;
  170. /// View transform.
  171. Matrix3x4 view_;
  172. /// Projection transform.
  173. Matrix4 projection_;
  174. /// Projection transform in API-specific format.
  175. Matrix4 gpuProjection_;
  176. /// View frustum.
  177. Frustum frustum_;
  178. /// Vertex buffer.
  179. SharedPtr<VertexBuffer> vertexBuffer_;
  180. /// Line antialiasing flag.
  181. bool lineAntiAlias_;
  182. // ATOMIC BEGIN
  183. /// Positioning of grid lines point 1
  184. Vector3 position1_;
  185. /// Positioning of grid lines point 2
  186. Vector3 position2_;
  187. /// Positioning of grid lines point 3
  188. Vector3 position3_;
  189. /// Number of total grid lines
  190. int numGridLines_;
  191. /// Length of a grid line
  192. int lineLength_;
  193. /// Offset centres the grid
  194. int offset_;
  195. /// Scales the grid according to y-position of camera
  196. int scale_;
  197. /// The amount the scale gets incremented
  198. int scaleIncrement_;
  199. // ATOMIC END
  200. };
  201. }