DebugRenderer.h 5.0 KB

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