BsViewport.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. #pragma once
  2. #include "BsCorePrerequisites.h"
  3. #include "BsColor.h"
  4. #include "BsRectI.h"
  5. #include "BsRectF.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. RectI getArea() const;
  87. /**
  88. * @brief Activates or deactivates clears for color, depth or stencil buffers.
  89. * Buffers will be cleared before rendering to this viewport is performed.
  90. */
  91. void setRequiresClear(bool colorClear, bool depthClear, bool stencilClear);
  92. /**
  93. * @brief Sets values to clear color, depth and stencil buffers to.
  94. */
  95. void setClearValues(const Color& clearColor, float clearDepth = 0.0f, UINT16 clearStencil = 0);
  96. /**
  97. * @brief Returns the color to clear the viewport color buffers to.
  98. */
  99. const Color& getClearColor() const { return mClearColor; }
  100. /**
  101. * @brief Returns the value to clear the viewport depth buffers to.
  102. */
  103. float getClearDepthValue() const { return mDepthClearValue; }
  104. /**
  105. * @brief Returns the value to clear the viewport stencil buffer to.
  106. */
  107. UINT16 getClearStencilValue() const { return mStencilClearValue; }
  108. /**
  109. * @brief Returns true if viewport requires color clear before rendering.
  110. */
  111. bool getRequiresColorClear() const { return mRequiresColorClear; }
  112. /**
  113. * @brief Returns true if viewport requires depth clear before rendering.
  114. */
  115. bool getRequiresDepthClear() const { return mRequiresDepthClear; }
  116. /**
  117. * @brief Returns true if viewport requires stencil clear before rendering.
  118. */
  119. bool getRequiresStencilClear() const { return mRequiresStencilClear; }
  120. /**
  121. * @brief Makes an exact copy of this viewport.
  122. */
  123. Viewport clone();
  124. /**
  125. * @brief Checks is the core dirty flag set. This is used by external systems
  126. * to know when internal data has changed and core thread potentially needs to be notified.
  127. */
  128. bool _isCoreDirty() const { return mCoreDirtyFlags != 0; }
  129. /**
  130. * @brief Marks the core dirty flag as clean.
  131. */
  132. void _markCoreClean() { mCoreDirtyFlags = 0; }
  133. protected:
  134. /**
  135. * @brief Marks the core data as dirty, signaling that the core thread needs an updated version.
  136. */
  137. void markCoreDirty() { mCoreDirtyFlags = 0xFFFFFFFF; }
  138. RenderTargetPtr mTarget;
  139. RectF mNormArea;
  140. bool mRequiresColorClear;
  141. bool mRequiresDepthClear;
  142. bool mRequiresStencilClear;
  143. Color mClearColor;
  144. float mDepthClearValue;
  145. UINT16 mStencilClearValue;
  146. UINT32 mCoreDirtyFlags; /**< True when internal data has changed and core thread wasn't yet informed. */
  147. static const Color DEFAULT_CLEAR_COLOR;
  148. };
  149. }