CmCamera.cpp 30 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 "CmSceneObject.h"
  37. namespace CamelotEngine {
  38. const float Camera::INFINITE_FAR_PLANE_ADJUST = 0.00001f;
  39. //-----------------------------------------------------------------------
  40. Camera::Camera(const HSceneObject& 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. target->waitUntilInitialized();
  80. mViewport = ViewportPtr(CM_NEW(Viewport, PoolAlloc) Viewport(target, left, top, width, height, ZOrder),
  81. &MemAllocDeleter<Viewport, PoolAlloc>::deleter);
  82. }
  83. //-----------------------------------------------------------------------
  84. void Camera::setFOVy(const Radian& fov)
  85. {
  86. mFOVy = fov;
  87. invalidateFrustum();
  88. }
  89. //-----------------------------------------------------------------------
  90. const Radian& Camera::getFOVy(void) const
  91. {
  92. return mFOVy;
  93. }
  94. //-----------------------------------------------------------------------
  95. void Camera::setFarClipDistance(float farPlane)
  96. {
  97. mFarDist = farPlane;
  98. invalidateFrustum();
  99. }
  100. //-----------------------------------------------------------------------
  101. float Camera::getFarClipDistance(void) const
  102. {
  103. return mFarDist;
  104. }
  105. //-----------------------------------------------------------------------
  106. void Camera::setNearClipDistance(float nearPlane)
  107. {
  108. if (nearPlane <= 0)
  109. {
  110. CM_EXCEPT(InvalidParametersException, "Near clip distance must be greater than zero.");
  111. }
  112. mNearDist = nearPlane;
  113. invalidateFrustum();
  114. }
  115. //-----------------------------------------------------------------------
  116. float Camera::getNearClipDistance(void) const
  117. {
  118. return mNearDist;
  119. }
  120. //---------------------------------------------------------------------
  121. void Camera::setFrustumOffset(const Vector2& offset)
  122. {
  123. mFrustumOffset = offset;
  124. invalidateFrustum();
  125. }
  126. //---------------------------------------------------------------------
  127. void Camera::setFrustumOffset(float horizontal, float vertical)
  128. {
  129. setFrustumOffset(Vector2(horizontal, vertical));
  130. }
  131. //---------------------------------------------------------------------
  132. const Vector2& Camera::getFrustumOffset() const
  133. {
  134. return mFrustumOffset;
  135. }
  136. //---------------------------------------------------------------------
  137. void Camera::setFocalLength(float focalLength)
  138. {
  139. if (focalLength <= 0)
  140. {
  141. CM_EXCEPT(InvalidParametersException,
  142. "Focal length must be greater than zero.");
  143. }
  144. mFocalLength = focalLength;
  145. invalidateFrustum();
  146. }
  147. //---------------------------------------------------------------------
  148. float Camera::getFocalLength() const
  149. {
  150. return mFocalLength;
  151. }
  152. //-----------------------------------------------------------------------
  153. const Matrix4& Camera::getProjectionMatrix(void) const
  154. {
  155. updateFrustum();
  156. return mProjMatrix;
  157. }
  158. //-----------------------------------------------------------------------
  159. const Matrix4& Camera::getProjectionMatrixWithRSDepth(void) const
  160. {
  161. updateFrustum();
  162. return mProjMatrixRSDepth;
  163. }
  164. //-----------------------------------------------------------------------
  165. const Matrix4& Camera::getProjectionMatrixRS(void) const
  166. {
  167. updateFrustum();
  168. return mProjMatrixRS;
  169. }
  170. //-----------------------------------------------------------------------
  171. const Matrix4& Camera::getViewMatrix(void) const
  172. {
  173. updateView();
  174. return mViewMatrix;
  175. }
  176. //-----------------------------------------------------------------------
  177. const Plane* Camera::getFrustumPlanes(void) const
  178. {
  179. // Make any pending updates to the calculated frustum planes
  180. updateFrustumPlanes();
  181. return mFrustumPlanes;
  182. }
  183. //-----------------------------------------------------------------------
  184. const Plane& Camera::getFrustumPlane(unsigned short plane) const
  185. {
  186. // Make any pending updates to the calculated frustum planes
  187. updateFrustumPlanes();
  188. return mFrustumPlanes[plane];
  189. }
  190. //-----------------------------------------------------------------------
  191. bool Camera::isVisible(const AxisAlignedBox& bound, FrustumPlane* culledBy) const
  192. {
  193. // Null boxes always invisible
  194. if (bound.isNull()) return false;
  195. // Infinite boxes always visible
  196. if (bound.isInfinite()) return true;
  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 (mFOVy * 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 = CamelotEngine::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.makeFloor(max);
  446. max.makeCeil(tmp);
  447. }
  448. if (mProjType == PT_PERSPECTIVE)
  449. {
  450. // Merge with far plane bounds
  451. float radio = farDist / mNearDist;
  452. min.makeFloor(Vector3(left * radio, bottom * radio, -farDist));
  453. max.makeCeil(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 AxisAlignedBox& 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. bool Camera::projectSphere(const Sphere& sphere,
  607. float* left, float* top, float* right, float* bottom) const
  608. {
  609. // See http://www.gamasutra.com/features/20021011/lengyel_06.htm
  610. // Transform light position into camera space
  611. updateView();
  612. Vector3 eyeSpacePos = mViewMatrix.transformAffine(sphere.getCenter());
  613. // initialise
  614. *left = *bottom = -1.0f;
  615. *right = *top = 1.0f;
  616. if (eyeSpacePos.z < 0)
  617. {
  618. updateFrustum();
  619. const Matrix4& projMatrix = getProjectionMatrix();
  620. float r = sphere.getRadius();
  621. float rsq = r * r;
  622. // early-exit
  623. if (eyeSpacePos.squaredLength() <= rsq)
  624. return false;
  625. float Lxz = Math::Sqr(eyeSpacePos.x) + Math::Sqr(eyeSpacePos.z);
  626. float Lyz = Math::Sqr(eyeSpacePos.y) + Math::Sqr(eyeSpacePos.z);
  627. // Find the tangent planes to the sphere
  628. // XZ first
  629. // calculate quadratic discriminant: b*b - 4ac
  630. // x = Nx
  631. // a = Lx^2 + Lz^2
  632. // b = -2rLx
  633. // c = r^2 - Lz^2
  634. float a = Lxz;
  635. float b = -2.0f * r * eyeSpacePos.x;
  636. float c = rsq - Math::Sqr(eyeSpacePos.z);
  637. float D = b*b - 4.0f*a*c;
  638. // two roots?
  639. if (D > 0)
  640. {
  641. float sqrootD = Math::Sqrt(D);
  642. // solve the quadratic to get the components of the normal
  643. float Nx0 = (-b + sqrootD) / (2 * a);
  644. float Nx1 = (-b - sqrootD) / (2 * a);
  645. // Derive Z from this
  646. float Nz0 = (r - Nx0 * eyeSpacePos.x) / eyeSpacePos.z;
  647. float Nz1 = (r - Nx1 * eyeSpacePos.x) / eyeSpacePos.z;
  648. // Get the point of tangency
  649. // Only consider points of tangency in front of the camera
  650. float Pz0 = (Lxz - rsq) / (eyeSpacePos.z - ((Nz0 / Nx0) * eyeSpacePos.x));
  651. if (Pz0 < 0)
  652. {
  653. // Project point onto near plane in worldspace
  654. float nearx0 = (Nz0 * mNearDist) / Nx0;
  655. // now we need to map this to viewport coords
  656. // use projection matrix since that will take into account all factors
  657. Vector3 relx0 = projMatrix * Vector3(nearx0, 0, -mNearDist);
  658. // find out whether this is a left side or right side
  659. float Px0 = -(Pz0 * Nz0) / Nx0;
  660. if (Px0 > eyeSpacePos.x)
  661. {
  662. *right = std::min(*right, relx0.x);
  663. }
  664. else
  665. {
  666. *left = std::max(*left, relx0.x);
  667. }
  668. }
  669. float Pz1 = (Lxz - rsq) / (eyeSpacePos.z - ((Nz1 / Nx1) * eyeSpacePos.x));
  670. if (Pz1 < 0)
  671. {
  672. // Project point onto near plane in worldspace
  673. float nearx1 = (Nz1 * mNearDist) / Nx1;
  674. // now we need to map this to viewport coords
  675. // use projection matrix since that will take into account all factors
  676. Vector3 relx1 = projMatrix * Vector3(nearx1, 0, -mNearDist);
  677. // find out whether this is a left side or right side
  678. float Px1 = -(Pz1 * Nz1) / Nx1;
  679. if (Px1 > eyeSpacePos.x)
  680. {
  681. *right = std::min(*right, relx1.x);
  682. }
  683. else
  684. {
  685. *left = std::max(*left, relx1.x);
  686. }
  687. }
  688. }
  689. // Now YZ
  690. // calculate quadratic discriminant: b*b - 4ac
  691. // x = Ny
  692. // a = Ly^2 + Lz^2
  693. // b = -2rLy
  694. // c = r^2 - Lz^2
  695. a = Lyz;
  696. b = -2.0f * r * eyeSpacePos.y;
  697. c = rsq - Math::Sqr(eyeSpacePos.z);
  698. D = b*b - 4.0f*a*c;
  699. // two roots?
  700. if (D > 0)
  701. {
  702. float sqrootD = Math::Sqrt(D);
  703. // solve the quadratic to get the components of the normal
  704. float Ny0 = (-b + sqrootD) / (2 * a);
  705. float Ny1 = (-b - sqrootD) / (2 * a);
  706. // Derive Z from this
  707. float Nz0 = (r - Ny0 * eyeSpacePos.y) / eyeSpacePos.z;
  708. float Nz1 = (r - Ny1 * eyeSpacePos.y) / eyeSpacePos.z;
  709. // Get the point of tangency
  710. // Only consider points of tangency in front of the camera
  711. float Pz0 = (Lyz - rsq) / (eyeSpacePos.z - ((Nz0 / Ny0) * eyeSpacePos.y));
  712. if (Pz0 < 0)
  713. {
  714. // Project point onto near plane in worldspace
  715. float neary0 = (Nz0 * mNearDist) / Ny0;
  716. // now we need to map this to viewport coords
  717. // use projection matriy since that will take into account all factors
  718. Vector3 rely0 = projMatrix * Vector3(0, neary0, -mNearDist);
  719. // find out whether this is a top side or bottom side
  720. float Py0 = -(Pz0 * Nz0) / Ny0;
  721. if (Py0 > eyeSpacePos.y)
  722. {
  723. *top = std::min(*top, rely0.y);
  724. }
  725. else
  726. {
  727. *bottom = std::max(*bottom, rely0.y);
  728. }
  729. }
  730. float Pz1 = (Lyz - rsq) / (eyeSpacePos.z - ((Nz1 / Ny1) * eyeSpacePos.y));
  731. if (Pz1 < 0)
  732. {
  733. // Project point onto near plane in worldspace
  734. float neary1 = (Nz1 * mNearDist) / Ny1;
  735. // now we need to map this to viewport coords
  736. // use projection matriy since that will take into account all factors
  737. Vector3 rely1 = projMatrix * Vector3(0, neary1, -mNearDist);
  738. // find out whether this is a top side or bottom side
  739. float Py1 = -(Pz1 * Nz1) / Ny1;
  740. if (Py1 > eyeSpacePos.y)
  741. {
  742. *top = std::min(*top, rely1.y);
  743. }
  744. else
  745. {
  746. *bottom = std::max(*bottom, rely1.y);
  747. }
  748. }
  749. }
  750. }
  751. return (*left != -1.0f) || (*top != 1.0f) || (*right != 1.0f) || (*bottom != -1.0f);
  752. }
  753. //---------------------------------------------------------------------
  754. void Camera::setCustomViewMatrix(bool enable, const Matrix4& viewMatrix)
  755. {
  756. mCustomViewMatrix = enable;
  757. if (enable)
  758. {
  759. assert(viewMatrix.isAffine());
  760. mViewMatrix = viewMatrix;
  761. }
  762. }
  763. //---------------------------------------------------------------------
  764. void Camera::setCustomProjectionMatrix(bool enable, const Matrix4& projMatrix)
  765. {
  766. mCustomProjMatrix = enable;
  767. if (enable)
  768. {
  769. mProjMatrix = projMatrix;
  770. }
  771. invalidateFrustum();
  772. }
  773. //---------------------------------------------------------------------
  774. void Camera::setOrthoWindow(float w, float h)
  775. {
  776. mOrthoHeight = h;
  777. mAspect = w / h;
  778. invalidateFrustum();
  779. }
  780. //---------------------------------------------------------------------
  781. void Camera::setOrthoWindowHeight(float h)
  782. {
  783. mOrthoHeight = h;
  784. invalidateFrustum();
  785. }
  786. //---------------------------------------------------------------------
  787. void Camera::setOrthoWindowWidth(float w)
  788. {
  789. mOrthoHeight = w / mAspect;
  790. invalidateFrustum();
  791. }
  792. //---------------------------------------------------------------------
  793. float Camera::getOrthoWindowHeight() const
  794. {
  795. return mOrthoHeight;
  796. }
  797. //---------------------------------------------------------------------
  798. float Camera::getOrthoWindowWidth() const
  799. {
  800. return mOrthoHeight * mAspect;
  801. }
  802. //---------------------------------------------------------------------
  803. void Camera::setFrustumExtents(float left, float right, float top, float bottom)
  804. {
  805. mFrustumExtentsManuallySet = true;
  806. mLeft = left;
  807. mRight = right;
  808. mTop = top;
  809. mBottom = bottom;
  810. invalidateFrustum();
  811. }
  812. //---------------------------------------------------------------------
  813. void Camera::resetFrustumExtents()
  814. {
  815. mFrustumExtentsManuallySet = false;
  816. invalidateFrustum();
  817. }
  818. //---------------------------------------------------------------------
  819. void Camera::getFrustumExtents(float& outleft, float& outright, float& outtop, float& outbottom) const
  820. {
  821. updateFrustum();
  822. outleft = mLeft;
  823. outright = mRight;
  824. outtop = mTop;
  825. outbottom = mBottom;
  826. }
  827. // -------------------------------------------------------------------
  828. void Camera::invalidateFrustum(void) const
  829. {
  830. mRecalcFrustumPlanes = true;
  831. mRecalcWorldSpaceCorners = true;
  832. mRecalcVertexData = true;
  833. }
  834. // -------------------------------------------------------------------
  835. float Camera::getBoundingRadius(void) const
  836. {
  837. // return a little bigger than the near distance
  838. // just to keep things just outside
  839. return mNearDist * 1.5f;
  840. }
  841. RTTITypeBase* Camera::getRTTIStatic()
  842. {
  843. return CameraRTTI::instance();
  844. }
  845. RTTITypeBase* Camera::getRTTI() const
  846. {
  847. return Camera::getRTTIStatic();
  848. }
  849. } // namespace CamelotEngine