DebugRenderer.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. class BoundingBox;
  29. class Camera;
  30. class Frustum;
  31. class Drawable;
  32. class Light;
  33. class Matrix4x3;
  34. class Renderer;
  35. class Skeleton;
  36. /// Debug rendering line
  37. struct DebugLine
  38. {
  39. /// Construct as undefined
  40. DebugLine()
  41. {
  42. }
  43. /// Construct with start and end positions and color
  44. DebugLine(const Vector3& start, const Vector3& end, unsigned color) :
  45. start_(start),
  46. end_(end),
  47. color_(color)
  48. {
  49. }
  50. /// Start position
  51. Vector3 start_;
  52. /// End position
  53. Vector3 end_;
  54. /// Color
  55. unsigned color_;
  56. };
  57. /// Debug geometry graphics
  58. class DebugRenderer : public Component
  59. {
  60. OBJECT(DebugRenderer);
  61. public:
  62. /// Construct
  63. DebugRenderer(Context* context);
  64. /// Destruct
  65. virtual ~DebugRenderer();
  66. /// Register object factory
  67. static void RegisterObject(Context* context);
  68. /// Set the camera viewpoint. Call before rendering, or before adding geometry if you want to use culling
  69. void SetView(Camera* camera);
  70. /// Add a line
  71. void AddLine(const Vector3& start, const Vector3& end, const Color& color, bool depthTest = true);
  72. /// Add a bounding box
  73. void AddBoundingBox(const BoundingBox& box, const Color& color, bool depthTest = true);
  74. /// Add a bounding box with transform
  75. void AddBoundingBox(const BoundingBox& box, const Matrix4x3& transform, const Color& color, bool depthTest = true);
  76. /// Add a frustum
  77. void AddFrustum(const Frustum& frustum, const Color& color, bool depthTest = true);
  78. /// Add a skeleton
  79. void AddSkeleton(const Skeleton& skeleton, const Color& color, bool depthTest = true);
  80. /// Render all debug lines. The viewport and rendertarget should be set before
  81. void Render();
  82. /// Return the view transform
  83. const Matrix4x3& GetView() const { return view_; }
  84. /// Return the projection transform
  85. const Matrix4& GetProjection() const { return projection_; }
  86. /// Return the view frustum
  87. const Frustum& GetFrustum() const { return frustum_; }
  88. /// Check whether a bounding box is inside the view frustum
  89. bool IsInside(const BoundingBox& box) const;
  90. private:
  91. /// Handle end of frame. Clear debug geometry
  92. void HandleEndFrame(StringHash eventType, VariantMap& eventData);
  93. /// Lines rendered with depth test
  94. Vector<DebugLine> lines_;
  95. /// Lines rendered without depth test
  96. Vector<DebugLine> noDepthLines_;
  97. /// View transform
  98. Matrix4x3 view_;
  99. /// Projection transform
  100. Matrix4 projection_;
  101. /// View frustum
  102. Frustum frustum_;
  103. };