CmCamera.cpp 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966
  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 "CmCamera.h"
  25. #include "CmCameraRTTI.h"
  26. #include "CmMath.h"
  27. #include "CmMatrix3.h"
  28. #include "CmVector2.h"
  29. #include "CmAxisAlignedBox.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 "CmGameObject.h"
  37. namespace CamelotEngine {
  38. const float Camera::INFINITE_FAR_PLANE_ADJUST = 0.00001f;
  39. //-----------------------------------------------------------------------
  40. Camera::Camera(GameObjectPtr parent)
  41. : Component(parent),
  42. mProjType(PT_PERSPECTIVE),
  43. mFOVy(Radian(Math::PI/4.0f)),
  44. mFarDist(100000.0f),
  45. mNearDist(100.0f),
  46. mAspect(1.33333333333333f),
  47. mOrthoHeight(1000),
  48. mFrustumOffset(Vector2::ZERO),
  49. mFocalLength(1.0f),
  50. mLastParentOrientation(Quaternion::IDENTITY),
  51. mLastParentPosition(Vector3::ZERO),
  52. mRecalcFrustum(true),
  53. mRecalcFrustumPlanes(true),
  54. mRecalcWorldSpaceCorners(true),
  55. mRecalcVertexData(true),
  56. mCustomViewMatrix(false),
  57. mCustomProjMatrix(false),
  58. mFrustumExtentsManuallySet(false),
  59. mViewport(nullptr)
  60. {
  61. updateView();
  62. updateFrustum();
  63. // Reasonable defaults to camera params
  64. mFOVy = Radian(Math::PI/4.0f);
  65. mNearDist = 100.0f;
  66. mFarDist = 100000.0f;
  67. mAspect = 1.33333333333333f;
  68. mProjType = PT_PERSPECTIVE;
  69. invalidateFrustum();
  70. // Init matrices
  71. mViewMatrix = Matrix4::ZERO;
  72. mProjMatrixRS = Matrix4::ZERO;
  73. }
  74. //-----------------------------------------------------------------------
  75. Camera::~Camera()
  76. {
  77. if(mViewport != nullptr)
  78. delete mViewport;
  79. }
  80. void Camera::init(RenderTargetPtr target, float left, float top, float width, float height, int ZOrder)
  81. {
  82. mViewport = new Viewport(target, left, top, width, height, ZOrder);
  83. }
  84. //-----------------------------------------------------------------------
  85. void Camera::setFOVy(const Radian& fov)
  86. {
  87. mFOVy = fov;
  88. invalidateFrustum();
  89. }
  90. //-----------------------------------------------------------------------
  91. const Radian& Camera::getFOVy(void) const
  92. {
  93. return mFOVy;
  94. }
  95. //-----------------------------------------------------------------------
  96. void Camera::setFarClipDistance(float farPlane)
  97. {
  98. mFarDist = farPlane;
  99. invalidateFrustum();
  100. }
  101. //-----------------------------------------------------------------------
  102. float Camera::getFarClipDistance(void) const
  103. {
  104. return mFarDist;
  105. }
  106. //-----------------------------------------------------------------------
  107. void Camera::setNearClipDistance(float nearPlane)
  108. {
  109. if (nearPlane <= 0)
  110. {
  111. CM_EXCEPT(InvalidParametersException, "Near clip distance must be greater than zero.");
  112. }
  113. mNearDist = nearPlane;
  114. invalidateFrustum();
  115. }
  116. //-----------------------------------------------------------------------
  117. float Camera::getNearClipDistance(void) const
  118. {
  119. return mNearDist;
  120. }
  121. //---------------------------------------------------------------------
  122. void Camera::setFrustumOffset(const Vector2& offset)
  123. {
  124. mFrustumOffset = offset;
  125. invalidateFrustum();
  126. }
  127. //---------------------------------------------------------------------
  128. void Camera::setFrustumOffset(float horizontal, float vertical)
  129. {
  130. setFrustumOffset(Vector2(horizontal, vertical));
  131. }
  132. //---------------------------------------------------------------------
  133. const Vector2& Camera::getFrustumOffset() const
  134. {
  135. return mFrustumOffset;
  136. }
  137. //---------------------------------------------------------------------
  138. void Camera::setFocalLength(float focalLength)
  139. {
  140. if (focalLength <= 0)
  141. {
  142. CM_EXCEPT(InvalidParametersException,
  143. "Focal length must be greater than zero.");
  144. }
  145. mFocalLength = focalLength;
  146. invalidateFrustum();
  147. }
  148. //---------------------------------------------------------------------
  149. float Camera::getFocalLength() const
  150. {
  151. return mFocalLength;
  152. }
  153. //-----------------------------------------------------------------------
  154. const Matrix4& Camera::getProjectionMatrix(void) const
  155. {
  156. updateFrustum();
  157. return mProjMatrix;
  158. }
  159. //-----------------------------------------------------------------------
  160. const Matrix4& Camera::getProjectionMatrixWithRSDepth(void) const
  161. {
  162. updateFrustum();
  163. return mProjMatrixRSDepth;
  164. }
  165. //-----------------------------------------------------------------------
  166. const Matrix4& Camera::getProjectionMatrixRS(void) const
  167. {
  168. updateFrustum();
  169. return mProjMatrixRS;
  170. }
  171. //-----------------------------------------------------------------------
  172. const Matrix4& Camera::getViewMatrix(void) const
  173. {
  174. updateView();
  175. return mViewMatrix;
  176. }
  177. //-----------------------------------------------------------------------
  178. const Plane* Camera::getFrustumPlanes(void) const
  179. {
  180. // Make any pending updates to the calculated frustum planes
  181. updateFrustumPlanes();
  182. return mFrustumPlanes;
  183. }
  184. //-----------------------------------------------------------------------
  185. const Plane& Camera::getFrustumPlane(unsigned short plane) const
  186. {
  187. // Make any pending updates to the calculated frustum planes
  188. updateFrustumPlanes();
  189. return mFrustumPlanes[plane];
  190. }
  191. //-----------------------------------------------------------------------
  192. bool Camera::isVisible(const AxisAlignedBox& bound, FrustumPlane* culledBy) const
  193. {
  194. // Null boxes always invisible
  195. if (bound.isNull()) return false;
  196. // Infinite boxes always visible
  197. if (bound.isInfinite()) return true;
  198. // Make any pending updates to the calculated frustum planes
  199. updateFrustumPlanes();
  200. // Get centre of the box
  201. Vector3 centre = bound.getCenter();
  202. // Get the half-size of the box
  203. Vector3 halfSize = bound.getHalfSize();
  204. // For each plane, see if all points are on the negative side
  205. // If so, object is not visible
  206. for (int plane = 0; plane < 6; ++plane)
  207. {
  208. // Skip far plane if infinite view frustum
  209. if (plane == FRUSTUM_PLANE_FAR && mFarDist == 0)
  210. continue;
  211. Plane::Side side = mFrustumPlanes[plane].getSide(centre, halfSize);
  212. if (side == Plane::NEGATIVE_SIDE)
  213. {
  214. // ALL corners on negative side therefore out of view
  215. if (culledBy)
  216. *culledBy = (FrustumPlane)plane;
  217. return false;
  218. }
  219. }
  220. return true;
  221. }
  222. //-----------------------------------------------------------------------
  223. bool Camera::isVisible(const Vector3& vert, FrustumPlane* culledBy) const
  224. {
  225. // Make any pending updates to the calculated frustum planes
  226. updateFrustumPlanes();
  227. // For each plane, see if all points are on the negative side
  228. // If so, object is not visible
  229. for (int plane = 0; plane < 6; ++plane)
  230. {
  231. // Skip far plane if infinite view frustum
  232. if (plane == FRUSTUM_PLANE_FAR && mFarDist == 0)
  233. continue;
  234. if (mFrustumPlanes[plane].getSide(vert) == Plane::NEGATIVE_SIDE)
  235. {
  236. // ALL corners on negative side therefore out of view
  237. if (culledBy)
  238. *culledBy = (FrustumPlane)plane;
  239. return false;
  240. }
  241. }
  242. return true;
  243. }
  244. //-----------------------------------------------------------------------
  245. bool Camera::isVisible(const Sphere& sphere, FrustumPlane* culledBy) const
  246. {
  247. // Make any pending updates to the calculated frustum planes
  248. updateFrustumPlanes();
  249. // For each plane, see if sphere is on negative side
  250. // If so, object is not visible
  251. for (int plane = 0; plane < 6; ++plane)
  252. {
  253. // Skip far plane if infinite view frustum
  254. if (plane == FRUSTUM_PLANE_FAR && mFarDist == 0)
  255. continue;
  256. // If the distance from sphere center to plane is negative, and 'more negative'
  257. // than the radius of the sphere, sphere is outside frustum
  258. if (mFrustumPlanes[plane].getDistance(sphere.getCenter()) < -sphere.getRadius())
  259. {
  260. // ALL corners on negative side therefore out of view
  261. if (culledBy)
  262. *culledBy = (FrustumPlane)plane;
  263. return false;
  264. }
  265. }
  266. return true;
  267. }
  268. //-----------------------------------------------------------------------
  269. void Camera::calcProjectionParameters(float& left, float& right, float& bottom, float& top) const
  270. {
  271. if (mCustomProjMatrix)
  272. {
  273. // Convert clipspace corners to camera space
  274. Matrix4 invProj = mProjMatrix.inverse();
  275. Vector3 topLeft(-0.5f, 0.5f, 0.0f);
  276. Vector3 bottomRight(0.5f, -0.5f, 0.0f);
  277. topLeft = invProj * topLeft;
  278. bottomRight = invProj * bottomRight;
  279. left = topLeft.x;
  280. top = topLeft.y;
  281. right = bottomRight.x;
  282. bottom = bottomRight.y;
  283. }
  284. else
  285. {
  286. if (mFrustumExtentsManuallySet)
  287. {
  288. left = mLeft;
  289. right = mRight;
  290. top = mTop;
  291. bottom = mBottom;
  292. }
  293. // Calculate general projection parameters
  294. else if (mProjType == PT_PERSPECTIVE)
  295. {
  296. Radian thetaY (mFOVy * 0.5f);
  297. float tanThetaY = Math::Tan(thetaY);
  298. float tanThetaX = tanThetaY * mAspect;
  299. float nearFocal = mNearDist / mFocalLength;
  300. float nearOffsetX = mFrustumOffset.x * nearFocal;
  301. float nearOffsetY = mFrustumOffset.y * nearFocal;
  302. float half_w = tanThetaX * mNearDist;
  303. float half_h = tanThetaY * mNearDist;
  304. left = - half_w + nearOffsetX;
  305. right = + half_w + nearOffsetX;
  306. bottom = - half_h + nearOffsetY;
  307. top = + half_h + nearOffsetY;
  308. mLeft = left;
  309. mRight = right;
  310. mTop = top;
  311. mBottom = bottom;
  312. }
  313. else
  314. {
  315. // Unknown how to apply frustum offset to orthographic camera, just ignore here
  316. float half_w = getOrthoWindowWidth() * 0.5f;
  317. float half_h = getOrthoWindowHeight() * 0.5f;
  318. left = - half_w;
  319. right = + half_w;
  320. bottom = - half_h;
  321. top = + half_h;
  322. mLeft = left;
  323. mRight = right;
  324. mTop = top;
  325. mBottom = bottom;
  326. }
  327. }
  328. }
  329. //-----------------------------------------------------------------------
  330. void Camera::updateFrustumImpl(void) const
  331. {
  332. // Common calcs
  333. float left, right, bottom, top;
  334. calcProjectionParameters(left, right, bottom, top);
  335. if (!mCustomProjMatrix)
  336. {
  337. // The code below will dealing with general projection
  338. // parameters, similar glFrustum and glOrtho.
  339. // Doesn't optimise manually except division operator, so the
  340. // code more self-explaining.
  341. float inv_w = 1 / (right - left);
  342. float inv_h = 1 / (top - bottom);
  343. float inv_d = 1 / (mFarDist - mNearDist);
  344. // Recalc if frustum params changed
  345. if (mProjType == PT_PERSPECTIVE)
  346. {
  347. // Calc matrix elements
  348. float A = 2 * mNearDist * inv_w;
  349. float B = 2 * mNearDist * inv_h;
  350. float C = (right + left) * inv_w;
  351. float D = (top + bottom) * inv_h;
  352. float q, qn;
  353. if (mFarDist == 0)
  354. {
  355. // Infinite far plane
  356. q = Camera::INFINITE_FAR_PLANE_ADJUST - 1;
  357. qn = mNearDist * (Camera::INFINITE_FAR_PLANE_ADJUST - 2);
  358. }
  359. else
  360. {
  361. q = - (mFarDist + mNearDist) * inv_d;
  362. qn = -2 * (mFarDist * mNearDist) * inv_d;
  363. }
  364. // NB: This creates 'uniform' perspective projection matrix,
  365. // which depth range [-1,1], right-handed rules
  366. //
  367. // [ A 0 C 0 ]
  368. // [ 0 B D 0 ]
  369. // [ 0 0 q qn ]
  370. // [ 0 0 -1 0 ]
  371. //
  372. // A = 2 * near / (right - left)
  373. // B = 2 * near / (top - bottom)
  374. // C = (right + left) / (right - left)
  375. // D = (top + bottom) / (top - bottom)
  376. // q = - (far + near) / (far - near)
  377. // qn = - 2 * (far * near) / (far - near)
  378. mProjMatrix = Matrix4::ZERO;
  379. mProjMatrix[0][0] = A;
  380. mProjMatrix[0][2] = C;
  381. mProjMatrix[1][1] = B;
  382. mProjMatrix[1][2] = D;
  383. mProjMatrix[2][2] = q;
  384. mProjMatrix[2][3] = qn;
  385. mProjMatrix[3][2] = -1;
  386. } // perspective
  387. else if (mProjType == PT_ORTHOGRAPHIC)
  388. {
  389. float A = 2 * inv_w;
  390. float B = 2 * inv_h;
  391. float C = - (right + left) * inv_w;
  392. float D = - (top + bottom) * inv_h;
  393. float q, qn;
  394. if (mFarDist == 0)
  395. {
  396. // Can not do infinite far plane here, avoid divided zero only
  397. q = - Camera::INFINITE_FAR_PLANE_ADJUST / mNearDist;
  398. qn = - Camera::INFINITE_FAR_PLANE_ADJUST - 1;
  399. }
  400. else
  401. {
  402. q = - 2 * inv_d;
  403. qn = - (mFarDist + mNearDist) * inv_d;
  404. }
  405. // NB: This creates 'uniform' orthographic projection matrix,
  406. // which depth range [-1,1], right-handed rules
  407. //
  408. // [ A 0 0 C ]
  409. // [ 0 B 0 D ]
  410. // [ 0 0 q qn ]
  411. // [ 0 0 0 1 ]
  412. //
  413. // A = 2 * / (right - left)
  414. // B = 2 * / (top - bottom)
  415. // C = - (right + left) / (right - left)
  416. // D = - (top + bottom) / (top - bottom)
  417. // q = - 2 / (far - near)
  418. // qn = - (far + near) / (far - near)
  419. mProjMatrix = Matrix4::ZERO;
  420. mProjMatrix[0][0] = A;
  421. mProjMatrix[0][3] = C;
  422. mProjMatrix[1][1] = B;
  423. mProjMatrix[1][3] = D;
  424. mProjMatrix[2][2] = q;
  425. mProjMatrix[2][3] = qn;
  426. mProjMatrix[3][3] = 1;
  427. } // ortho
  428. } // !mCustomProjMatrix
  429. RenderSystem* renderSystem = CamelotEngine::RenderSystem::instancePtr();
  430. // API specific
  431. renderSystem->convertProjectionMatrix(mProjMatrix, mProjMatrixRS);
  432. // API specific for Gpu Programs
  433. renderSystem->convertProjectionMatrix(mProjMatrix, mProjMatrixRSDepth, true);
  434. // Calculate bounding box (local)
  435. // Box is from 0, down -Z, max dimensions as determined from far plane
  436. // If infinite view frustum just pick a far value
  437. float farDist = (mFarDist == 0) ? 100000 : mFarDist;
  438. // Near plane bounds
  439. Vector3 min(left, bottom, -farDist);
  440. Vector3 max(right, top, 0);
  441. if (mCustomProjMatrix)
  442. {
  443. // Some custom projection matrices can have unusual inverted settings
  444. // So make sure the AABB is the right way around to start with
  445. Vector3 tmp = min;
  446. min.makeFloor(max);
  447. max.makeCeil(tmp);
  448. }
  449. if (mProjType == PT_PERSPECTIVE)
  450. {
  451. // Merge with far plane bounds
  452. float radio = farDist / mNearDist;
  453. min.makeFloor(Vector3(left * radio, bottom * radio, -farDist));
  454. max.makeCeil(Vector3(right * radio, top * radio, 0));
  455. }
  456. mBoundingBox.setExtents(min, max);
  457. mRecalcFrustum = false;
  458. // Signal to update frustum clipping planes
  459. mRecalcFrustumPlanes = true;
  460. }
  461. //-----------------------------------------------------------------------
  462. void Camera::updateFrustum(void) const
  463. {
  464. if (isFrustumOutOfDate())
  465. {
  466. updateFrustumImpl();
  467. }
  468. }
  469. //-----------------------------------------------------------------------
  470. bool Camera::isFrustumOutOfDate(void) const
  471. {
  472. return mRecalcFrustum;
  473. }
  474. //-----------------------------------------------------------------------
  475. void Camera::updateView(void) const
  476. {
  477. if (!mCustomViewMatrix)
  478. {
  479. Matrix3 rot;
  480. const Quaternion& orientation = gameObject()->getWorldRotation();
  481. const Vector3& position = gameObject()->getWorldPosition();
  482. mViewMatrix = Math::makeViewMatrix(position, orientation, 0);
  483. }
  484. }
  485. //-----------------------------------------------------------------------
  486. void Camera::updateFrustumPlanesImpl(void) const
  487. {
  488. // -------------------------
  489. // Update the frustum planes
  490. // -------------------------
  491. Matrix4 combo = mProjMatrix * mViewMatrix;
  492. mFrustumPlanes[FRUSTUM_PLANE_LEFT].normal.x = combo[3][0] + combo[0][0];
  493. mFrustumPlanes[FRUSTUM_PLANE_LEFT].normal.y = combo[3][1] + combo[0][1];
  494. mFrustumPlanes[FRUSTUM_PLANE_LEFT].normal.z = combo[3][2] + combo[0][2];
  495. mFrustumPlanes[FRUSTUM_PLANE_LEFT].d = combo[3][3] + combo[0][3];
  496. mFrustumPlanes[FRUSTUM_PLANE_RIGHT].normal.x = combo[3][0] - combo[0][0];
  497. mFrustumPlanes[FRUSTUM_PLANE_RIGHT].normal.y = combo[3][1] - combo[0][1];
  498. mFrustumPlanes[FRUSTUM_PLANE_RIGHT].normal.z = combo[3][2] - combo[0][2];
  499. mFrustumPlanes[FRUSTUM_PLANE_RIGHT].d = combo[3][3] - combo[0][3];
  500. mFrustumPlanes[FRUSTUM_PLANE_TOP].normal.x = combo[3][0] - combo[1][0];
  501. mFrustumPlanes[FRUSTUM_PLANE_TOP].normal.y = combo[3][1] - combo[1][1];
  502. mFrustumPlanes[FRUSTUM_PLANE_TOP].normal.z = combo[3][2] - combo[1][2];
  503. mFrustumPlanes[FRUSTUM_PLANE_TOP].d = combo[3][3] - combo[1][3];
  504. mFrustumPlanes[FRUSTUM_PLANE_BOTTOM].normal.x = combo[3][0] + combo[1][0];
  505. mFrustumPlanes[FRUSTUM_PLANE_BOTTOM].normal.y = combo[3][1] + combo[1][1];
  506. mFrustumPlanes[FRUSTUM_PLANE_BOTTOM].normal.z = combo[3][2] + combo[1][2];
  507. mFrustumPlanes[FRUSTUM_PLANE_BOTTOM].d = combo[3][3] + combo[1][3];
  508. mFrustumPlanes[FRUSTUM_PLANE_NEAR].normal.x = combo[3][0] + combo[2][0];
  509. mFrustumPlanes[FRUSTUM_PLANE_NEAR].normal.y = combo[3][1] + combo[2][1];
  510. mFrustumPlanes[FRUSTUM_PLANE_NEAR].normal.z = combo[3][2] + combo[2][2];
  511. mFrustumPlanes[FRUSTUM_PLANE_NEAR].d = combo[3][3] + combo[2][3];
  512. mFrustumPlanes[FRUSTUM_PLANE_FAR].normal.x = combo[3][0] - combo[2][0];
  513. mFrustumPlanes[FRUSTUM_PLANE_FAR].normal.y = combo[3][1] - combo[2][1];
  514. mFrustumPlanes[FRUSTUM_PLANE_FAR].normal.z = combo[3][2] - combo[2][2];
  515. mFrustumPlanes[FRUSTUM_PLANE_FAR].d = combo[3][3] - combo[2][3];
  516. // Renormalise any normals which were not unit length
  517. for(int i=0; i<6; i++ )
  518. {
  519. float length = mFrustumPlanes[i].normal.normalise();
  520. mFrustumPlanes[i].d /= length;
  521. }
  522. mRecalcFrustumPlanes = false;
  523. }
  524. //-----------------------------------------------------------------------
  525. void Camera::updateFrustumPlanes(void) const
  526. {
  527. updateView();
  528. updateFrustum();
  529. if (mRecalcFrustumPlanes)
  530. {
  531. updateFrustumPlanesImpl();
  532. }
  533. }
  534. //-----------------------------------------------------------------------
  535. void Camera::updateWorldSpaceCornersImpl(void) const
  536. {
  537. Matrix4 eyeToWorld = mViewMatrix.inverseAffine();
  538. // Note: Even though we can dealing with general projection matrix here,
  539. // but because it's incompatibly with infinite far plane, thus, we
  540. // still need to working with projection parameters.
  541. // Calc near plane corners
  542. float nearLeft, nearRight, nearBottom, nearTop;
  543. calcProjectionParameters(nearLeft, nearRight, nearBottom, nearTop);
  544. // Treat infinite fardist as some arbitrary far value
  545. float farDist = (mFarDist == 0) ? 100000 : mFarDist;
  546. // Calc far palne corners
  547. float radio = mProjType == PT_PERSPECTIVE ? farDist / mNearDist : 1;
  548. float farLeft = nearLeft * radio;
  549. float farRight = nearRight * radio;
  550. float farBottom = nearBottom * radio;
  551. float farTop = nearTop * radio;
  552. // near
  553. mWorldSpaceCorners[0] = eyeToWorld.transformAffine(Vector3(nearRight, nearTop, -mNearDist));
  554. mWorldSpaceCorners[1] = eyeToWorld.transformAffine(Vector3(nearLeft, nearTop, -mNearDist));
  555. mWorldSpaceCorners[2] = eyeToWorld.transformAffine(Vector3(nearLeft, nearBottom, -mNearDist));
  556. mWorldSpaceCorners[3] = eyeToWorld.transformAffine(Vector3(nearRight, nearBottom, -mNearDist));
  557. // far
  558. mWorldSpaceCorners[4] = eyeToWorld.transformAffine(Vector3(farRight, farTop, -farDist));
  559. mWorldSpaceCorners[5] = eyeToWorld.transformAffine(Vector3(farLeft, farTop, -farDist));
  560. mWorldSpaceCorners[6] = eyeToWorld.transformAffine(Vector3(farLeft, farBottom, -farDist));
  561. mWorldSpaceCorners[7] = eyeToWorld.transformAffine(Vector3(farRight, farBottom, -farDist));
  562. mRecalcWorldSpaceCorners = false;
  563. }
  564. //-----------------------------------------------------------------------
  565. void Camera::updateWorldSpaceCorners(void) const
  566. {
  567. updateView();
  568. if (mRecalcWorldSpaceCorners)
  569. {
  570. updateWorldSpaceCornersImpl();
  571. }
  572. }
  573. //-----------------------------------------------------------------------
  574. float Camera::getAspectRatio(void) const
  575. {
  576. return mAspect;
  577. }
  578. //-----------------------------------------------------------------------
  579. void Camera::setAspectRatio(float r)
  580. {
  581. mAspect = r;
  582. invalidateFrustum();
  583. }
  584. //-----------------------------------------------------------------------
  585. const AxisAlignedBox& Camera::getBoundingBox(void) const
  586. {
  587. return mBoundingBox;
  588. }
  589. // -------------------------------------------------------------------
  590. const Vector3* Camera::getWorldSpaceCorners(void) const
  591. {
  592. updateWorldSpaceCorners();
  593. return mWorldSpaceCorners;
  594. }
  595. //-----------------------------------------------------------------------
  596. void Camera::setProjectionType(ProjectionType pt)
  597. {
  598. mProjType = pt;
  599. invalidateFrustum();
  600. }
  601. //-----------------------------------------------------------------------
  602. ProjectionType Camera::getProjectionType(void) const
  603. {
  604. return mProjType;
  605. }
  606. //---------------------------------------------------------------------
  607. bool Camera::projectSphere(const Sphere& sphere,
  608. float* left, float* top, float* right, float* bottom) const
  609. {
  610. // See http://www.gamasutra.com/features/20021011/lengyel_06.htm
  611. // Transform light position into camera space
  612. updateView();
  613. Vector3 eyeSpacePos = mViewMatrix.transformAffine(sphere.getCenter());
  614. // initialise
  615. *left = *bottom = -1.0f;
  616. *right = *top = 1.0f;
  617. if (eyeSpacePos.z < 0)
  618. {
  619. updateFrustum();
  620. const Matrix4& projMatrix = getProjectionMatrix();
  621. float r = sphere.getRadius();
  622. float rsq = r * r;
  623. // early-exit
  624. if (eyeSpacePos.squaredLength() <= rsq)
  625. return false;
  626. float Lxz = Math::Sqr(eyeSpacePos.x) + Math::Sqr(eyeSpacePos.z);
  627. float Lyz = Math::Sqr(eyeSpacePos.y) + Math::Sqr(eyeSpacePos.z);
  628. // Find the tangent planes to the sphere
  629. // XZ first
  630. // calculate quadratic discriminant: b*b - 4ac
  631. // x = Nx
  632. // a = Lx^2 + Lz^2
  633. // b = -2rLx
  634. // c = r^2 - Lz^2
  635. float a = Lxz;
  636. float b = -2.0f * r * eyeSpacePos.x;
  637. float c = rsq - Math::Sqr(eyeSpacePos.z);
  638. float D = b*b - 4.0f*a*c;
  639. // two roots?
  640. if (D > 0)
  641. {
  642. float sqrootD = Math::Sqrt(D);
  643. // solve the quadratic to get the components of the normal
  644. float Nx0 = (-b + sqrootD) / (2 * a);
  645. float Nx1 = (-b - sqrootD) / (2 * a);
  646. // Derive Z from this
  647. float Nz0 = (r - Nx0 * eyeSpacePos.x) / eyeSpacePos.z;
  648. float Nz1 = (r - Nx1 * eyeSpacePos.x) / eyeSpacePos.z;
  649. // Get the point of tangency
  650. // Only consider points of tangency in front of the camera
  651. float Pz0 = (Lxz - rsq) / (eyeSpacePos.z - ((Nz0 / Nx0) * eyeSpacePos.x));
  652. if (Pz0 < 0)
  653. {
  654. // Project point onto near plane in worldspace
  655. float nearx0 = (Nz0 * mNearDist) / Nx0;
  656. // now we need to map this to viewport coords
  657. // use projection matrix since that will take into account all factors
  658. Vector3 relx0 = projMatrix * Vector3(nearx0, 0, -mNearDist);
  659. // find out whether this is a left side or right side
  660. float Px0 = -(Pz0 * Nz0) / Nx0;
  661. if (Px0 > eyeSpacePos.x)
  662. {
  663. *right = std::min(*right, relx0.x);
  664. }
  665. else
  666. {
  667. *left = std::max(*left, relx0.x);
  668. }
  669. }
  670. float Pz1 = (Lxz - rsq) / (eyeSpacePos.z - ((Nz1 / Nx1) * eyeSpacePos.x));
  671. if (Pz1 < 0)
  672. {
  673. // Project point onto near plane in worldspace
  674. float nearx1 = (Nz1 * mNearDist) / Nx1;
  675. // now we need to map this to viewport coords
  676. // use projection matrix since that will take into account all factors
  677. Vector3 relx1 = projMatrix * Vector3(nearx1, 0, -mNearDist);
  678. // find out whether this is a left side or right side
  679. float Px1 = -(Pz1 * Nz1) / Nx1;
  680. if (Px1 > eyeSpacePos.x)
  681. {
  682. *right = std::min(*right, relx1.x);
  683. }
  684. else
  685. {
  686. *left = std::max(*left, relx1.x);
  687. }
  688. }
  689. }
  690. // Now YZ
  691. // calculate quadratic discriminant: b*b - 4ac
  692. // x = Ny
  693. // a = Ly^2 + Lz^2
  694. // b = -2rLy
  695. // c = r^2 - Lz^2
  696. a = Lyz;
  697. b = -2.0f * r * eyeSpacePos.y;
  698. c = rsq - Math::Sqr(eyeSpacePos.z);
  699. D = b*b - 4.0f*a*c;
  700. // two roots?
  701. if (D > 0)
  702. {
  703. float sqrootD = Math::Sqrt(D);
  704. // solve the quadratic to get the components of the normal
  705. float Ny0 = (-b + sqrootD) / (2 * a);
  706. float Ny1 = (-b - sqrootD) / (2 * a);
  707. // Derive Z from this
  708. float Nz0 = (r - Ny0 * eyeSpacePos.y) / eyeSpacePos.z;
  709. float Nz1 = (r - Ny1 * eyeSpacePos.y) / eyeSpacePos.z;
  710. // Get the point of tangency
  711. // Only consider points of tangency in front of the camera
  712. float Pz0 = (Lyz - rsq) / (eyeSpacePos.z - ((Nz0 / Ny0) * eyeSpacePos.y));
  713. if (Pz0 < 0)
  714. {
  715. // Project point onto near plane in worldspace
  716. float neary0 = (Nz0 * mNearDist) / Ny0;
  717. // now we need to map this to viewport coords
  718. // use projection matriy since that will take into account all factors
  719. Vector3 rely0 = projMatrix * Vector3(0, neary0, -mNearDist);
  720. // find out whether this is a top side or bottom side
  721. float Py0 = -(Pz0 * Nz0) / Ny0;
  722. if (Py0 > eyeSpacePos.y)
  723. {
  724. *top = std::min(*top, rely0.y);
  725. }
  726. else
  727. {
  728. *bottom = std::max(*bottom, rely0.y);
  729. }
  730. }
  731. float Pz1 = (Lyz - rsq) / (eyeSpacePos.z - ((Nz1 / Ny1) * eyeSpacePos.y));
  732. if (Pz1 < 0)
  733. {
  734. // Project point onto near plane in worldspace
  735. float neary1 = (Nz1 * mNearDist) / Ny1;
  736. // now we need to map this to viewport coords
  737. // use projection matriy since that will take into account all factors
  738. Vector3 rely1 = projMatrix * Vector3(0, neary1, -mNearDist);
  739. // find out whether this is a top side or bottom side
  740. float Py1 = -(Pz1 * Nz1) / Ny1;
  741. if (Py1 > eyeSpacePos.y)
  742. {
  743. *top = std::min(*top, rely1.y);
  744. }
  745. else
  746. {
  747. *bottom = std::max(*bottom, rely1.y);
  748. }
  749. }
  750. }
  751. }
  752. return (*left != -1.0f) || (*top != 1.0f) || (*right != 1.0f) || (*bottom != -1.0f);
  753. }
  754. //---------------------------------------------------------------------
  755. void Camera::setCustomViewMatrix(bool enable, const Matrix4& viewMatrix)
  756. {
  757. mCustomViewMatrix = enable;
  758. if (enable)
  759. {
  760. assert(viewMatrix.isAffine());
  761. mViewMatrix = viewMatrix;
  762. }
  763. }
  764. //---------------------------------------------------------------------
  765. void Camera::setCustomProjectionMatrix(bool enable, const Matrix4& projMatrix)
  766. {
  767. mCustomProjMatrix = enable;
  768. if (enable)
  769. {
  770. mProjMatrix = projMatrix;
  771. }
  772. invalidateFrustum();
  773. }
  774. //---------------------------------------------------------------------
  775. void Camera::setOrthoWindow(float w, float h)
  776. {
  777. mOrthoHeight = h;
  778. mAspect = w / h;
  779. invalidateFrustum();
  780. }
  781. //---------------------------------------------------------------------
  782. void Camera::setOrthoWindowHeight(float h)
  783. {
  784. mOrthoHeight = h;
  785. invalidateFrustum();
  786. }
  787. //---------------------------------------------------------------------
  788. void Camera::setOrthoWindowWidth(float w)
  789. {
  790. mOrthoHeight = w / mAspect;
  791. invalidateFrustum();
  792. }
  793. //---------------------------------------------------------------------
  794. float Camera::getOrthoWindowHeight() const
  795. {
  796. return mOrthoHeight;
  797. }
  798. //---------------------------------------------------------------------
  799. float Camera::getOrthoWindowWidth() const
  800. {
  801. return mOrthoHeight * mAspect;
  802. }
  803. //---------------------------------------------------------------------
  804. void Camera::setFrustumExtents(float left, float right, float top, float bottom)
  805. {
  806. mFrustumExtentsManuallySet = true;
  807. mLeft = left;
  808. mRight = right;
  809. mTop = top;
  810. mBottom = bottom;
  811. invalidateFrustum();
  812. }
  813. //---------------------------------------------------------------------
  814. void Camera::resetFrustumExtents()
  815. {
  816. mFrustumExtentsManuallySet = false;
  817. invalidateFrustum();
  818. }
  819. //---------------------------------------------------------------------
  820. void Camera::getFrustumExtents(float& outleft, float& outright, float& outtop, float& outbottom) const
  821. {
  822. updateFrustum();
  823. outleft = mLeft;
  824. outright = mRight;
  825. outtop = mTop;
  826. outbottom = mBottom;
  827. }
  828. // -------------------------------------------------------------------
  829. void Camera::invalidateFrustum(void) const
  830. {
  831. mRecalcFrustumPlanes = true;
  832. mRecalcWorldSpaceCorners = true;
  833. mRecalcVertexData = true;
  834. }
  835. // -------------------------------------------------------------------
  836. float Camera::getBoundingRadius(void) const
  837. {
  838. // return a little bigger than the near distance
  839. // just to keep things just outside
  840. return mNearDist * 1.5f;
  841. }
  842. RTTITypeBase* Camera::getRTTIStatic()
  843. {
  844. return CameraRTTI::instance();
  845. }
  846. RTTITypeBase* Camera::getRTTI() const
  847. {
  848. return Camera::getRTTIStatic();
  849. }
  850. } // namespace CamelotEngine