CmViewport.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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(RenderTarget* target, float left, float top, float width, float height, int ZOrder)
  33. :mTarget(target)
  34. , mRelLeft(left)
  35. , mRelTop(top)
  36. , mRelWidth(width)
  37. , mRelHeight(height)
  38. // Actual dimensions will update later
  39. , mZOrder(ZOrder)
  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()
  49. {
  50. }
  51. //---------------------------------------------------------------------
  52. void Viewport::_updateDimensions(void)
  53. {
  54. if(mTarget != nullptr)
  55. {
  56. float height = (float) mTarget->getHeight();
  57. float width = (float) mTarget->getWidth();
  58. mActLeft = (int) (mRelLeft * width);
  59. mActTop = (int) (mRelTop * height);
  60. mActWidth = (int) (mRelWidth * width);
  61. mActHeight = (int) (mRelHeight * height);
  62. }
  63. }
  64. //---------------------------------------------------------------------
  65. int Viewport::getZOrder(void) const
  66. {
  67. return mZOrder;
  68. }
  69. //---------------------------------------------------------------------
  70. RenderTarget* Viewport::getTarget(void) const
  71. {
  72. return mTarget;
  73. }
  74. //---------------------------------------------------------------------
  75. float Viewport::getLeft(void) const
  76. {
  77. return mRelLeft;
  78. }
  79. //---------------------------------------------------------------------
  80. float Viewport::getTop(void) const
  81. {
  82. return mRelTop;
  83. }
  84. //---------------------------------------------------------------------
  85. float Viewport::getWidth(void) const
  86. {
  87. return mRelWidth;
  88. }
  89. //---------------------------------------------------------------------
  90. float Viewport::getHeight(void) const
  91. {
  92. return mRelHeight;
  93. }
  94. //---------------------------------------------------------------------
  95. int Viewport::getActualLeft(void) const
  96. {
  97. return mActLeft;
  98. }
  99. //---------------------------------------------------------------------
  100. int Viewport::getActualTop(void) const
  101. {
  102. return mActTop;
  103. }
  104. //---------------------------------------------------------------------
  105. int Viewport::getActualWidth(void) const
  106. {
  107. return mActWidth;
  108. }
  109. //---------------------------------------------------------------------
  110. int Viewport::getActualHeight(void) const
  111. {
  112. return mActHeight;
  113. }
  114. //---------------------------------------------------------------------
  115. void Viewport::setDimensions(float left, float top, float width, float height)
  116. {
  117. mRelLeft = left;
  118. mRelTop = top;
  119. mRelWidth = width;
  120. mRelHeight = height;
  121. _updateDimensions();
  122. }
  123. //---------------------------------------------------------------------
  124. void Viewport::update(void)
  125. {
  126. }
  127. //---------------------------------------------------------------------
  128. void Viewport::setBackgroundColour(const Color& colour)
  129. {
  130. mBackColour = colour;
  131. }
  132. //---------------------------------------------------------------------
  133. const Color& Viewport::getBackgroundColour(void) const
  134. {
  135. return mBackColour;
  136. }
  137. //---------------------------------------------------------------------
  138. void Viewport::setClearEveryFrame(bool inClear, unsigned int inBuffers)
  139. {
  140. mClearEveryFrame = inClear;
  141. mClearBuffers = inBuffers;
  142. }
  143. //---------------------------------------------------------------------
  144. bool Viewport::getClearEveryFrame(void) const
  145. {
  146. return mClearEveryFrame;
  147. }
  148. //---------------------------------------------------------------------
  149. unsigned int Viewport::getClearBuffers(void) const
  150. {
  151. return mClearBuffers;
  152. }
  153. //---------------------------------------------------------------------
  154. void Viewport::clear(unsigned int buffers, const Color& col,
  155. float depth, unsigned short stencil)
  156. {
  157. RenderSystem* rs = CamelotEngine::RenderSystemManager::getActive();
  158. if (rs)
  159. {
  160. Viewport* currentvp = rs->_getViewport();
  161. if (currentvp && currentvp == this)
  162. rs->clearFrameBuffer(buffers, col, depth, stencil);
  163. else if (currentvp)
  164. {
  165. rs->_setViewport(this);
  166. rs->clearFrameBuffer(buffers, col, depth, stencil);
  167. rs->_setViewport(currentvp);
  168. }
  169. }
  170. }
  171. //---------------------------------------------------------------------
  172. void Viewport::getActualDimensions(int &left, int&top, int &width, int &height) const
  173. {
  174. left = mActLeft;
  175. top = mActTop;
  176. width = mActWidth;
  177. height = mActHeight;
  178. }
  179. //-----------------------------------------------------------------------
  180. }