DebugRenderer.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. //
  2. // Copyright (c) 2008-2014 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 "Color.h"
  24. #include "Component.h"
  25. #include "Frustum.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()
  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 URHO3D_API DebugRenderer : public Component
  85. {
  86. OBJECT(DebugRenderer);
  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 skeleton.
  117. void AddSkeleton(const Skeleton& skeleton, const Color& color, bool depthTest = true);
  118. /// Add a triangle mesh.
  119. void AddTriangleMesh(const void* vertexData, unsigned vertexSize, const void* indexData, unsigned indexSize, unsigned indexStart, unsigned indexCount, const Matrix3x4& transform, 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 the view transform.
  123. const Matrix3x4& GetView() const { return view_; }
  124. /// Return the projection transform.
  125. const Matrix4& GetProjection() const { return projection_; }
  126. /// Return the view frustum.
  127. const Frustum& GetFrustum() const { return frustum_; }
  128. /// Check whether a bounding box is inside the view frustum.
  129. bool IsInside(const BoundingBox& box) const;
  130. private:
  131. /// Handle end of frame. Clear debug geometry.
  132. void HandleEndFrame(StringHash eventType, VariantMap& eventData);
  133. /// Lines rendered with depth test.
  134. PODVector<DebugLine> lines_;
  135. /// Lines rendered without depth test.
  136. PODVector<DebugLine> noDepthLines_;
  137. /// Triangles rendered with depth test.
  138. PODVector<DebugTriangle> triangles_;
  139. /// Triangles rendered without depth test.
  140. PODVector<DebugTriangle> noDepthTriangles_;
  141. /// View transform.
  142. Matrix3x4 view_;
  143. /// Projection transform.
  144. Matrix4 projection_;
  145. /// View frustum.
  146. Frustum frustum_;
  147. /// Vertex buffer.
  148. SharedPtr<VertexBuffer> vertexBuffer_;
  149. };
  150. }