DebugRenderer.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. #include "SharedPtr.h"
  28. #include <vector>
  29. class BoundingBox;
  30. class Camera;
  31. class Frustum;
  32. class Drawable;
  33. class Light;
  34. class Matrix4x3;
  35. class Renderer;
  36. class Skeleton;
  37. /// Debug rendering line
  38. struct DebugLine
  39. {
  40. /// Construct as 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 graphics
  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 bounding box
  74. void AddBoundingBox(const BoundingBox& box, const Color& color, bool depthTest = true);
  75. /// Add a bounding box with transform
  76. void AddBoundingBox(const BoundingBox& box, const Matrix4x3& transform, const Color& color, bool depthTest = true);
  77. /// Add a frustum
  78. void AddFrustum(const Frustum& frustum, const Color& color, bool depthTest = true);
  79. /// Add a skeleton
  80. void AddSkeleton(const Skeleton& skeleton, const Color& color, bool depthTest = true);
  81. /// Render all debug lines. The viewport and rendertarget should be set before
  82. void Render();
  83. /// Return the view transform
  84. const Matrix4x3& GetView() const { return view_; }
  85. /// Return the projection transform
  86. const Matrix4& GetProjection() const { return projection_; }
  87. /// Return the view frustum
  88. const Frustum& GetFrustum() const { return frustum_; }
  89. /// Check whether a bounding box is inside the view frustum
  90. bool IsInside(const BoundingBox& box) const;
  91. private:
  92. /// Handle end of frame. Clear debug geometry
  93. void HandleEndFrame(StringHash eventType, VariantMap& eventData);
  94. /// Lines rendered with depth test
  95. std::vector<DebugLine> lines_;
  96. /// Lines rendered without depth test
  97. std::vector<DebugLine> noDepthLines_;
  98. /// View transform
  99. Matrix4x3 view_;
  100. /// Projection transform
  101. Matrix4 projection_;
  102. /// View frustum
  103. Frustum frustum_;
  104. };