BsViewport.h 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. #pragma once
  2. #include "BsCorePrerequisites.h"
  3. #include "BsIReflectable.h"
  4. #include "BsCoreObject.h"
  5. #include "BsColor.h"
  6. #include "BsRect2I.h"
  7. #include "BsRect2.h"
  8. #include "BsEvent.h"
  9. namespace BansheeEngine
  10. {
  11. /** @addtogroup Implementation
  12. * @{
  13. */
  14. /**
  15. * Viewport provides you with a way to render to only a part of a RenderTarget. It also allows you to set up
  16. * color/depth/stencil clear values for that specific region.
  17. */
  18. class BS_CORE_EXPORT ViewportBase
  19. {
  20. public:
  21. virtual ~ViewportBase() { }
  22. /** Gets the normalized x coordinate of the viewport, in [0, 1] range. */
  23. float getNormalizedX() const { return mNormArea.x; }
  24. /** Gets the normalized y coordinate of the viewport, in [0, 1] range. */
  25. float getNormalizedY() const { return mNormArea.y; }
  26. /** Gets the normalized width of the viewport, in [0, 1] range. */
  27. float getNormalizedWidth() const { return mNormArea.width; }
  28. /** Gets the normalized height of the viewport, in [0, 1] range. */
  29. float getNormalizedHeight() const { return mNormArea.height; }
  30. /** Gets the actual x coordinate of the viewport in pixels, in [0, RenderTargetWidth] range. */
  31. INT32 getX() const;
  32. /** Gets the actual y coordinate of the viewport in pixels, in [0, RenderTargetHeight] range. */
  33. INT32 getY() const;
  34. /** Gets the actual width coordinate of the viewport in pixels, in [0, RenderTargetWidth] range. */
  35. INT32 getWidth() const;
  36. /** Gets the actual height coordinate of the viewport in pixels, in [0, RenderTargetHeight] range. */
  37. INT32 getHeight() const;
  38. /**
  39. * Changes the area that the viewport covers.
  40. *
  41. * @note Viewport coordinates are normalized in [0, 1] range.
  42. */
  43. void setArea(float x, float y, float width, float height);
  44. /** Returns actual area of the viewport, in pixels. */
  45. Rect2I getArea() const;
  46. /**
  47. * Returns the normalized area of the viewport.
  48. *
  49. * @note Viewport coordinates are normalized in [0, 1] range.
  50. */
  51. Rect2 getNormArea() const { return mNormArea; }
  52. /**
  53. * Activates or deactivates clears for color, depth or stencil buffers. Buffers will be cleared before rendering
  54. * to this viewport is performed.
  55. */
  56. void setRequiresClear(bool colorClear, bool depthClear, bool stencilClear);
  57. /** Sets values to clear color, depth and stencil buffers to. */
  58. void setClearValues(const Color& clearColor, float clearDepth = 0.0f, UINT16 clearStencil = 0);
  59. /** Returns the color to clear the viewport color buffers to. */
  60. const Color& getClearColor() const { return mClearColor; }
  61. /** Returns the value to clear the viewport depth buffers to. */
  62. float getClearDepthValue() const { return mDepthClearValue; }
  63. /** Returns the value to clear the viewport stencil buffer to. */
  64. UINT16 getClearStencilValue() const { return mStencilClearValue; }
  65. /** Returns true if viewport requires color clear before rendering. */
  66. bool getRequiresColorClear() const { return mRequiresColorClear; }
  67. /** Returns true if viewport requires depth clear before rendering. */
  68. bool getRequiresDepthClear() const { return mRequiresDepthClear; }
  69. /** Returns true if viewport requires stencil clear before rendering. */
  70. bool getRequiresStencilClear() const { return mRequiresStencilClear; }
  71. protected:
  72. /**
  73. * Constructs a new viewport.
  74. *
  75. * @note Viewport coordinates are normalized in [0, 1] range.
  76. */
  77. ViewportBase(float x = 0.0f, float y = 0.0f, float width = 1.0f, float height = 1.0f);
  78. /** @copydoc CoreObject::markCoreDirty */
  79. virtual void _markCoreDirty() { }
  80. /** Gets the render target width. */
  81. virtual UINT32 getTargetWidth() const = 0;
  82. /** Gets the render target width. */
  83. virtual UINT32 getTargetHeight() const = 0;
  84. Rect2 mNormArea;
  85. bool mRequiresColorClear;
  86. bool mRequiresDepthClear;
  87. bool mRequiresStencilClear;
  88. Color mClearColor;
  89. float mDepthClearValue;
  90. UINT16 mStencilClearValue;
  91. static const Color DEFAULT_CLEAR_COLOR;
  92. };
  93. /** @} */
  94. /** @addtogroup RenderAPI
  95. * @{
  96. */
  97. /** @cond INTERNAL */
  98. /**
  99. * @copydoc ViewportBase
  100. */
  101. class BS_CORE_EXPORT ViewportCore : public CoreObjectCore, public ViewportBase
  102. {
  103. public:
  104. /** Returns the render target the viewport is associated with. */
  105. SPtr<RenderTargetCore> getTarget() const { return mTarget; }
  106. /** Sets the render target the viewport will be associated with. */
  107. void setTarget(const SPtr<RenderTargetCore>& target) { mTarget = target; }
  108. /** @copydoc ViewportBase::ViewportBase */
  109. static SPtr<ViewportCore> create(const SPtr<RenderTargetCore>& target, float x = 0.0f, float y = 0.0f, float width = 1.0f, float height = 1.0f);
  110. protected:
  111. friend class Viewport;
  112. /** @copydoc ViewportBase::ViewportBase */
  113. ViewportCore(const SPtr<RenderTargetCore>& target, float x = 0.0f, float y = 0.0f, float width = 1.0f, float height = 1.0f);
  114. /** @copydoc CoreObject::getTargetWidth */
  115. UINT32 getTargetWidth() const override;
  116. /** @copydoc CoreObject::getTargetHeight */
  117. UINT32 getTargetHeight() const override;
  118. /** @copydoc CoreObject::syncToCore */
  119. void syncToCore(const CoreSyncData& data) override;
  120. SPtr<RenderTargetCore> mTarget;
  121. };
  122. /** @endcond */
  123. /** @copydoc ViewportBase */
  124. class BS_CORE_EXPORT Viewport : public IReflectable, public CoreObject, public ViewportBase
  125. {
  126. public:
  127. /** Returns the render target the viewport is associated with. */
  128. RenderTargetPtr getTarget() const { return mTarget; }
  129. /** Sets the render target the viewport will be associated with. */
  130. void setTarget(const RenderTargetPtr& target);
  131. /** Retrieves a core implementation of a viewport usable only from the core thread. */
  132. SPtr<ViewportCore> getCore() const;
  133. /** @copydoc ViewportBase::ViewportBase */
  134. static ViewportPtr create(const RenderTargetPtr& target, float x = 0.0f, float y = 0.0f,
  135. float width = 1.0f, float height = 1.0f);
  136. protected:
  137. /** @copydoc ViewportBase::ViewportBase */
  138. Viewport(const RenderTargetPtr& target, float x = 0.0f, float y = 0.0f, float width = 1.0f, float height = 1.0f);
  139. /** @copydoc CoreObject::markCoreDirty */
  140. void _markCoreDirty() override;
  141. /** @copydoc CoreObject::getTargetWidth */
  142. UINT32 getTargetWidth() const override;
  143. /** @copydoc CoreObject::getTargetHeight */
  144. UINT32 getTargetHeight() const override;
  145. /** @copydoc CoreObject::syncToCore */
  146. CoreSyncData syncToCore(FrameAlloc* allocator) override;
  147. /** @copydoc CoreObject::getCoreDependencies */
  148. void getCoreDependencies(Vector<CoreObject*>& dependencies) override;
  149. /** @copydoc CoreObject::createCore */
  150. SPtr<CoreObjectCore> createCore() const override;
  151. RenderTargetPtr mTarget;
  152. /************************************************************************/
  153. /* RTTI */
  154. /************************************************************************/
  155. Viewport();
  156. /** Creates an empty viewport for serialization purposes. */
  157. static ViewportPtr createEmpty();
  158. public:
  159. friend class ViewportRTTI;
  160. static RTTITypeBase* getRTTIStatic();
  161. virtual RTTITypeBase* getRTTI() const override;
  162. };
  163. /** @} */
  164. }