BsViewport.h 7.6 KB

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