Viewport.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. // Copyright (c) 2008-2023 the Urho3D project
  2. // License: MIT
  3. #pragma once
  4. #include "../Core/Object.h"
  5. #include "../Container/Ptr.h"
  6. #include "../Math/Ray.h"
  7. #include "../Math/Rect.h"
  8. #include "../Math/Vector2.h"
  9. namespace Urho3D
  10. {
  11. class Camera;
  12. class RenderPath;
  13. class Scene;
  14. class XMLFile;
  15. class View;
  16. /// %Viewport definition either for a render surface or the backbuffer.
  17. class URHO3D_API Viewport : public Object
  18. {
  19. URHO3D_OBJECT(Viewport, Object);
  20. public:
  21. /// Construct with defaults.
  22. explicit Viewport(Context* context);
  23. /// Construct with a full rectangle.
  24. Viewport(Context* context, Scene* scene, Camera* camera, RenderPath* renderPath = nullptr);
  25. /// Construct with a specified rectangle.
  26. Viewport(Context* context, Scene* scene, Camera* camera, const IntRect& rect, RenderPath* renderPath = nullptr);
  27. /// Destruct.
  28. ~Viewport() override;
  29. /// Set scene.
  30. /// @property
  31. void SetScene(Scene* scene);
  32. /// Set viewport camera.
  33. /// @property
  34. void SetCamera(Camera* camera);
  35. /// Set view rectangle. A zero rectangle (0 0 0 0) means to use the rendertarget's full dimensions.
  36. /// @property
  37. void SetRect(const IntRect& rect);
  38. /// Set rendering path.
  39. /// @property
  40. void SetRenderPath(RenderPath* renderPath);
  41. /// Set rendering path from an XML file.
  42. bool SetRenderPath(XMLFile* file);
  43. /// Set whether to render debug geometry. Default true.
  44. /// @property
  45. void SetDrawDebug(bool enable);
  46. /// 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.
  47. /// @property
  48. void SetCullCamera(Camera* camera);
  49. /// Return scene.
  50. /// @property
  51. Scene* GetScene() const;
  52. /// Return viewport camera.
  53. /// @property
  54. Camera* GetCamera() const;
  55. /// Return the internal rendering structure. May be null if the viewport has not been rendered yet.
  56. View* GetView() const;
  57. /// 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.
  58. /// @property
  59. const IntRect& GetRect() const { return rect_; }
  60. /// Return rendering path.
  61. /// @property
  62. RenderPath* GetRenderPath() const;
  63. /// Return whether to draw debug geometry.
  64. /// @property
  65. bool GetDrawDebug() const { return drawDebug_; }
  66. /// Return the culling camera. If null, the viewport camera will be used for culling (normal case).
  67. /// @property
  68. Camera* GetCullCamera() const;
  69. /// Return ray corresponding to normalized screen coordinates.
  70. Ray GetScreenRay(int x, int y) const;
  71. /// Convert a world space point to normalized screen coordinates.
  72. IntVector2 WorldToScreenPoint(const Vector3& worldPos) const;
  73. /// Convert screen coordinates and depth to a world space point.
  74. Vector3 ScreenToWorldPoint(int x, int y, float depth) const;
  75. /// Allocate the view structure. Called by Renderer.
  76. void AllocateView();
  77. private:
  78. /// Scene pointer.
  79. WeakPtr<Scene> scene_;
  80. /// Camera pointer.
  81. WeakPtr<Camera> camera_;
  82. /// Culling camera pointer.
  83. WeakPtr<Camera> cullCamera_;
  84. /// Viewport rectangle.
  85. IntRect rect_;
  86. /// Rendering path.
  87. SharedPtr<RenderPath> renderPath_;
  88. /// Internal rendering structure.
  89. SharedPtr<View> view_;
  90. /// Debug draw flag.
  91. bool drawDebug_;
  92. };
  93. }