BsViewport.h 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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. ViewportBase(float x = 0.0f, float y = 0.0f, float width = 1.0f, float height = 1.0f);
  75. /** @copydoc CoreObject::markCoreDirty */
  76. virtual void _markCoreDirty() { }
  77. /** Gets the render target width. */
  78. virtual UINT32 getTargetWidth() const = 0;
  79. /** Gets the render target width. */
  80. virtual UINT32 getTargetHeight() const = 0;
  81. Rect2 mNormArea;
  82. bool mRequiresColorClear;
  83. bool mRequiresDepthClear;
  84. bool mRequiresStencilClear;
  85. Color mClearColor;
  86. float mDepthClearValue;
  87. UINT16 mStencilClearValue;
  88. static const Color DEFAULT_CLEAR_COLOR;
  89. };
  90. /** @} */
  91. /** @addtogroup RenderAPI
  92. * @{
  93. */
  94. /** @copydoc ViewportBase */
  95. class BS_CORE_EXPORT Viewport : public IReflectable, public CoreObject, public ViewportBase
  96. {
  97. public:
  98. /** Returns the render target the viewport is associated with. */
  99. RenderTargetPtr getTarget() const { return mTarget; }
  100. /** Sets the render target the viewport will be associated with. */
  101. void setTarget(const RenderTargetPtr& target);
  102. /** Retrieves a core implementation of a viewport usable only from the core thread. */
  103. SPtr<ViewportCore> getCore() const;
  104. /**
  105. * Creates a new viewport.
  106. *
  107. * @note Viewport coordinates are normalized in [0, 1] range.
  108. */
  109. static ViewportPtr create(const RenderTargetPtr& target, float x = 0.0f, float y = 0.0f,
  110. float width = 1.0f, float height = 1.0f);
  111. protected:
  112. Viewport(const RenderTargetPtr& target, float x = 0.0f, float y = 0.0f, float width = 1.0f, float height = 1.0f);
  113. /** @copydoc CoreObject::markCoreDirty */
  114. void _markCoreDirty() override;
  115. /** @copydoc CoreObject::getTargetWidth */
  116. UINT32 getTargetWidth() const override;
  117. /** @copydoc CoreObject::getTargetHeight */
  118. UINT32 getTargetHeight() const override;
  119. /** @copydoc CoreObject::syncToCore */
  120. CoreSyncData syncToCore(FrameAlloc* allocator) override;
  121. /** @copydoc CoreObject::getCoreDependencies */
  122. void getCoreDependencies(Vector<CoreObject*>& dependencies) override;
  123. /** @copydoc CoreObject::createCore */
  124. SPtr<CoreObjectCore> createCore() const override;
  125. RenderTargetPtr mTarget;
  126. /************************************************************************/
  127. /* RTTI */
  128. /************************************************************************/
  129. Viewport();
  130. /** Creates an empty viewport for serialization purposes. */
  131. static ViewportPtr createEmpty();
  132. public:
  133. friend class ViewportRTTI;
  134. static RTTITypeBase* getRTTIStatic();
  135. virtual RTTITypeBase* getRTTI() const override;
  136. };
  137. /** @} */
  138. /** @addtogroup RenderAPI-Internal
  139. * @{
  140. */
  141. /** @copydoc ViewportBase */
  142. class BS_CORE_EXPORT ViewportCore : public CoreObjectCore, public ViewportBase
  143. {
  144. public:
  145. /** Returns the render target the viewport is associated with. */
  146. SPtr<RenderTargetCore> getTarget() const { return mTarget; }
  147. /** Sets the render target the viewport will be associated with. */
  148. void setTarget(const SPtr<RenderTargetCore>& target) { mTarget = target; }
  149. /** @copydoc Viewport::create() */
  150. static SPtr<ViewportCore> create(const SPtr<RenderTargetCore>& target, float x = 0.0f, float y = 0.0f,
  151. float width = 1.0f, float height = 1.0f);
  152. protected:
  153. friend class Viewport;
  154. ViewportCore(const SPtr<RenderTargetCore>& target, float x = 0.0f, float y = 0.0f, float width = 1.0f, float height = 1.0f);
  155. /** @copydoc CoreObject::getTargetWidth */
  156. UINT32 getTargetWidth() const override;
  157. /** @copydoc CoreObject::getTargetHeight */
  158. UINT32 getTargetHeight() const override;
  159. /** @copydoc CoreObject::syncToCore */
  160. void syncToCore(const CoreSyncData& data) override;
  161. SPtr<RenderTargetCore> mTarget;
  162. };
  163. /** @} */
  164. }