Viewport.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. //
  2. // Copyright (c) 2008-2020 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 "../Core/Object.h"
  24. #include "../Container/Ptr.h"
  25. #include "../Math/Ray.h"
  26. #include "../Math/Rect.h"
  27. #include "../Math/Vector2.h"
  28. namespace Urho3D
  29. {
  30. class Camera;
  31. class RenderPath;
  32. class Scene;
  33. class XMLFile;
  34. class View;
  35. /// %Viewport definition either for a render surface or the backbuffer.
  36. class URHO3D_API Viewport : public Object
  37. {
  38. URHO3D_OBJECT(Viewport, Object);
  39. public:
  40. /// Construct with defaults.
  41. explicit Viewport(Context* context);
  42. /// Construct with a full rectangle.
  43. Viewport(Context* context, Scene* scene, Camera* camera, RenderPath* renderPath = nullptr);
  44. /// Construct with a specified rectangle.
  45. Viewport(Context* context, Scene* scene, Camera* camera, const IntRect& rect, RenderPath* renderPath = nullptr);
  46. /// Destruct.
  47. ~Viewport() override;
  48. /// Set scene.
  49. /// @property
  50. void SetScene(Scene* scene);
  51. /// Set viewport camera.
  52. /// @property
  53. void SetCamera(Camera* camera);
  54. /// Set view rectangle. A zero rectangle (0 0 0 0) means to use the rendertarget's full dimensions.
  55. /// @property
  56. void SetRect(const IntRect& rect);
  57. /// Set rendering path.
  58. /// @property
  59. void SetRenderPath(RenderPath* renderPath);
  60. /// Set rendering path from an XML file.
  61. bool SetRenderPath(XMLFile* file);
  62. /// Set whether to render debug geometry. Default true.
  63. /// @property
  64. void SetDrawDebug(bool enable);
  65. /// Set separate camera to use for culling. Sharing a culling camera between several viewports allows to prepare the view only once, saving in CPU use. The culling camera's frustum should cover all the viewport cameras' frusta or else objects may be missing from the rendered view.
  66. /// @property
  67. void SetCullCamera(Camera* camera);
  68. /// Return scene.
  69. /// @property
  70. Scene* GetScene() const;
  71. /// Return viewport camera.
  72. /// @property
  73. Camera* GetCamera() const;
  74. /// Return the internal rendering structure. May be null if the viewport has not been rendered yet.
  75. View* GetView() const;
  76. /// Return view rectangle. A zero rectangle (0 0 0 0) means to use the rendertarget's full dimensions. In this case you could fetch the actual view rectangle from View object, though it will be valid only after the first frame.
  77. /// @property
  78. const IntRect& GetRect() const { return rect_; }
  79. /// Return rendering path.
  80. /// @property
  81. RenderPath* GetRenderPath() const;
  82. /// Return whether to draw debug geometry.
  83. /// @property
  84. bool GetDrawDebug() const { return drawDebug_; }
  85. /// Return the culling camera. If null, the viewport camera will be used for culling (normal case).
  86. /// @property
  87. Camera* GetCullCamera() const;
  88. /// Return ray corresponding to normalized screen coordinates.
  89. Ray GetScreenRay(int x, int y) const;
  90. /// Convert a world space point to normalized screen coordinates.
  91. IntVector2 WorldToScreenPoint(const Vector3& worldPos) const;
  92. /// Convert screen coordinates and depth to a world space point.
  93. Vector3 ScreenToWorldPoint(int x, int y, float depth) const;
  94. /// Allocate the view structure. Called by Renderer.
  95. void AllocateView();
  96. private:
  97. /// Scene pointer.
  98. WeakPtr<Scene> scene_;
  99. /// Camera pointer.
  100. WeakPtr<Camera> camera_;
  101. /// Culling camera pointer.
  102. WeakPtr<Camera> cullCamera_;
  103. /// Viewport rectangle.
  104. IntRect rect_;
  105. /// Rendering path.
  106. SharedPtr<RenderPath> renderPath_;
  107. /// Internal rendering structure.
  108. SharedPtr<View> view_;
  109. /// Debug draw flag.
  110. bool drawDebug_;
  111. };
  112. }