BsCamera.cpp 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796
  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 "BsCamera.h"
  25. #include "BsCameraRTTI.h"
  26. #include "CmMath.h"
  27. #include "CmMatrix3.h"
  28. #include "CmVector2.h"
  29. #include "CmAABox.h"
  30. #include "CmSphere.h"
  31. #include "CmHardwareBufferManager.h"
  32. #include "CmVertexBuffer.h"
  33. #include "CmIndexBuffer.h"
  34. #include "CmException.h"
  35. #include "CmRenderSystem.h"
  36. #include "CmSceneObject.h"
  37. using namespace CamelotFramework;
  38. namespace BansheeEngine
  39. {
  40. const float Camera::INFINITE_FAR_PLANE_ADJUST = 0.00001f;
  41. //-----------------------------------------------------------------------
  42. Camera::Camera(const HSceneObject& parent)
  43. : Component(parent),
  44. mProjType(PT_PERSPECTIVE),
  45. mHorzFOV(Radian(Math::PI/4.0f)),
  46. mFarDist(100000.0f),
  47. mNearDist(100.0f),
  48. mAspect(1.33333333333333f),
  49. mOrthoHeight(1000),
  50. mFrustumOffset(Vector2::ZERO),
  51. mFocalLength(1.0f),
  52. mLastParentOrientation(Quaternion::IDENTITY),
  53. mLastParentPosition(Vector3::ZERO),
  54. mRecalcFrustum(true),
  55. mRecalcFrustumPlanes(true),
  56. mRecalcWorldSpaceCorners(true),
  57. mRecalcVertexData(true),
  58. mCustomViewMatrix(false),
  59. mCustomProjMatrix(false),
  60. mFrustumExtentsManuallySet(false),
  61. mIgnoreSceneRenderables(false),
  62. mPriority(0),
  63. mLayers(0xFFFFFFFFFFFFFFFF)
  64. {
  65. updateView();
  66. updateFrustum();
  67. // Reasonable defaults to camera params
  68. mHorzFOV = Radian(Math::PI/4.0f);
  69. mNearDist = 100.0f;
  70. mFarDist = 100000.0f;
  71. mAspect = 1.33333333333333f;
  72. mProjType = PT_PERSPECTIVE;
  73. invalidateFrustum();
  74. // Init matrices
  75. mViewMatrix = Matrix4::ZERO;
  76. mProjMatrixRS = Matrix4::ZERO;
  77. }
  78. //-----------------------------------------------------------------------
  79. Camera::~Camera()
  80. {
  81. }
  82. void Camera::initialize(RenderTargetPtr target, float left, float top, float width, float height)
  83. {
  84. target->synchonize();
  85. mViewport = cm_shared_ptr<Viewport, PoolAlloc>(target, left, top, width, height);
  86. }
  87. //-----------------------------------------------------------------------
  88. void Camera::setHorzFOV(const Radian& fov)
  89. {
  90. mHorzFOV = fov;
  91. invalidateFrustum();
  92. }
  93. //-----------------------------------------------------------------------
  94. const Radian& Camera::getHorzFOV(void) const
  95. {
  96. return mHorzFOV;
  97. }
  98. //-----------------------------------------------------------------------
  99. void Camera::setFarClipDistance(float farPlane)
  100. {
  101. mFarDist = farPlane;
  102. invalidateFrustum();
  103. }
  104. //-----------------------------------------------------------------------
  105. float Camera::getFarClipDistance(void) const
  106. {
  107. return mFarDist;
  108. }
  109. //-----------------------------------------------------------------------
  110. void Camera::setNearClipDistance(float nearPlane)
  111. {
  112. if (nearPlane <= 0)
  113. {
  114. CM_EXCEPT(InvalidParametersException, "Near clip distance must be greater than zero.");
  115. }
  116. mNearDist = nearPlane;
  117. invalidateFrustum();
  118. }
  119. //-----------------------------------------------------------------------
  120. float Camera::getNearClipDistance(void) const
  121. {
  122. return mNearDist;
  123. }
  124. //---------------------------------------------------------------------
  125. void Camera::setFrustumOffset(const Vector2& offset)
  126. {
  127. mFrustumOffset = offset;
  128. invalidateFrustum();
  129. }
  130. //---------------------------------------------------------------------
  131. void Camera::setFrustumOffset(float horizontal, float vertical)
  132. {
  133. setFrustumOffset(Vector2(horizontal, vertical));
  134. }
  135. //---------------------------------------------------------------------
  136. const Vector2& Camera::getFrustumOffset() const
  137. {
  138. return mFrustumOffset;
  139. }
  140. //---------------------------------------------------------------------
  141. void Camera::setFocalLength(float focalLength)
  142. {
  143. if (focalLength <= 0)
  144. {
  145. CM_EXCEPT(InvalidParametersException,
  146. "Focal length must be greater than zero.");
  147. }
  148. mFocalLength = focalLength;
  149. invalidateFrustum();
  150. }
  151. //---------------------------------------------------------------------
  152. float Camera::getFocalLength() const
  153. {
  154. return mFocalLength;
  155. }
  156. //-----------------------------------------------------------------------
  157. const Matrix4& Camera::getProjectionMatrix(void) const
  158. {
  159. updateFrustum();
  160. return mProjMatrix;
  161. }
  162. //-----------------------------------------------------------------------
  163. const Matrix4& Camera::getProjectionMatrixWithRSDepth(void) const
  164. {
  165. updateFrustum();
  166. return mProjMatrixRSDepth;
  167. }
  168. //-----------------------------------------------------------------------
  169. const Matrix4& Camera::getProjectionMatrixRS(void) const
  170. {
  171. updateFrustum();
  172. return mProjMatrixRS;
  173. }
  174. //-----------------------------------------------------------------------
  175. const Matrix4& Camera::getViewMatrix(void) const
  176. {
  177. updateView();
  178. return mViewMatrix;
  179. }
  180. //-----------------------------------------------------------------------
  181. const Plane* Camera::getFrustumPlanes(void) const
  182. {
  183. // Make any pending updates to the calculated frustum planes
  184. updateFrustumPlanes();
  185. return mFrustumPlanes;
  186. }
  187. //-----------------------------------------------------------------------
  188. const Plane& Camera::getFrustumPlane(unsigned short plane) const
  189. {
  190. // Make any pending updates to the calculated frustum planes
  191. updateFrustumPlanes();
  192. return mFrustumPlanes[plane];
  193. }
  194. //-----------------------------------------------------------------------
  195. bool Camera::isVisible(const AABox& bound, FrustumPlane* culledBy) const
  196. {
  197. // Make any pending updates to the calculated frustum planes
  198. updateFrustumPlanes();
  199. // Get centre of the box
  200. Vector3 centre = bound.getCenter();
  201. // Get the half-size of the box
  202. Vector3 halfSize = bound.getHalfSize();
  203. // For each plane, see if all points are on the negative side
  204. // If so, object is not visible
  205. for (int plane = 0; plane < 6; ++plane)
  206. {
  207. // Skip far plane if infinite view frustum
  208. if (plane == FRUSTUM_PLANE_FAR && mFarDist == 0)
  209. continue;
  210. Plane::Side side = mFrustumPlanes[plane].getSide(centre, halfSize);
  211. if (side == Plane::NEGATIVE_SIDE)
  212. {
  213. // ALL corners on negative side therefore out of view
  214. if (culledBy)
  215. *culledBy = (FrustumPlane)plane;
  216. return false;
  217. }
  218. }
  219. return true;
  220. }
  221. //-----------------------------------------------------------------------
  222. bool Camera::isVisible(const Vector3& vert, FrustumPlane* culledBy) const
  223. {
  224. // Make any pending updates to the calculated frustum planes
  225. updateFrustumPlanes();
  226. // For each plane, see if all points are on the negative side
  227. // If so, object is not visible
  228. for (int plane = 0; plane < 6; ++plane)
  229. {
  230. // Skip far plane if infinite view frustum
  231. if (plane == FRUSTUM_PLANE_FAR && mFarDist == 0)
  232. continue;
  233. if (mFrustumPlanes[plane].getSide(vert) == Plane::NEGATIVE_SIDE)
  234. {
  235. // ALL corners on negative side therefore out of view
  236. if (culledBy)
  237. *culledBy = (FrustumPlane)plane;
  238. return false;
  239. }
  240. }
  241. return true;
  242. }
  243. //-----------------------------------------------------------------------
  244. bool Camera::isVisible(const Sphere& sphere, FrustumPlane* culledBy) const
  245. {
  246. // Make any pending updates to the calculated frustum planes
  247. updateFrustumPlanes();
  248. // For each plane, see if sphere is on negative side
  249. // If so, object is not visible
  250. for (int plane = 0; plane < 6; ++plane)
  251. {
  252. // Skip far plane if infinite view frustum
  253. if (plane == FRUSTUM_PLANE_FAR && mFarDist == 0)
  254. continue;
  255. // If the distance from sphere center to plane is negative, and 'more negative'
  256. // than the radius of the sphere, sphere is outside frustum
  257. if (mFrustumPlanes[plane].getDistance(sphere.getCenter()) < -sphere.getRadius())
  258. {
  259. // ALL corners on negative side therefore out of view
  260. if (culledBy)
  261. *culledBy = (FrustumPlane)plane;
  262. return false;
  263. }
  264. }
  265. return true;
  266. }
  267. //-----------------------------------------------------------------------
  268. void Camera::calcProjectionParameters(float& left, float& right, float& bottom, float& top) const
  269. {
  270. if (mCustomProjMatrix)
  271. {
  272. // Convert clipspace corners to camera space
  273. Matrix4 invProj = mProjMatrix.inverse();
  274. Vector3 topLeft(-0.5f, 0.5f, 0.0f);
  275. Vector3 bottomRight(0.5f, -0.5f, 0.0f);
  276. topLeft = invProj * topLeft;
  277. bottomRight = invProj * bottomRight;
  278. left = topLeft.x;
  279. top = topLeft.y;
  280. right = bottomRight.x;
  281. bottom = bottomRight.y;
  282. }
  283. else
  284. {
  285. if (mFrustumExtentsManuallySet)
  286. {
  287. left = mLeft;
  288. right = mRight;
  289. top = mTop;
  290. bottom = mBottom;
  291. }
  292. // Calculate general projection parameters
  293. else if (mProjType == PT_PERSPECTIVE)
  294. {
  295. Radian thetaY (mHorzFOV * 0.5f);
  296. float tanThetaY = Math::Tan(thetaY);
  297. float tanThetaX = tanThetaY * mAspect;
  298. float nearFocal = mNearDist / mFocalLength;
  299. float nearOffsetX = mFrustumOffset.x * nearFocal;
  300. float nearOffsetY = mFrustumOffset.y * nearFocal;
  301. float half_w = tanThetaX * mNearDist;
  302. float half_h = tanThetaY * mNearDist;
  303. left = - half_w + nearOffsetX;
  304. right = + half_w + nearOffsetX;
  305. bottom = - half_h + nearOffsetY;
  306. top = + half_h + nearOffsetY;
  307. mLeft = left;
  308. mRight = right;
  309. mTop = top;
  310. mBottom = bottom;
  311. }
  312. else
  313. {
  314. // Unknown how to apply frustum offset to orthographic camera, just ignore here
  315. float half_w = getOrthoWindowWidth() * 0.5f;
  316. float half_h = getOrthoWindowHeight() * 0.5f;
  317. left = - half_w;
  318. right = + half_w;
  319. bottom = - half_h;
  320. top = + half_h;
  321. mLeft = left;
  322. mRight = right;
  323. mTop = top;
  324. mBottom = bottom;
  325. }
  326. }
  327. }
  328. //-----------------------------------------------------------------------
  329. void Camera::updateFrustumImpl(void) const
  330. {
  331. // Common calcs
  332. float left, right, bottom, top;
  333. calcProjectionParameters(left, right, bottom, top);
  334. if (!mCustomProjMatrix)
  335. {
  336. // The code below will dealing with general projection
  337. // parameters, similar glFrustum and glOrtho.
  338. // Doesn't optimise manually except division operator, so the
  339. // code more self-explaining.
  340. float inv_w = 1 / (right - left);
  341. float inv_h = 1 / (top - bottom);
  342. float inv_d = 1 / (mFarDist - mNearDist);
  343. // Recalc if frustum params changed
  344. if (mProjType == PT_PERSPECTIVE)
  345. {
  346. // Calc matrix elements
  347. float A = 2 * mNearDist * inv_w;
  348. float B = 2 * mNearDist * inv_h;
  349. float C = (right + left) * inv_w;
  350. float D = (top + bottom) * inv_h;
  351. float q, qn;
  352. if (mFarDist == 0)
  353. {
  354. // Infinite far plane
  355. q = Camera::INFINITE_FAR_PLANE_ADJUST - 1;
  356. qn = mNearDist * (Camera::INFINITE_FAR_PLANE_ADJUST - 2);
  357. }
  358. else
  359. {
  360. q = - (mFarDist + mNearDist) * inv_d;
  361. qn = -2 * (mFarDist * mNearDist) * inv_d;
  362. }
  363. // NB: This creates 'uniform' perspective projection matrix,
  364. // which depth range [-1,1], right-handed rules
  365. //
  366. // [ A 0 C 0 ]
  367. // [ 0 B D 0 ]
  368. // [ 0 0 q qn ]
  369. // [ 0 0 -1 0 ]
  370. //
  371. // A = 2 * near / (right - left)
  372. // B = 2 * near / (top - bottom)
  373. // C = (right + left) / (right - left)
  374. // D = (top + bottom) / (top - bottom)
  375. // q = - (far + near) / (far - near)
  376. // qn = - 2 * (far * near) / (far - near)
  377. mProjMatrix = Matrix4::ZERO;
  378. mProjMatrix[0][0] = A;
  379. mProjMatrix[0][2] = C;
  380. mProjMatrix[1][1] = B;
  381. mProjMatrix[1][2] = D;
  382. mProjMatrix[2][2] = q;
  383. mProjMatrix[2][3] = qn;
  384. mProjMatrix[3][2] = -1;
  385. } // perspective
  386. else if (mProjType == PT_ORTHOGRAPHIC)
  387. {
  388. float A = 2 * inv_w;
  389. float B = 2 * inv_h;
  390. float C = - (right + left) * inv_w;
  391. float D = - (top + bottom) * inv_h;
  392. float q, qn;
  393. if (mFarDist == 0)
  394. {
  395. // Can not do infinite far plane here, avoid divided zero only
  396. q = - Camera::INFINITE_FAR_PLANE_ADJUST / mNearDist;
  397. qn = - Camera::INFINITE_FAR_PLANE_ADJUST - 1;
  398. }
  399. else
  400. {
  401. q = - 2 * inv_d;
  402. qn = - (mFarDist + mNearDist) * inv_d;
  403. }
  404. // NB: This creates 'uniform' orthographic projection matrix,
  405. // which depth range [-1,1], right-handed rules
  406. //
  407. // [ A 0 0 C ]
  408. // [ 0 B 0 D ]
  409. // [ 0 0 q qn ]
  410. // [ 0 0 0 1 ]
  411. //
  412. // A = 2 * / (right - left)
  413. // B = 2 * / (top - bottom)
  414. // C = - (right + left) / (right - left)
  415. // D = - (top + bottom) / (top - bottom)
  416. // q = - 2 / (far - near)
  417. // qn = - (far + near) / (far - near)
  418. mProjMatrix = Matrix4::ZERO;
  419. mProjMatrix[0][0] = A;
  420. mProjMatrix[0][3] = C;
  421. mProjMatrix[1][1] = B;
  422. mProjMatrix[1][3] = D;
  423. mProjMatrix[2][2] = q;
  424. mProjMatrix[2][3] = qn;
  425. mProjMatrix[3][3] = 1;
  426. } // ortho
  427. } // !mCustomProjMatrix
  428. RenderSystem* renderSystem = CamelotFramework::RenderSystem::instancePtr();
  429. // API specific
  430. renderSystem->convertProjectionMatrix(mProjMatrix, mProjMatrixRS);
  431. // API specific for Gpu Programs
  432. renderSystem->convertProjectionMatrix(mProjMatrix, mProjMatrixRSDepth, true);
  433. // Calculate bounding box (local)
  434. // Box is from 0, down -Z, max dimensions as determined from far plane
  435. // If infinite view frustum just pick a far value
  436. float farDist = (mFarDist == 0) ? 100000 : mFarDist;
  437. // Near plane bounds
  438. Vector3 min(left, bottom, -farDist);
  439. Vector3 max(right, top, 0);
  440. if (mCustomProjMatrix)
  441. {
  442. // Some custom projection matrices can have unusual inverted settings
  443. // So make sure the AABB is the right way around to start with
  444. Vector3 tmp = min;
  445. min.floor(max);
  446. max.ceil(tmp);
  447. }
  448. if (mProjType == PT_PERSPECTIVE)
  449. {
  450. // Merge with far plane bounds
  451. float radio = farDist / mNearDist;
  452. min.floor(Vector3(left * radio, bottom * radio, -farDist));
  453. max.ceil(Vector3(right * radio, top * radio, 0));
  454. }
  455. mBoundingBox.setExtents(min, max);
  456. mRecalcFrustum = false;
  457. // Signal to update frustum clipping planes
  458. mRecalcFrustumPlanes = true;
  459. }
  460. //-----------------------------------------------------------------------
  461. void Camera::updateFrustum(void) const
  462. {
  463. if (isFrustumOutOfDate())
  464. {
  465. updateFrustumImpl();
  466. }
  467. }
  468. //-----------------------------------------------------------------------
  469. bool Camera::isFrustumOutOfDate(void) const
  470. {
  471. return mRecalcFrustum;
  472. }
  473. //-----------------------------------------------------------------------
  474. void Camera::updateView(void) const
  475. {
  476. if (!mCustomViewMatrix)
  477. {
  478. Matrix3 rot;
  479. const Quaternion& orientation = sceneObject()->getWorldRotation();
  480. const Vector3& position = sceneObject()->getWorldPosition();
  481. mViewMatrix = Math::makeViewMatrix(position, orientation, 0);
  482. }
  483. }
  484. //-----------------------------------------------------------------------
  485. void Camera::updateFrustumPlanesImpl(void) const
  486. {
  487. // -------------------------
  488. // Update the frustum planes
  489. // -------------------------
  490. Matrix4 combo = mProjMatrix * mViewMatrix;
  491. mFrustumPlanes[FRUSTUM_PLANE_LEFT].normal.x = combo[3][0] + combo[0][0];
  492. mFrustumPlanes[FRUSTUM_PLANE_LEFT].normal.y = combo[3][1] + combo[0][1];
  493. mFrustumPlanes[FRUSTUM_PLANE_LEFT].normal.z = combo[3][2] + combo[0][2];
  494. mFrustumPlanes[FRUSTUM_PLANE_LEFT].d = combo[3][3] + combo[0][3];
  495. mFrustumPlanes[FRUSTUM_PLANE_RIGHT].normal.x = combo[3][0] - combo[0][0];
  496. mFrustumPlanes[FRUSTUM_PLANE_RIGHT].normal.y = combo[3][1] - combo[0][1];
  497. mFrustumPlanes[FRUSTUM_PLANE_RIGHT].normal.z = combo[3][2] - combo[0][2];
  498. mFrustumPlanes[FRUSTUM_PLANE_RIGHT].d = combo[3][3] - combo[0][3];
  499. mFrustumPlanes[FRUSTUM_PLANE_TOP].normal.x = combo[3][0] - combo[1][0];
  500. mFrustumPlanes[FRUSTUM_PLANE_TOP].normal.y = combo[3][1] - combo[1][1];
  501. mFrustumPlanes[FRUSTUM_PLANE_TOP].normal.z = combo[3][2] - combo[1][2];
  502. mFrustumPlanes[FRUSTUM_PLANE_TOP].d = combo[3][3] - combo[1][3];
  503. mFrustumPlanes[FRUSTUM_PLANE_BOTTOM].normal.x = combo[3][0] + combo[1][0];
  504. mFrustumPlanes[FRUSTUM_PLANE_BOTTOM].normal.y = combo[3][1] + combo[1][1];
  505. mFrustumPlanes[FRUSTUM_PLANE_BOTTOM].normal.z = combo[3][2] + combo[1][2];
  506. mFrustumPlanes[FRUSTUM_PLANE_BOTTOM].d = combo[3][3] + combo[1][3];
  507. mFrustumPlanes[FRUSTUM_PLANE_NEAR].normal.x = combo[3][0] + combo[2][0];
  508. mFrustumPlanes[FRUSTUM_PLANE_NEAR].normal.y = combo[3][1] + combo[2][1];
  509. mFrustumPlanes[FRUSTUM_PLANE_NEAR].normal.z = combo[3][2] + combo[2][2];
  510. mFrustumPlanes[FRUSTUM_PLANE_NEAR].d = combo[3][3] + combo[2][3];
  511. mFrustumPlanes[FRUSTUM_PLANE_FAR].normal.x = combo[3][0] - combo[2][0];
  512. mFrustumPlanes[FRUSTUM_PLANE_FAR].normal.y = combo[3][1] - combo[2][1];
  513. mFrustumPlanes[FRUSTUM_PLANE_FAR].normal.z = combo[3][2] - combo[2][2];
  514. mFrustumPlanes[FRUSTUM_PLANE_FAR].d = combo[3][3] - combo[2][3];
  515. // Renormalise any normals which were not unit length
  516. for(int i=0; i<6; i++ )
  517. {
  518. float length = mFrustumPlanes[i].normal.normalize();
  519. mFrustumPlanes[i].d /= length;
  520. }
  521. mRecalcFrustumPlanes = false;
  522. }
  523. //-----------------------------------------------------------------------
  524. void Camera::updateFrustumPlanes(void) const
  525. {
  526. updateView();
  527. updateFrustum();
  528. if (mRecalcFrustumPlanes)
  529. {
  530. updateFrustumPlanesImpl();
  531. }
  532. }
  533. //-----------------------------------------------------------------------
  534. void Camera::updateWorldSpaceCornersImpl(void) const
  535. {
  536. Matrix4 eyeToWorld = mViewMatrix.inverseAffine();
  537. // Note: Even though we can dealing with general projection matrix here,
  538. // but because it's incompatibly with infinite far plane, thus, we
  539. // still need to working with projection parameters.
  540. // Calc near plane corners
  541. float nearLeft, nearRight, nearBottom, nearTop;
  542. calcProjectionParameters(nearLeft, nearRight, nearBottom, nearTop);
  543. // Treat infinite fardist as some arbitrary far value
  544. float farDist = (mFarDist == 0) ? 100000 : mFarDist;
  545. // Calc far palne corners
  546. float radio = mProjType == PT_PERSPECTIVE ? farDist / mNearDist : 1;
  547. float farLeft = nearLeft * radio;
  548. float farRight = nearRight * radio;
  549. float farBottom = nearBottom * radio;
  550. float farTop = nearTop * radio;
  551. // near
  552. mWorldSpaceCorners[0] = eyeToWorld.transformAffine(Vector3(nearRight, nearTop, -mNearDist));
  553. mWorldSpaceCorners[1] = eyeToWorld.transformAffine(Vector3(nearLeft, nearTop, -mNearDist));
  554. mWorldSpaceCorners[2] = eyeToWorld.transformAffine(Vector3(nearLeft, nearBottom, -mNearDist));
  555. mWorldSpaceCorners[3] = eyeToWorld.transformAffine(Vector3(nearRight, nearBottom, -mNearDist));
  556. // far
  557. mWorldSpaceCorners[4] = eyeToWorld.transformAffine(Vector3(farRight, farTop, -farDist));
  558. mWorldSpaceCorners[5] = eyeToWorld.transformAffine(Vector3(farLeft, farTop, -farDist));
  559. mWorldSpaceCorners[6] = eyeToWorld.transformAffine(Vector3(farLeft, farBottom, -farDist));
  560. mWorldSpaceCorners[7] = eyeToWorld.transformAffine(Vector3(farRight, farBottom, -farDist));
  561. mRecalcWorldSpaceCorners = false;
  562. }
  563. //-----------------------------------------------------------------------
  564. void Camera::updateWorldSpaceCorners(void) const
  565. {
  566. updateView();
  567. if (mRecalcWorldSpaceCorners)
  568. {
  569. updateWorldSpaceCornersImpl();
  570. }
  571. }
  572. //-----------------------------------------------------------------------
  573. float Camera::getAspectRatio(void) const
  574. {
  575. return mAspect;
  576. }
  577. //-----------------------------------------------------------------------
  578. void Camera::setAspectRatio(float r)
  579. {
  580. mAspect = r;
  581. invalidateFrustum();
  582. }
  583. //-----------------------------------------------------------------------
  584. const AABox& Camera::getBoundingBox(void) const
  585. {
  586. return mBoundingBox;
  587. }
  588. // -------------------------------------------------------------------
  589. const Vector3* Camera::getWorldSpaceCorners(void) const
  590. {
  591. updateWorldSpaceCorners();
  592. return mWorldSpaceCorners;
  593. }
  594. //-----------------------------------------------------------------------
  595. void Camera::setProjectionType(ProjectionType pt)
  596. {
  597. mProjType = pt;
  598. invalidateFrustum();
  599. }
  600. //-----------------------------------------------------------------------
  601. ProjectionType Camera::getProjectionType(void) const
  602. {
  603. return mProjType;
  604. }
  605. //---------------------------------------------------------------------
  606. void Camera::setCustomViewMatrix(bool enable, const Matrix4& viewMatrix)
  607. {
  608. mCustomViewMatrix = enable;
  609. if (enable)
  610. {
  611. assert(viewMatrix.isAffine());
  612. mViewMatrix = viewMatrix;
  613. }
  614. }
  615. //---------------------------------------------------------------------
  616. void Camera::setCustomProjectionMatrix(bool enable, const Matrix4& projMatrix)
  617. {
  618. mCustomProjMatrix = enable;
  619. if (enable)
  620. {
  621. mProjMatrix = projMatrix;
  622. }
  623. invalidateFrustum();
  624. }
  625. //---------------------------------------------------------------------
  626. void Camera::setOrthoWindow(float w, float h)
  627. {
  628. mOrthoHeight = h;
  629. mAspect = w / h;
  630. invalidateFrustum();
  631. }
  632. //---------------------------------------------------------------------
  633. void Camera::setOrthoWindowHeight(float h)
  634. {
  635. mOrthoHeight = h;
  636. invalidateFrustum();
  637. }
  638. //---------------------------------------------------------------------
  639. void Camera::setOrthoWindowWidth(float w)
  640. {
  641. mOrthoHeight = w / mAspect;
  642. invalidateFrustum();
  643. }
  644. //---------------------------------------------------------------------
  645. float Camera::getOrthoWindowHeight() const
  646. {
  647. return mOrthoHeight;
  648. }
  649. //---------------------------------------------------------------------
  650. float Camera::getOrthoWindowWidth() const
  651. {
  652. return mOrthoHeight * mAspect;
  653. }
  654. //---------------------------------------------------------------------
  655. void Camera::setFrustumExtents(float left, float right, float top, float bottom)
  656. {
  657. mFrustumExtentsManuallySet = true;
  658. mLeft = left;
  659. mRight = right;
  660. mTop = top;
  661. mBottom = bottom;
  662. invalidateFrustum();
  663. }
  664. //---------------------------------------------------------------------
  665. void Camera::resetFrustumExtents()
  666. {
  667. mFrustumExtentsManuallySet = false;
  668. invalidateFrustum();
  669. }
  670. //---------------------------------------------------------------------
  671. void Camera::getFrustumExtents(float& outleft, float& outright, float& outtop, float& outbottom) const
  672. {
  673. updateFrustum();
  674. outleft = mLeft;
  675. outright = mRight;
  676. outtop = mTop;
  677. outbottom = mBottom;
  678. }
  679. // -------------------------------------------------------------------
  680. void Camera::invalidateFrustum(void) const
  681. {
  682. mRecalcFrustumPlanes = true;
  683. mRecalcWorldSpaceCorners = true;
  684. mRecalcVertexData = true;
  685. }
  686. // -------------------------------------------------------------------
  687. float Camera::getBoundingRadius(void) const
  688. {
  689. // return a little bigger than the near distance
  690. // just to keep things just outside
  691. return mNearDist * 1.5f;
  692. }
  693. RTTITypeBase* Camera::getRTTIStatic()
  694. {
  695. return CameraRTTI::instance();
  696. }
  697. RTTITypeBase* Camera::getRTTI() const
  698. {
  699. return Camera::getRTTIStatic();
  700. }
  701. } // namespace CamelotFramework