DebugRenderer.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2011 Lasse Öörni
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. // THE SOFTWARE.
  22. //
  23. #pragma once
  24. #include "Color.h"
  25. #include "Component.h"
  26. #include "Frustum.h"
  27. class BoundingBox;
  28. class Camera;
  29. class Polyhedron;
  30. class Drawable;
  31. class Light;
  32. class Matrix3x4;
  33. class Renderer;
  34. class Skeleton;
  35. class Sphere;
  36. class VertexBuffer;
  37. /// Debug rendering line.
  38. struct DebugLine
  39. {
  40. /// Construct undefined.
  41. DebugLine()
  42. {
  43. }
  44. /// Construct with start and end positions and color.
  45. DebugLine(const Vector3& start, const Vector3& end, unsigned color) :
  46. start_(start),
  47. end_(end),
  48. color_(color)
  49. {
  50. }
  51. /// Start position.
  52. Vector3 start_;
  53. /// End position.
  54. Vector3 end_;
  55. /// Color.
  56. unsigned color_;
  57. };
  58. /// Debug geometry rendering component. Should be added only to the root scene node.
  59. class DebugRenderer : public Component
  60. {
  61. OBJECT(DebugRenderer);
  62. public:
  63. /// Construct.
  64. DebugRenderer(Context* context);
  65. /// Destruct.
  66. virtual ~DebugRenderer();
  67. /// Register object factory.
  68. static void RegisterObject(Context* context);
  69. /// %Set the camera viewpoint. Call before rendering, or before adding geometry if you want to use culling.
  70. void SetView(Camera* camera);
  71. /// Add a line.
  72. void AddLine(const Vector3& start, const Vector3& end, const Color& color, bool depthTest = true);
  73. /// Add a line with color already converted to unsigned.
  74. void AddLine(const Vector3& start, const Vector3& end, unsigned color, bool depthTest = true);
  75. /// Add a scene node represented as its local axes.
  76. void AddNode(Node* node, bool depthTest = true);
  77. /// Add a bounding box.
  78. void AddBoundingBox(const BoundingBox& box, const Color& color, bool depthTest = true);
  79. /// Add a bounding box with transform.
  80. void AddBoundingBox(const BoundingBox& box, const Matrix3x4& transform, const Color& color, bool depthTest = true);
  81. /// Add a frustum.
  82. void AddFrustum(const Frustum& frustum, const Color& color, bool depthTest = true);
  83. /// Add a polyhedron.
  84. void AddPolyhedron(const Polyhedron& poly, const Color& color, bool depthTest = true);
  85. /// Add a sphere.
  86. void AddSphere(const Sphere& sphere, const Color& color, bool depthTest = true);
  87. /// Add a skeleton.
  88. void AddSkeleton(const Skeleton& skeleton, const Color& color, bool depthTest = true);
  89. /// Add a triangle mesh.
  90. 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);
  91. /// Update vertex buffer and render all debug lines. The viewport and render target should be set before.
  92. void Render();
  93. /// Return the view transform.
  94. const Matrix3x4& GetView() const { return view_; }
  95. /// Return the projection transform.
  96. const Matrix4& GetProjection() const { return projection_; }
  97. /// Return the view frustum.
  98. const Frustum& GetFrustum() const { return frustum_; }
  99. /// Check whether a bounding box is inside the view frustum.
  100. bool IsInside(const BoundingBox& box) const;
  101. private:
  102. /// Handle end of frame. Clear debug geometry.
  103. void HandleEndFrame(StringHash eventType, VariantMap& eventData);
  104. /// Lines rendered with depth test.
  105. PODVector<DebugLine> lines_;
  106. /// Lines rendered without depth test.
  107. PODVector<DebugLine> noDepthLines_;
  108. /// View transform.
  109. Matrix3x4 view_;
  110. /// Projection transform.
  111. Matrix4 projection_;
  112. /// View frustum.
  113. Frustum frustum_;
  114. /// Vertex buffer.
  115. SharedPtr<VertexBuffer> vertexBuffer_;
  116. };