OgreViewport.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  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 "OgreViewport.h"
  25. #include "OgreException.h"
  26. #include "OgreRenderTarget.h"
  27. #include "OgreMath.h"
  28. #include "OgreRenderSystem.h"
  29. #include "CmRenderSystemManager.h"
  30. namespace Ogre {
  31. OrientationMode Viewport::mDefaultOrientationMode = OR_DEGREE_0;
  32. //---------------------------------------------------------------------
  33. Viewport::Viewport(RenderTarget* target, Real left, Real top, Real width, Real height, int ZOrder)
  34. :mTarget(target)
  35. , mRelLeft(left)
  36. , mRelTop(top)
  37. , mRelWidth(width)
  38. , mRelHeight(height)
  39. // Actual dimensions will update later
  40. , mZOrder(ZOrder)
  41. , mBackColour(ColourValue::Black)
  42. , mClearEveryFrame(true)
  43. , mClearBuffers(FBT_COLOUR | FBT_DEPTH)
  44. , mUpdated(false)
  45. , mShowOverlays(true)
  46. , mShowSkies(true)
  47. , mShowShadows(true)
  48. , mVisibilityMask(0xFFFFFFFF)
  49. , mMaterialSchemeName("NotUsedInPort")
  50. , mIsAutoUpdated(true)
  51. {
  52. // Set the default orientation mode
  53. mOrientationMode = mDefaultOrientationMode;
  54. // Calculate actual dimensions
  55. _updateDimensions();
  56. }
  57. //---------------------------------------------------------------------
  58. Viewport::~Viewport()
  59. {
  60. }
  61. //---------------------------------------------------------------------
  62. bool Viewport::_isUpdated(void) const
  63. {
  64. return mUpdated;
  65. }
  66. //---------------------------------------------------------------------
  67. void Viewport::_clearUpdatedFlag(void)
  68. {
  69. mUpdated = false;
  70. }
  71. //---------------------------------------------------------------------
  72. void Viewport::_updateDimensions(void)
  73. {
  74. Real height = (Real) mTarget->getHeight();
  75. Real width = (Real) mTarget->getWidth();
  76. mActLeft = (int) (mRelLeft * width);
  77. mActTop = (int) (mRelTop * height);
  78. mActWidth = (int) (mRelWidth * width);
  79. mActHeight = (int) (mRelHeight * height);
  80. mUpdated = true;
  81. }
  82. //---------------------------------------------------------------------
  83. int Viewport::getZOrder(void) const
  84. {
  85. return mZOrder;
  86. }
  87. //---------------------------------------------------------------------
  88. RenderTarget* Viewport::getTarget(void) const
  89. {
  90. return mTarget;
  91. }
  92. //---------------------------------------------------------------------
  93. Real Viewport::getLeft(void) const
  94. {
  95. return mRelLeft;
  96. }
  97. //---------------------------------------------------------------------
  98. Real Viewport::getTop(void) const
  99. {
  100. return mRelTop;
  101. }
  102. //---------------------------------------------------------------------
  103. Real Viewport::getWidth(void) const
  104. {
  105. return mRelWidth;
  106. }
  107. //---------------------------------------------------------------------
  108. Real Viewport::getHeight(void) const
  109. {
  110. return mRelHeight;
  111. }
  112. //---------------------------------------------------------------------
  113. int Viewport::getActualLeft(void) const
  114. {
  115. return mActLeft;
  116. }
  117. //---------------------------------------------------------------------
  118. int Viewport::getActualTop(void) const
  119. {
  120. return mActTop;
  121. }
  122. //---------------------------------------------------------------------
  123. int Viewport::getActualWidth(void) const
  124. {
  125. return mActWidth;
  126. }
  127. //---------------------------------------------------------------------
  128. int Viewport::getActualHeight(void) const
  129. {
  130. return mActHeight;
  131. }
  132. //---------------------------------------------------------------------
  133. void Viewport::setDimensions(Real left, Real top, Real width, Real height)
  134. {
  135. mRelLeft = left;
  136. mRelTop = top;
  137. mRelWidth = width;
  138. mRelHeight = height;
  139. _updateDimensions();
  140. }
  141. //---------------------------------------------------------------------
  142. void Viewport::update(void)
  143. {
  144. }
  145. //---------------------------------------------------------------------
  146. void Viewport::setOrientationMode(OrientationMode orientationMode, bool setDefault)
  147. {
  148. #if OGRE_NO_VIEWPORT_ORIENTATIONMODE != 0
  149. OGRE_EXCEPT(Exception::ERR_NOT_IMPLEMENTED,
  150. "Setting Viewport orientation mode is not supported",
  151. __FUNCTION__);
  152. #endif
  153. mOrientationMode = orientationMode;
  154. if (setDefault)
  155. {
  156. setDefaultOrientationMode(orientationMode);
  157. }
  158. // Update the render system config
  159. RenderSystem* rs = CamelotEngine::RenderSystemManager::getActive();
  160. if(mOrientationMode == OR_LANDSCAPELEFT)
  161. rs->setConfigOption("Orientation", "Landscape Left");
  162. else if(mOrientationMode == OR_LANDSCAPERIGHT)
  163. rs->setConfigOption("Orientation", "Landscape Right");
  164. else if(mOrientationMode == OR_PORTRAIT)
  165. rs->setConfigOption("Orientation", "Portrait");
  166. }
  167. //---------------------------------------------------------------------
  168. OrientationMode Viewport::getOrientationMode() const
  169. {
  170. #if OGRE_NO_VIEWPORT_ORIENTATIONMODE != 0
  171. OGRE_EXCEPT(Exception::ERR_NOT_IMPLEMENTED,
  172. "Getting Viewport orientation mode is not supported",
  173. __FUNCTION__);
  174. #endif
  175. return mOrientationMode;
  176. }
  177. //---------------------------------------------------------------------
  178. void Viewport::setDefaultOrientationMode(OrientationMode orientationMode)
  179. {
  180. #if OGRE_NO_VIEWPORT_ORIENTATIONMODE != 0
  181. OGRE_EXCEPT(Exception::ERR_NOT_IMPLEMENTED,
  182. "Setting default Viewport orientation mode is not supported",
  183. __FUNCTION__);
  184. #endif
  185. mDefaultOrientationMode = orientationMode;
  186. }
  187. //---------------------------------------------------------------------
  188. OrientationMode Viewport::getDefaultOrientationMode()
  189. {
  190. #if OGRE_NO_VIEWPORT_ORIENTATIONMODE != 0
  191. OGRE_EXCEPT(Exception::ERR_NOT_IMPLEMENTED,
  192. "Getting default Viewport orientation mode is not supported",
  193. __FUNCTION__);
  194. #endif
  195. return mDefaultOrientationMode;
  196. }
  197. //---------------------------------------------------------------------
  198. void Viewport::setBackgroundColour(const ColourValue& colour)
  199. {
  200. mBackColour = colour;
  201. }
  202. //---------------------------------------------------------------------
  203. const ColourValue& Viewport::getBackgroundColour(void) const
  204. {
  205. return mBackColour;
  206. }
  207. //---------------------------------------------------------------------
  208. void Viewport::setClearEveryFrame(bool inClear, unsigned int inBuffers)
  209. {
  210. mClearEveryFrame = inClear;
  211. mClearBuffers = inBuffers;
  212. }
  213. //---------------------------------------------------------------------
  214. bool Viewport::getClearEveryFrame(void) const
  215. {
  216. return mClearEveryFrame;
  217. }
  218. //---------------------------------------------------------------------
  219. unsigned int Viewport::getClearBuffers(void) const
  220. {
  221. return mClearBuffers;
  222. }
  223. //---------------------------------------------------------------------
  224. void Viewport::clear(unsigned int buffers, const ColourValue& col,
  225. Real depth, unsigned short stencil)
  226. {
  227. RenderSystem* rs = CamelotEngine::RenderSystemManager::getActive();
  228. if (rs)
  229. {
  230. Viewport* currentvp = rs->_getViewport();
  231. if (currentvp && currentvp == this)
  232. rs->clearFrameBuffer(buffers, col, depth, stencil);
  233. else if (currentvp)
  234. {
  235. rs->_setViewport(this);
  236. rs->clearFrameBuffer(buffers, col, depth, stencil);
  237. rs->_setViewport(currentvp);
  238. }
  239. }
  240. }
  241. //---------------------------------------------------------------------
  242. void Viewport::getActualDimensions(int &left, int&top, int &width, int &height) const
  243. {
  244. left = mActLeft;
  245. top = mActTop;
  246. width = mActWidth;
  247. height = mActHeight;
  248. }
  249. //---------------------------------------------------------------------
  250. void Viewport::setAutoUpdated(bool inAutoUpdated)
  251. {
  252. mIsAutoUpdated = inAutoUpdated;
  253. }
  254. //---------------------------------------------------------------------
  255. bool Viewport::isAutoUpdated() const
  256. {
  257. return mIsAutoUpdated;
  258. }
  259. //---------------------------------------------------------------------
  260. void Viewport::setOverlaysEnabled(bool enabled)
  261. {
  262. mShowOverlays = enabled;
  263. }
  264. //---------------------------------------------------------------------
  265. bool Viewport::getOverlaysEnabled(void) const
  266. {
  267. return mShowOverlays;
  268. }
  269. //---------------------------------------------------------------------
  270. void Viewport::setSkiesEnabled(bool enabled)
  271. {
  272. mShowSkies = enabled;
  273. }
  274. //---------------------------------------------------------------------
  275. bool Viewport::getSkiesEnabled(void) const
  276. {
  277. return mShowSkies;
  278. }
  279. //---------------------------------------------------------------------
  280. void Viewport::setShadowsEnabled(bool enabled)
  281. {
  282. mShowShadows = enabled;
  283. }
  284. //---------------------------------------------------------------------
  285. bool Viewport::getShadowsEnabled(void) const
  286. {
  287. return mShowShadows;
  288. }
  289. //-----------------------------------------------------------------------
  290. void Viewport::pointOrientedToScreen(const Vector2 &v, int orientationMode, Vector2 &outv)
  291. {
  292. pointOrientedToScreen(v.x, v.y, orientationMode, outv.x, outv.y);
  293. }
  294. //-----------------------------------------------------------------------
  295. void Viewport::pointOrientedToScreen(Real orientedX, Real orientedY, int orientationMode,
  296. Real &screenX, Real &screenY)
  297. {
  298. Real orX = orientedX;
  299. Real orY = orientedY;
  300. switch (orientationMode)
  301. {
  302. case 1:
  303. screenX = orY;
  304. screenY = Real(1.0) - orX;
  305. break;
  306. case 2:
  307. screenX = Real(1.0) - orX;
  308. screenY = Real(1.0) - orY;
  309. break;
  310. case 3:
  311. screenX = Real(1.0) - orY;
  312. screenY = orX;
  313. break;
  314. default:
  315. screenX = orX;
  316. screenY = orY;
  317. break;
  318. }
  319. }
  320. //-----------------------------------------------------------------------
  321. }