BsViewport.h 5.4 KB

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