CmViewport.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. /*
  2. -----------------------------------------------------------------------------
  3. This source file is part of OGRE
  4. (Object-oriented Graphics Rendering Engine)
  5. For the latest info, see http://www.ogre3d.org/
  6. Copyright (c) 2000-2011 Torus Knot Software Ltd
  7. Permission is hereby granted, free of charge, to any person obtaining a copy
  8. of this software and associated documentation files (the "Software"), to deal
  9. in the Software without restriction, including without limitation the rights
  10. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. copies of the Software, and to permit persons to whom the Software is
  12. furnished to do so, subject to the following conditions:
  13. The above copyright notice and this permission notice shall be included in
  14. all copies or substantial portions of the Software.
  15. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. THE SOFTWARE.
  22. -----------------------------------------------------------------------------
  23. */
  24. #include "CmViewport.h"
  25. #include "CmException.h"
  26. #include "CmRenderTarget.h"
  27. #include "CmMath.h"
  28. #include "CmRenderSystem.h"
  29. #include "CmRenderSystemManager.h"
  30. namespace CamelotEngine {
  31. //---------------------------------------------------------------------
  32. Viewport::Viewport()
  33. :mTarget(nullptr)
  34. , mRelLeft(0)
  35. , mRelTop(0)
  36. , mRelWidth(0)
  37. , mRelHeight(0)
  38. // Actual dimensions will update later
  39. , mZOrder(0)
  40. , mBackColour(Color::Black)
  41. , mClearEveryFrame(true)
  42. , mClearBuffers(FBT_COLOUR | FBT_DEPTH)
  43. {
  44. // Calculate actual dimensions
  45. updateDimensions();
  46. }
  47. //---------------------------------------------------------------------
  48. Viewport::Viewport(RenderTarget* target, float left, float top, float width, float height, int ZOrder)
  49. :mTarget(target)
  50. , mRelLeft(left)
  51. , mRelTop(top)
  52. , mRelWidth(width)
  53. , mRelHeight(height)
  54. // Actual dimensions will update later
  55. , mZOrder(ZOrder)
  56. , mBackColour(Color::Black)
  57. , mClearEveryFrame(true)
  58. , mClearBuffers(FBT_COLOUR | FBT_DEPTH)
  59. {
  60. // Calculate actual dimensions
  61. updateDimensions();
  62. }
  63. //---------------------------------------------------------------------
  64. Viewport::~Viewport()
  65. {
  66. }
  67. //---------------------------------------------------------------------
  68. void Viewport::updateDimensions(void)
  69. {
  70. if(mTarget != nullptr)
  71. {
  72. float height = (float) mTarget->getHeight();
  73. float width = (float) mTarget->getWidth();
  74. mActLeft = (int) (mRelLeft * width);
  75. mActTop = (int) (mRelTop * height);
  76. mActWidth = (int) (mRelWidth * width);
  77. mActHeight = (int) (mRelHeight * height);
  78. }
  79. }
  80. //---------------------------------------------------------------------
  81. int Viewport::getZOrder(void) const
  82. {
  83. return mZOrder;
  84. }
  85. //---------------------------------------------------------------------
  86. RenderTarget* Viewport::getTarget(void) const
  87. {
  88. return mTarget;
  89. }
  90. //---------------------------------------------------------------------
  91. float Viewport::getLeft(void) const
  92. {
  93. return mRelLeft;
  94. }
  95. //---------------------------------------------------------------------
  96. float Viewport::getTop(void) const
  97. {
  98. return mRelTop;
  99. }
  100. //---------------------------------------------------------------------
  101. float Viewport::getWidth(void) const
  102. {
  103. return mRelWidth;
  104. }
  105. //---------------------------------------------------------------------
  106. float Viewport::getHeight(void) const
  107. {
  108. return mRelHeight;
  109. }
  110. //---------------------------------------------------------------------
  111. int Viewport::getActualLeft(void) const
  112. {
  113. return mActLeft;
  114. }
  115. //---------------------------------------------------------------------
  116. int Viewport::getActualTop(void) const
  117. {
  118. return mActTop;
  119. }
  120. //---------------------------------------------------------------------
  121. int Viewport::getActualWidth(void) const
  122. {
  123. return mActWidth;
  124. }
  125. //---------------------------------------------------------------------
  126. int Viewport::getActualHeight(void) const
  127. {
  128. return mActHeight;
  129. }
  130. //---------------------------------------------------------------------
  131. void Viewport::setDimensions(float left, float top, float width, float height)
  132. {
  133. mRelLeft = left;
  134. mRelTop = top;
  135. mRelWidth = width;
  136. mRelHeight = height;
  137. updateDimensions();
  138. }
  139. //---------------------------------------------------------------------
  140. void Viewport::setBackgroundColour(const Color& colour)
  141. {
  142. mBackColour = colour;
  143. }
  144. //---------------------------------------------------------------------
  145. const Color& Viewport::getBackgroundColour(void) const
  146. {
  147. return mBackColour;
  148. }
  149. //---------------------------------------------------------------------
  150. void Viewport::setClearEveryFrame(bool inClear, unsigned int inBuffers)
  151. {
  152. mClearEveryFrame = inClear;
  153. mClearBuffers = inBuffers;
  154. }
  155. //---------------------------------------------------------------------
  156. bool Viewport::getClearEveryFrame(void) const
  157. {
  158. return mClearEveryFrame;
  159. }
  160. //---------------------------------------------------------------------
  161. unsigned int Viewport::getClearBuffers(void) const
  162. {
  163. return mClearBuffers;
  164. }
  165. //---------------------------------------------------------------------
  166. void Viewport::clear(unsigned int buffers, const Color& col,
  167. float depth, unsigned short stencil)
  168. {
  169. RenderSystem* rs = CamelotEngine::RenderSystemManager::getActive();
  170. if (rs)
  171. {
  172. Viewport currentvp = rs->getViewport_internal();
  173. rs->setViewport_internal(*this);
  174. rs->clearFrameBuffer_internal(buffers, col, depth, stencil);
  175. rs->setViewport_internal(currentvp);
  176. }
  177. }
  178. //---------------------------------------------------------------------
  179. void Viewport::getActualDimensions(int &left, int&top, int &width, int &height) const
  180. {
  181. left = mActLeft;
  182. top = mActTop;
  183. width = mActWidth;
  184. height = mActHeight;
  185. }
  186. }