BsViewport.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. #pragma once
  2. #include "BsCorePrerequisites.h"
  3. #include "BsIReflectable.h"
  4. #include "BsColor.h"
  5. #include "BsRect2I.h"
  6. #include "BsRect2.h"
  7. #include "BsEvent.h"
  8. namespace BansheeEngine
  9. {
  10. /**
  11. * @brief Viewport provides you with a way to render to only a part of a
  12. * RenderTarget. It also allows you to set up color/depth/stencil
  13. * clear values for that specific region.
  14. *
  15. * @note Thread safe unless noted otherwise.
  16. */
  17. class BS_CORE_EXPORT Viewport : public IReflectable
  18. {
  19. public:
  20. Viewport();
  21. /**
  22. * @brief Constructs a new viewport.
  23. *
  24. * @note Viewport coordinates are normalized in [0, 1] range.
  25. */
  26. Viewport(const RenderTargetPtr& target, float x = 0.0f, float y = 0.0f, float width = 1.0f, float height = 1.0f);
  27. virtual ~Viewport();
  28. /**
  29. * @brief Returns the render target the viewport is associated with.
  30. */
  31. RenderTargetPtr getTarget() const { return mTarget; }
  32. /**
  33. * @brief Sets the render target the viewport will be associated with.
  34. */
  35. void setTarget(const RenderTargetPtr& target) { mTarget = target; markCoreDirty(); }
  36. /**
  37. * @brief Gets the normalized x coordinate of the viewport, in [0, 1] range.
  38. */
  39. float getNormalizedX() const { return mNormArea.x; }
  40. /**
  41. * @brief Gets the normalized y coordinate of the viewport, in [0, 1] range.
  42. */
  43. float getNormalizedY() const { return mNormArea.y; }
  44. /**
  45. * @brief Gets the normalized width of the viewport, in [0, 1] range.
  46. */
  47. float getNormalizedWidth() const { return mNormArea.width; }
  48. /**
  49. * @brief Gets the normalized height of the viewport, in [0, 1] range.
  50. */
  51. float getNormalizedHeight() const { return mNormArea.height; }
  52. /**
  53. * @brief Gets the actual x coordinate of the viewport in pixels, in [0, RenderTargetWidth] range.
  54. *
  55. * @note Sim thread only.
  56. */
  57. INT32 getX() const;
  58. /**
  59. * @brief Gets the actual y coordinate of the viewport in pixels, in [0, RenderTargetHeight] range.
  60. *
  61. * @note Sim thread only.
  62. */
  63. INT32 getY() const;
  64. /**
  65. * @brief Gets the actual width coordinate of the viewport in pixels, in [0, RenderTargetWidth] range.
  66. *
  67. * @note Sim thread only.
  68. */
  69. INT32 getWidth() const;
  70. /**
  71. * @brief Gets the actual height coordinate of the viewport in pixels, in [0, RenderTargetHeight] range.
  72. *
  73. * @note Sim thread only.
  74. */
  75. INT32 getHeight() const;
  76. /**
  77. * @brief Changes the area that the viewport covers.
  78. *
  79. * @note Viewport coordinates are normalized in [0, 1] range.
  80. */
  81. void setArea(float x, float y, float width, float height);
  82. /**
  83. * @brief Returns actual area of the viewport, in pixels.
  84. *
  85. * @note Sim thread only.
  86. */
  87. Rect2I getArea() const;
  88. /**
  89. * @brief Returns the normalized area of the viewport.
  90. *
  91. * @note Viewport coordinates are normalized in [0, 1] range.
  92. */
  93. Rect2 getNormArea() const { return mNormArea; }
  94. /**
  95. * @brief Activates or deactivates clears for color, depth or stencil buffers.
  96. * Buffers will be cleared before rendering to this viewport is performed.
  97. */
  98. void setRequiresClear(bool colorClear, bool depthClear, bool stencilClear);
  99. /**
  100. * @brief Sets values to clear color, depth and stencil buffers to.
  101. */
  102. void setClearValues(const Color& clearColor, float clearDepth = 0.0f, UINT16 clearStencil = 0);
  103. /**
  104. * @brief Returns the color to clear the viewport color buffers to.
  105. */
  106. const Color& getClearColor() const { return mClearColor; }
  107. /**
  108. * @brief Returns the value to clear the viewport depth buffers to.
  109. */
  110. float getClearDepthValue() const { return mDepthClearValue; }
  111. /**
  112. * @brief Returns the value to clear the viewport stencil buffer to.
  113. */
  114. UINT16 getClearStencilValue() const { return mStencilClearValue; }
  115. /**
  116. * @brief Returns true if viewport requires color clear before rendering.
  117. */
  118. bool getRequiresColorClear() const { return mRequiresColorClear; }
  119. /**
  120. * @brief Returns true if viewport requires depth clear before rendering.
  121. */
  122. bool getRequiresDepthClear() const { return mRequiresDepthClear; }
  123. /**
  124. * @brief Returns true if viewport requires stencil clear before rendering.
  125. */
  126. bool getRequiresStencilClear() const { return mRequiresStencilClear; }
  127. /**
  128. * @brief Makes an exact copy of this viewport.
  129. */
  130. Viewport clone();
  131. /**
  132. * @brief Checks is the core dirty flag set. This is used by external systems
  133. * to know when internal data has changed and core thread potentially needs to be notified.
  134. */
  135. bool _isCoreDirty() const { return mCoreDirtyFlags != 0; }
  136. /**
  137. * @brief Marks the core dirty flag as clean.
  138. */
  139. void _markCoreClean() { mCoreDirtyFlags = 0; }
  140. protected:
  141. /**
  142. * @brief Marks the core data as dirty, signaling that the core thread needs an updated version.
  143. */
  144. void markCoreDirty() { mCoreDirtyFlags = 0xFFFFFFFF; }
  145. RenderTargetPtr mTarget;
  146. Rect2 mNormArea;
  147. bool mRequiresColorClear;
  148. bool mRequiresDepthClear;
  149. bool mRequiresStencilClear;
  150. Color mClearColor;
  151. float mDepthClearValue;
  152. UINT16 mStencilClearValue;
  153. UINT32 mCoreDirtyFlags; /**< True when internal data has changed and core thread wasn't yet informed. */
  154. static const Color DEFAULT_CLEAR_COLOR;
  155. /************************************************************************/
  156. /* RTTI */
  157. /************************************************************************/
  158. public:
  159. friend class ViewportRTTI;
  160. static RTTITypeBase* getRTTIStatic();
  161. virtual RTTITypeBase* getRTTI() const;
  162. };
  163. }