2
0

CmCamera.cpp 29 KB

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