DebugRenderer.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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 geometry rendering component. Should be added only to the root scene node.
  60. class URHO3D_API DebugRenderer : public Component
  61. {
  62. OBJECT(DebugRenderer);
  63. public:
  64. /// Construct.
  65. DebugRenderer(Context* context);
  66. /// Destruct.
  67. virtual ~DebugRenderer();
  68. /// Register object factory.
  69. static void RegisterObject(Context* context);
  70. /// Set the camera viewpoint. Call before rendering, or before adding geometry if you want to use culling.
  71. void SetView(Camera* camera);
  72. /// Add a line.
  73. void AddLine(const Vector3& start, const Vector3& end, const Color& color, bool depthTest = true);
  74. /// Add a line with color already converted to unsigned.
  75. void AddLine(const Vector3& start, const Vector3& end, unsigned color, bool depthTest = true);
  76. /// Add a scene node represented as its coordinate axes.
  77. void AddNode(Node* node, float scale = 1.0f, bool depthTest = true);
  78. /// Add a bounding box.
  79. void AddBoundingBox(const BoundingBox& box, const Color& color, bool depthTest = true);
  80. /// Add a bounding box with transform.
  81. void AddBoundingBox(const BoundingBox& box, const Matrix3x4& transform, const Color& color, bool depthTest = true);
  82. /// Add a frustum.
  83. void AddFrustum(const Frustum& frustum, const Color& color, bool depthTest = true);
  84. /// Add a polyhedron.
  85. void AddPolyhedron(const Polyhedron& poly, const Color& color, bool depthTest = true);
  86. /// Add a sphere.
  87. void AddSphere(const Sphere& sphere, const Color& color, bool depthTest = true);
  88. /// Add a skeleton.
  89. void AddSkeleton(const Skeleton& skeleton, const Color& color, bool depthTest = true);
  90. /// Add a triangle mesh.
  91. 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);
  92. /// Update vertex buffer and render all debug lines. The viewport and rendertarget should be set before.
  93. void Render();
  94. /// Return the view transform.
  95. const Matrix3x4& GetView() const { return view_; }
  96. /// Return the projection transform.
  97. const Matrix4& GetProjection() const { return projection_; }
  98. /// Return the view frustum.
  99. const Frustum& GetFrustum() const { return frustum_; }
  100. /// Check whether a bounding box is inside the view frustum.
  101. bool IsInside(const BoundingBox& box) const;
  102. private:
  103. /// Handle end of frame. Clear debug geometry.
  104. void HandleEndFrame(StringHash eventType, VariantMap& eventData);
  105. /// Lines rendered with depth test.
  106. PODVector<DebugLine> lines_;
  107. /// Lines rendered without depth test.
  108. PODVector<DebugLine> noDepthLines_;
  109. /// View transform.
  110. Matrix3x4 view_;
  111. /// Projection transform.
  112. Matrix4 projection_;
  113. /// View frustum.
  114. Frustum frustum_;
  115. /// Vertex buffer.
  116. SharedPtr<VertexBuffer> vertexBuffer_;
  117. };
  118. }