BsViewport.h 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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. /**
  12. * @brief Viewport provides you with a way to render to only a part of a
  13. * RenderTarget. It also allows you to set up color/depth/stencil
  14. * clear values for that specific region.
  15. */
  16. class BS_CORE_EXPORT ViewportBase
  17. {
  18. public:
  19. virtual ~ViewportBase() { }
  20. /**
  21. * @brief Gets the normalized x coordinate of the viewport, in [0, 1] range.
  22. */
  23. float getNormalizedX() const { return mNormArea.x; }
  24. /**
  25. * @brief Gets the normalized y coordinate of the viewport, in [0, 1] range.
  26. */
  27. float getNormalizedY() const { return mNormArea.y; }
  28. /**
  29. * @brief Gets the normalized width of the viewport, in [0, 1] range.
  30. */
  31. float getNormalizedWidth() const { return mNormArea.width; }
  32. /**
  33. * @brief Gets the normalized height of the viewport, in [0, 1] range.
  34. */
  35. float getNormalizedHeight() const { return mNormArea.height; }
  36. /**
  37. * @brief Gets the actual x coordinate of the viewport in pixels, in [0, RenderTargetWidth] range.
  38. */
  39. INT32 getX() const;
  40. /**
  41. * @brief Gets the actual y coordinate of the viewport in pixels, in [0, RenderTargetHeight] range.
  42. */
  43. INT32 getY() const;
  44. /**
  45. * @brief Gets the actual width coordinate of the viewport in pixels, in [0, RenderTargetWidth] range.
  46. */
  47. INT32 getWidth() const;
  48. /**
  49. * @brief Gets the actual height coordinate of the viewport in pixels, in [0, RenderTargetHeight] range.
  50. */
  51. INT32 getHeight() const;
  52. /**
  53. * @brief Changes the area that the viewport covers.
  54. *
  55. * @note Viewport coordinates are normalized in [0, 1] range.
  56. */
  57. void setArea(float x, float y, float width, float height);
  58. /**
  59. * @brief Returns actual area of the viewport, in pixels.
  60. */
  61. Rect2I getArea() const;
  62. /**
  63. * @brief Returns the normalized area of the viewport.
  64. *
  65. * @note Viewport coordinates are normalized in [0, 1] range.
  66. */
  67. Rect2 getNormArea() const { return mNormArea; }
  68. /**
  69. * @brief Activates or deactivates clears for color, depth or stencil buffers.
  70. * Buffers will be cleared before rendering to this viewport is performed.
  71. */
  72. void setRequiresClear(bool colorClear, bool depthClear, bool stencilClear);
  73. /**
  74. * @brief Sets values to clear color, depth and stencil buffers to.
  75. */
  76. void setClearValues(const Color& clearColor, float clearDepth = 0.0f, UINT16 clearStencil = 0);
  77. /**
  78. * @brief Returns the color to clear the viewport color buffers to.
  79. */
  80. const Color& getClearColor() const { return mClearColor; }
  81. /**
  82. * @brief Returns the value to clear the viewport depth buffers to.
  83. */
  84. float getClearDepthValue() const { return mDepthClearValue; }
  85. /**
  86. * @brief Returns the value to clear the viewport stencil buffer to.
  87. */
  88. UINT16 getClearStencilValue() const { return mStencilClearValue; }
  89. /**
  90. * @brief Returns true if viewport requires color clear before rendering.
  91. */
  92. bool getRequiresColorClear() const { return mRequiresColorClear; }
  93. /**
  94. * @brief Returns true if viewport requires depth clear before rendering.
  95. */
  96. bool getRequiresDepthClear() const { return mRequiresDepthClear; }
  97. /**
  98. * @brief Returns true if viewport requires stencil clear before rendering.
  99. */
  100. bool getRequiresStencilClear() const { return mRequiresStencilClear; }
  101. protected:
  102. /**
  103. * @brief Constructs a new viewport.
  104. *
  105. * @note Viewport coordinates are normalized in [0, 1] range.
  106. */
  107. ViewportBase(float x = 0.0f, float y = 0.0f, float width = 1.0f, float height = 1.0f);
  108. /**
  109. * @copydoc CoreObject::markCoreDirty
  110. */
  111. virtual void _markCoreDirty() { }
  112. /**
  113. * @brief Gets the render target width.
  114. */
  115. virtual UINT32 getTargetWidth() const = 0;
  116. /**
  117. * @brief Gets the render target width.
  118. */
  119. virtual UINT32 getTargetHeight() const = 0;
  120. Rect2 mNormArea;
  121. bool mRequiresColorClear;
  122. bool mRequiresDepthClear;
  123. bool mRequiresStencilClear;
  124. Color mClearColor;
  125. float mDepthClearValue;
  126. UINT16 mStencilClearValue;
  127. static const Color DEFAULT_CLEAR_COLOR;
  128. };
  129. /**
  130. * @copydoc ViewportBase
  131. */
  132. class BS_CORE_EXPORT ViewportCore : public CoreObjectCore, public ViewportBase
  133. {
  134. public:
  135. /**
  136. * @brief Returns the render target the viewport is associated with.
  137. */
  138. SPtr<RenderTargetCore> getTarget() const { return mTarget; }
  139. /**
  140. * @brief Sets the render target the viewport will be associated with.
  141. */
  142. void setTarget(const SPtr<RenderTargetCore>& target) { mTarget = target; }
  143. /**
  144. * @copydoc ViewportBase::ViewportBase
  145. */
  146. static SPtr<ViewportCore> create(const SPtr<RenderTargetCore>& target, float x = 0.0f, float y = 0.0f, float width = 1.0f, float height = 1.0f);
  147. protected:
  148. friend class Viewport;
  149. /**
  150. * @copydoc ViewportBase::ViewportBase
  151. */
  152. ViewportCore(const SPtr<RenderTargetCore>& target, float x = 0.0f, float y = 0.0f, float width = 1.0f, float height = 1.0f);
  153. /**
  154. * @copydoc CoreObject::getTargetWidth
  155. */
  156. UINT32 getTargetWidth() const override;
  157. /**
  158. * @copydoc CoreObject::getTargetHeight
  159. */
  160. UINT32 getTargetHeight() const override;
  161. /**
  162. * @copydoc CoreObject::syncToCore
  163. */
  164. void syncToCore(const CoreSyncData& data) override;
  165. SPtr<RenderTargetCore> mTarget;
  166. };
  167. /**
  168. * @copydoc ViewportBase
  169. */
  170. class BS_CORE_EXPORT Viewport : public IReflectable, public CoreObject, public ViewportBase
  171. {
  172. public:
  173. /**
  174. * @brief Returns the render target the viewport is associated with.
  175. */
  176. RenderTargetPtr getTarget() const { return mTarget; }
  177. /**
  178. * @brief Sets the render target the viewport will be associated with.
  179. */
  180. void setTarget(const RenderTargetPtr& target) { mTarget = target; _markCoreDirty(); }
  181. /**
  182. * @brief Retrieves a core implementation of a viewport usable only from the
  183. * core thread.
  184. */
  185. SPtr<ViewportCore> getCore() const;
  186. /**
  187. * @copydoc ViewportBase::ViewportBase
  188. */
  189. static ViewportPtr create(const RenderTargetPtr& target, float x = 0.0f, float y = 0.0f,
  190. float width = 1.0f, float height = 1.0f);
  191. protected:
  192. /**
  193. * @copydoc ViewportBase::ViewportBase
  194. */
  195. Viewport(const RenderTargetPtr& target, float x = 0.0f, float y = 0.0f, float width = 1.0f, float height = 1.0f);
  196. /**
  197. * @copydoc CoreObject::markCoreDirty
  198. */
  199. void _markCoreDirty() override;
  200. /**
  201. * @copydoc CoreObject::getTargetWidth
  202. */
  203. UINT32 getTargetWidth() const override;
  204. /**
  205. * @copydoc CoreObject::getTargetHeight
  206. */
  207. UINT32 getTargetHeight() const override;
  208. /**
  209. * @copydoc CoreObject::syncToCore
  210. */
  211. CoreSyncData syncToCore(FrameAlloc* allocator) override;
  212. /**
  213. * @copydoc CoreObject::getCoreDependencies
  214. */
  215. void getCoreDependencies(FrameVector<SPtr<CoreObject>>& dependencies) override;
  216. /**
  217. * @copydoc CoreObject::createCore
  218. */
  219. SPtr<CoreObjectCore> createCore() const override;
  220. RenderTargetPtr mTarget;
  221. /************************************************************************/
  222. /* RTTI */
  223. /************************************************************************/
  224. Viewport();
  225. /**
  226. * @brief Creates an empty viewport for serialization purposes.
  227. */
  228. static ViewportPtr createEmpty();
  229. public:
  230. friend class ViewportRTTI;
  231. static RTTITypeBase* getRTTIStatic();
  232. virtual RTTITypeBase* getRTTI() const override;
  233. };
  234. }