BsViewport.h 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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 "Reflection/BsIReflectable.h"
  6. #include "CoreThread/BsCoreObject.h"
  7. #include "Image/BsColor.h"
  8. #include "Math/BsRect2I.h"
  9. #include "Math/BsRect2.h"
  10. #include "Utility/BsEvent.h"
  11. namespace bs
  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. ViewportBase(float x = 0.0f, float y = 0.0f, float width = 1.0f, float height = 1.0f);
  75. /**
  76. * Marks the core data as dirty. This causes the data from the sim thread object be synced with the core thread
  77. * version of the object.
  78. */
  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. /** @copydoc ViewportBase */
  98. class BS_CORE_EXPORT Viewport : public IReflectable, public CoreObject, public ViewportBase
  99. {
  100. public:
  101. /** Returns the render target the viewport is associated with. */
  102. SPtr<RenderTarget> getTarget() const { return mTarget; }
  103. /** Sets the render target the viewport will be associated with. */
  104. void setTarget(const SPtr<RenderTarget>& target);
  105. /** Retrieves a core implementation of a viewport usable only from the core thread. */
  106. SPtr<ct::Viewport> getCore() const;
  107. /**
  108. * Creates a new viewport.
  109. *
  110. * @note Viewport coordinates are normalized in [0, 1] range.
  111. */
  112. static SPtr<Viewport> create(const SPtr<RenderTarget>& target, float x = 0.0f, float y = 0.0f,
  113. float width = 1.0f, float height = 1.0f);
  114. protected:
  115. Viewport(const SPtr<RenderTarget>& target, float x = 0.0f, float y = 0.0f, float width = 1.0f, float height = 1.0f);
  116. /** @copydoc ViewportBase::_markCoreDirty */
  117. void _markCoreDirty() override;
  118. /** @copydoc ViewportBase::getTargetWidth */
  119. UINT32 getTargetWidth() const override;
  120. /** @copydoc ViewportBase::getTargetHeight */
  121. UINT32 getTargetHeight() const override;
  122. /** @copydoc CoreObject::syncToCore */
  123. CoreSyncData syncToCore(FrameAlloc* allocator) override;
  124. /** @copydoc CoreObject::getCoreDependencies */
  125. void getCoreDependencies(Vector<CoreObject*>& dependencies) override;
  126. /** @copydoc CoreObject::createCore */
  127. SPtr<ct::CoreObject> createCore() const override;
  128. SPtr<RenderTarget> mTarget;
  129. /************************************************************************/
  130. /* RTTI */
  131. /************************************************************************/
  132. Viewport();
  133. /** Creates an empty viewport for serialization purposes. */
  134. static SPtr<Viewport> createEmpty();
  135. public:
  136. friend class ViewportRTTI;
  137. static RTTITypeBase* getRTTIStatic();
  138. RTTITypeBase* getRTTI() const override;
  139. };
  140. /** @} */
  141. namespace ct
  142. {
  143. /** @addtogroup RenderAPI-Internal
  144. * @{
  145. */
  146. /** @copydoc ViewportBase */
  147. class BS_CORE_EXPORT Viewport : public CoreObject, public ViewportBase
  148. {
  149. public:
  150. /** Returns the render target the viewport is associated with. */
  151. SPtr<RenderTarget> getTarget() const { return mTarget; }
  152. /** Sets the render target the viewport will be associated with. */
  153. void setTarget(const SPtr<RenderTarget>& target) { mTarget = target; }
  154. /** @copydoc bs::Viewport::create() */
  155. static SPtr<Viewport> create(const SPtr<RenderTarget>& target, float x = 0.0f, float y = 0.0f,
  156. float width = 1.0f, float height = 1.0f);
  157. protected:
  158. friend class bs::Viewport;
  159. Viewport(const SPtr<RenderTarget>& target, float x = 0.0f, float y = 0.0f, float width = 1.0f, float height = 1.0f);
  160. /** @copydoc ViewportBase::getTargetWidth */
  161. UINT32 getTargetWidth() const override;
  162. /** @copydoc ViewportBase::getTargetHeight */
  163. UINT32 getTargetHeight() const override;
  164. /** @copydoc CoreObject::syncToCore */
  165. void syncToCore(const CoreSyncData& data) override;
  166. SPtr<RenderTarget> mTarget;
  167. };
  168. /** @} */
  169. }
  170. }