Viewport.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. //
  2. // Copyright (c) 2008-2017 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 Atomic
  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 ATOMIC_API Viewport : public Object
  37. {
  38. ATOMIC_OBJECT(Viewport, Object);
  39. public:
  40. /// Construct with defaults.
  41. Viewport(Context* context);
  42. /// Construct with a full rectangle.
  43. Viewport(Context* context, Scene* scene, Camera* camera, RenderPath* renderPath = 0);
  44. /// Construct with a specified rectangle.
  45. Viewport(Context* context, Scene* scene, Camera* camera, const IntRect& rect, RenderPath* renderPath = 0);
  46. /// Destruct.
  47. ~Viewport();
  48. /// Set scene.
  49. void SetScene(Scene* scene);
  50. /// Set viewport camera.
  51. void SetCamera(Camera* camera);
  52. /// Set view rectangle. A zero rectangle (0 0 0 0) means to use the rendertarget's full dimensions.
  53. void SetRect(const IntRect& rect);
  54. /// Set rendering path.
  55. void SetRenderPath(RenderPath* path);
  56. /// Set rendering path from an XML file.
  57. void SetRenderPath(XMLFile* file);
  58. /// Set whether to render debug geometry. Default true.
  59. void SetDrawDebug(bool enable);
  60. /// 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.
  61. void SetCullCamera(Camera* camera);
  62. /// Return scene.
  63. Scene* GetScene() const;
  64. /// Return viewport camera.
  65. Camera* GetCamera() const;
  66. /// Return the internal rendering structure. May be null if the viewport has not been rendered yet.
  67. View* GetView() const;
  68. /// 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.
  69. const IntRect& GetRect() const { return rect_; }
  70. /// Return rendering path.
  71. RenderPath* GetRenderPath() const;
  72. /// Return whether to draw debug geometry.
  73. bool GetDrawDebug() const { return drawDebug_; }
  74. /// Return the culling camera. If null, the viewport camera will be used for culling (normal case.)
  75. Camera* GetCullCamera() const;
  76. /// Return ray corresponding to normalized screen coordinates.
  77. Ray GetScreenRay(int x, int y) const;
  78. /// Convert a world space point to normalized screen coordinates.
  79. IntVector2 WorldToScreenPoint(const Vector3& worldPos) const;
  80. /// Convert screen coordinates and depth to a world space point.
  81. Vector3 ScreenToWorldPoint(int x, int y, float depth) const;
  82. /// Allocate the view structure. Called by Renderer.
  83. void AllocateView();
  84. private:
  85. /// Scene pointer.
  86. WeakPtr<Scene> scene_;
  87. /// Camera pointer.
  88. WeakPtr<Camera> camera_;
  89. /// Culling camera pointer.
  90. WeakPtr<Camera> cullCamera_;
  91. /// Viewport rectangle.
  92. IntRect rect_;
  93. /// Rendering path.
  94. SharedPtr<RenderPath> renderPath_;
  95. /// Internal rendering structure.
  96. SharedPtr<View> view_;
  97. /// Debug draw flag.
  98. bool drawDebug_;
  99. };
  100. }