DebugRenderer.h 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. //
  2. // Copyright (c) 2008-2016 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 the camera viewpoint. Call before rendering, or before adding geometry if you want to use culling.
  95. void SetView(Camera* camera);
  96. /// Add a line.
  97. void AddLine(const Vector3& start, const Vector3& end, const Color& color, bool depthTest = true);
  98. /// Add a line with color already converted to unsigned.
  99. void AddLine(const Vector3& start, const Vector3& end, unsigned color, bool depthTest = true);
  100. /// Add a triangle.
  101. void AddTriangle(const Vector3& v1, const Vector3& v2, const Vector3& v3, const Color& color, bool depthTest = true);
  102. /// Add a triangle with color already converted to unsigned.
  103. void AddTriangle(const Vector3& v1, const Vector3& v2, const Vector3& v3, unsigned color, bool depthTest = true);
  104. /// Add a scene node represented as its coordinate axes.
  105. void AddNode(Node* node, float scale = 1.0f, bool depthTest = true);
  106. /// Add a bounding box.
  107. void AddBoundingBox(const BoundingBox& box, const Color& color, bool depthTest = true);
  108. /// Add a bounding box with transform.
  109. void AddBoundingBox(const BoundingBox& box, const Matrix3x4& transform, const Color& color, bool depthTest = true);
  110. /// Add a frustum.
  111. void AddFrustum(const Frustum& frustum, const Color& color, bool depthTest = true);
  112. /// Add a polyhedron.
  113. void AddPolyhedron(const Polyhedron& poly, const Color& color, bool depthTest = true);
  114. /// Add a sphere.
  115. void AddSphere(const Sphere& sphere, const Color& color, bool depthTest = true);
  116. /// Add a cylinder
  117. void AddCylinder(const Vector3& position, float radius, float height, const Color& color, bool depthTest = true);
  118. /// Add a skeleton.
  119. void AddSkeleton(const Skeleton& skeleton, const Color& color, bool depthTest = true);
  120. /// Add a triangle mesh.
  121. void AddTriangleMesh
  122. (const void* vertexData, unsigned vertexSize, const void* indexData, unsigned indexSize, unsigned indexStart,
  123. unsigned indexCount, const Matrix3x4& transform, const Color& color, bool depthTest = true);
  124. /// Add a circle.
  125. void AddCircle(const Vector3& center, const Vector3& normal, float radius, const Color& color, int steps = 64, bool depthTest = true);
  126. /// Add a cross.
  127. void AddCross(const Vector3& center, float size, const Color& color, bool depthTest = true);
  128. /// Add a quad on the XZ plane.
  129. void AddQuad(const Vector3& center, float width, float height, const Color& color, bool depthTest = true);
  130. /// Update vertex buffer and render all debug lines. The viewport and rendertarget should be set before.
  131. void Render();
  132. /// Return the view transform.
  133. const Matrix3x4& GetView() const { return view_; }
  134. /// Return the projection transform.
  135. const Matrix4& GetProjection() const { return projection_; }
  136. /// Return the view frustum.
  137. const Frustum& GetFrustum() const { return frustum_; }
  138. /// Check whether a bounding box is inside the view frustum.
  139. bool IsInside(const BoundingBox& box) const;
  140. /// Return whether has something to render.
  141. bool HasContent() const;
  142. // ATOMIC BEGIN
  143. /// Creates a grid on all axis
  144. void CreateGrid(const Color& grid, bool depthTest, Vector3 position);
  145. void CreateXAxisLines(unsigned gridColor, bool depthTest, int x, int y, int z);
  146. void CreateZAxisLines(unsigned gridColor, bool depthTest, int x, int y, int z);
  147. // ATOMIC END
  148. private:
  149. /// Handle end of frame. Clear debug geometry.
  150. void HandleEndFrame(StringHash eventType, VariantMap& eventData);
  151. /// Lines rendered with depth test.
  152. PODVector<DebugLine> lines_;
  153. /// Lines rendered without depth test.
  154. PODVector<DebugLine> noDepthLines_;
  155. /// Triangles rendered with depth test.
  156. PODVector<DebugTriangle> triangles_;
  157. /// Triangles rendered without depth test.
  158. PODVector<DebugTriangle> noDepthTriangles_;
  159. /// View transform.
  160. Matrix3x4 view_;
  161. /// Projection transform.
  162. Matrix4 projection_;
  163. /// Projection transform in API-specific format.
  164. Matrix4 gpuProjection_;
  165. /// View frustum.
  166. Frustum frustum_;
  167. /// Vertex buffer.
  168. SharedPtr<VertexBuffer> vertexBuffer_;
  169. // ATOMIC BEGIN
  170. /// Positioning of grid lines point 1
  171. Vector3 position1_;
  172. /// Positioning of grid lines point 2
  173. Vector3 position2_;
  174. /// Positioning of grid lines point 3
  175. Vector3 position3_;
  176. /// Number of total grid lines
  177. int numGridLines_;
  178. /// Length of a grid line
  179. int lineLength_;
  180. /// Offset centres the grid
  181. int offset_;
  182. /// Scales the grid according to y-position of camera
  183. int scale_;
  184. /// The amount the scale gets incremented
  185. int scaleIncrement_;
  186. // ATOMIC END
  187. };
  188. }