CmCamera.cpp 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858
  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 "CmMath.h"
  26. #include "CmMatrix3.h"
  27. #include "CmAxisAlignedBox.h"
  28. #include "CmSphere.h"
  29. #include "CmException.h"
  30. #include "CmRenderSystem.h"
  31. #if CM_PLATFORM == OGRE_PLATFORM_IPHONE
  32. #include "macUtils.h"
  33. #endif
  34. namespace CamelotEngine {
  35. //-----------------------------------------------------------------------
  36. Camera::Camera(RenderTarget* target,
  37. float left, float top,
  38. float width, float height,
  39. int ZOrder)
  40. : Frustum(),
  41. mOrientation(Quaternion::IDENTITY),
  42. mPosition(Vector3::ZERO),
  43. mSceneDetail(PM_SOLID),
  44. mWindowSet(false),
  45. mLastViewport(0),
  46. mAutoAspectRatio(false),
  47. mCullFrustum(0)
  48. {
  49. // Reasonable defaults to camera params
  50. mFOVy = Radian(Math::PI/4.0f);
  51. mNearDist = 100.0f;
  52. mFarDist = 100000.0f;
  53. mAspect = 1.33333333333333f;
  54. mProjType = PT_PERSPECTIVE;
  55. setFixedYawAxis(true); // Default to fixed yaw, like freelook since most people expect this
  56. invalidateFrustum();
  57. invalidateView();
  58. // Init matrices
  59. mViewMatrix = Matrix4::ZERO;
  60. mProjMatrixRS = Matrix4::ZERO;
  61. mViewport = new Viewport(target, left, top, width, height, ZOrder);
  62. }
  63. //-----------------------------------------------------------------------
  64. Camera::~Camera()
  65. {
  66. if(mViewport != nullptr)
  67. delete mViewport;
  68. }
  69. //-----------------------------------------------------------------------
  70. void Camera::setPolygonMode(PolygonMode sd)
  71. {
  72. mSceneDetail = sd;
  73. }
  74. //-----------------------------------------------------------------------
  75. PolygonMode Camera::getPolygonMode(void) const
  76. {
  77. return mSceneDetail;
  78. }
  79. //-----------------------------------------------------------------------
  80. void Camera::setPosition(float x, float y, float z)
  81. {
  82. mPosition.x = x;
  83. mPosition.y = y;
  84. mPosition.z = z;
  85. invalidateView();
  86. }
  87. //-----------------------------------------------------------------------
  88. void Camera::setPosition(const Vector3& vec)
  89. {
  90. mPosition = vec;
  91. invalidateView();
  92. }
  93. //-----------------------------------------------------------------------
  94. const Vector3& Camera::getPosition(void) const
  95. {
  96. return mPosition;
  97. }
  98. //-----------------------------------------------------------------------
  99. void Camera::move(const Vector3& vec)
  100. {
  101. mPosition = mPosition + vec;
  102. invalidateView();
  103. }
  104. //-----------------------------------------------------------------------
  105. void Camera::moveRelative(const Vector3& vec)
  106. {
  107. // Transform the axes of the relative vector by camera's local axes
  108. Vector3 trans = mOrientation * vec;
  109. mPosition = mPosition + trans;
  110. invalidateView();
  111. }
  112. //-----------------------------------------------------------------------
  113. void Camera::setDirection(float x, float y, float z)
  114. {
  115. setDirection(Vector3(x,y,z));
  116. }
  117. //-----------------------------------------------------------------------
  118. void Camera::setDirection(const Vector3& vec)
  119. {
  120. // Do nothing if given a zero vector
  121. // (Replaced assert since this could happen with auto tracking camera and
  122. // camera passes through the lookAt point)
  123. if (vec == Vector3::ZERO) return;
  124. // Remember, camera points down -Z of local axes!
  125. // Therefore reverse direction of direction vector before determining local Z
  126. Vector3 zAdjustVec = -vec;
  127. zAdjustVec.normalise();
  128. Quaternion targetWorldOrientation;
  129. if( mYawFixed )
  130. {
  131. Vector3 xVec = mYawFixedAxis.crossProduct( zAdjustVec );
  132. xVec.normalise();
  133. Vector3 yVec = zAdjustVec.crossProduct( xVec );
  134. yVec.normalise();
  135. targetWorldOrientation.FromAxes( xVec, yVec, zAdjustVec );
  136. }
  137. else
  138. {
  139. // Get axes from current quaternion
  140. Vector3 axes[3];
  141. updateView();
  142. mRealOrientation.ToAxes(axes);
  143. Quaternion rotQuat;
  144. if ( (axes[2]+zAdjustVec).squaredLength() < 0.00005f)
  145. {
  146. // Oops, a 180 degree turn (infinite possible rotation axes)
  147. // Default to yaw i.e. use current UP
  148. rotQuat.FromAngleAxis(Radian(Math::PI), axes[1]);
  149. }
  150. else
  151. {
  152. // Derive shortest arc to new direction
  153. rotQuat = axes[2].getRotationTo(zAdjustVec);
  154. }
  155. targetWorldOrientation = rotQuat * mRealOrientation;
  156. }
  157. // transform to parent space
  158. // TODO PORT - Can't get orientation from parent until we hook it up properly as a Component
  159. // if (mParentNode)
  160. // {
  161. // mOrientation =
  162. // mParentNode->_getDerivedOrientation().Inverse() * targetWorldOrientation;
  163. // }
  164. //else
  165. {
  166. mOrientation = targetWorldOrientation;
  167. }
  168. // TODO If we have a fixed yaw axis, we mustn't break it by using the
  169. // shortest arc because this will sometimes cause a relative yaw
  170. // which will tip the camera
  171. invalidateView();
  172. }
  173. //-----------------------------------------------------------------------
  174. Vector3 Camera::getDirection(void) const
  175. {
  176. // Direction points down -Z by default
  177. return mOrientation * -Vector3::UNIT_Z;
  178. }
  179. //-----------------------------------------------------------------------
  180. Vector3 Camera::getUp(void) const
  181. {
  182. return mOrientation * Vector3::UNIT_Y;
  183. }
  184. //-----------------------------------------------------------------------
  185. Vector3 Camera::getRight(void) const
  186. {
  187. return mOrientation * Vector3::UNIT_X;
  188. }
  189. //-----------------------------------------------------------------------
  190. void Camera::lookAt(const Vector3& targetPoint)
  191. {
  192. updateView();
  193. this->setDirection(targetPoint - mRealPosition);
  194. }
  195. //-----------------------------------------------------------------------
  196. void Camera::lookAt( float x, float y, float z )
  197. {
  198. Vector3 vTemp( x, y, z );
  199. this->lookAt(vTemp);
  200. }
  201. //-----------------------------------------------------------------------
  202. void Camera::roll(const Radian& angle)
  203. {
  204. // Rotate around local Z axis
  205. Vector3 zAxis = mOrientation * Vector3::UNIT_Z;
  206. rotate(zAxis, angle);
  207. invalidateView();
  208. }
  209. //-----------------------------------------------------------------------
  210. void Camera::yaw(const Radian& angle)
  211. {
  212. Vector3 yAxis;
  213. if (mYawFixed)
  214. {
  215. // Rotate around fixed yaw axis
  216. yAxis = mYawFixedAxis;
  217. }
  218. else
  219. {
  220. // Rotate around local Y axis
  221. yAxis = mOrientation * Vector3::UNIT_Y;
  222. }
  223. rotate(yAxis, angle);
  224. invalidateView();
  225. }
  226. //-----------------------------------------------------------------------
  227. void Camera::pitch(const Radian& angle)
  228. {
  229. // Rotate around local X axis
  230. Vector3 xAxis = mOrientation * Vector3::UNIT_X;
  231. rotate(xAxis, angle);
  232. invalidateView();
  233. }
  234. //-----------------------------------------------------------------------
  235. void Camera::rotate(const Vector3& axis, const Radian& angle)
  236. {
  237. Quaternion q;
  238. q.FromAngleAxis(angle,axis);
  239. rotate(q);
  240. }
  241. //-----------------------------------------------------------------------
  242. void Camera::rotate(const Quaternion& q)
  243. {
  244. // Note the order of the mult, i.e. q comes after
  245. // Normalise the quat to avoid cumulative problems with precision
  246. Quaternion qnorm = q;
  247. qnorm.normalise();
  248. mOrientation = qnorm * mOrientation;
  249. invalidateView();
  250. }
  251. //-----------------------------------------------------------------------
  252. bool Camera::isViewOutOfDate(void) const
  253. {
  254. // Overridden from Frustum to use local orientation / position offsets
  255. // Attached to node?
  256. // TODO PORT - Can't get orientation/position from parent until we hook it up properly as a Component
  257. //if (mParentNode != 0)
  258. //{
  259. // if (mRecalcView ||
  260. // mParentNode->_getDerivedOrientation() != mLastParentOrientation ||
  261. // mParentNode->_getDerivedPosition() != mLastParentPosition)
  262. // {
  263. // // Ok, we're out of date with SceneNode we're attached to
  264. // mLastParentOrientation = mParentNode->_getDerivedOrientation();
  265. // mLastParentPosition = mParentNode->_getDerivedPosition();
  266. // mRealOrientation = mLastParentOrientation * mOrientation;
  267. // mRealPosition = (mLastParentOrientation * mPosition) + mLastParentPosition;
  268. // mRecalcView = true;
  269. // mRecalcWindow = true;
  270. // }
  271. //}
  272. //else
  273. {
  274. // Rely on own updates
  275. mRealOrientation = mOrientation;
  276. mRealPosition = mPosition;
  277. }
  278. // Deriving reflected orientation / position
  279. if (mRecalcView)
  280. {
  281. mDerivedOrientation = mRealOrientation;
  282. mDerivedPosition = mRealPosition;
  283. }
  284. return mRecalcView;
  285. }
  286. // -------------------------------------------------------------------
  287. void Camera::invalidateView() const
  288. {
  289. mRecalcWindow = true;
  290. Frustum::invalidateView();
  291. }
  292. // -------------------------------------------------------------------
  293. void Camera::invalidateFrustum(void) const
  294. {
  295. mRecalcWindow = true;
  296. Frustum::invalidateFrustum();
  297. }
  298. //-----------------------------------------------------------------------
  299. void Camera::_renderScene(Viewport *vp, bool includeOverlays)
  300. {
  301. // TODO PORT - I'm not going to be rendering the scene like this (yet), but I think I will do it eventually
  302. //mSceneMgr->_renderScene(this, vp, includeOverlays);
  303. }
  304. //-----------------------------------------------------------------------
  305. void Camera::setFixedYawAxis(bool useFixed, const Vector3& fixedAxis)
  306. {
  307. mYawFixed = useFixed;
  308. mYawFixedAxis = fixedAxis;
  309. }
  310. //-----------------------------------------------------------------------
  311. const Quaternion& Camera::getOrientation(void) const
  312. {
  313. return mOrientation;
  314. }
  315. //-----------------------------------------------------------------------
  316. void Camera::setOrientation(const Quaternion& q)
  317. {
  318. mOrientation = q;
  319. mOrientation.normalise();
  320. invalidateView();
  321. }
  322. //-----------------------------------------------------------------------
  323. const Quaternion& Camera::getDerivedOrientation(void) const
  324. {
  325. updateView();
  326. return mDerivedOrientation;
  327. }
  328. //-----------------------------------------------------------------------
  329. const Vector3& Camera::getDerivedPosition(void) const
  330. {
  331. updateView();
  332. return mDerivedPosition;
  333. }
  334. //-----------------------------------------------------------------------
  335. Vector3 Camera::getDerivedDirection(void) const
  336. {
  337. // Direction points down -Z
  338. updateView();
  339. return mDerivedOrientation * Vector3::NEGATIVE_UNIT_Z;
  340. }
  341. //-----------------------------------------------------------------------
  342. Vector3 Camera::getDerivedUp(void) const
  343. {
  344. updateView();
  345. return mDerivedOrientation * Vector3::UNIT_Y;
  346. }
  347. //-----------------------------------------------------------------------
  348. Vector3 Camera::getDerivedRight(void) const
  349. {
  350. updateView();
  351. return mDerivedOrientation * Vector3::UNIT_X;
  352. }
  353. //-----------------------------------------------------------------------
  354. const Quaternion& Camera::getRealOrientation(void) const
  355. {
  356. updateView();
  357. return mRealOrientation;
  358. }
  359. //-----------------------------------------------------------------------
  360. const Vector3& Camera::getRealPosition(void) const
  361. {
  362. updateView();
  363. return mRealPosition;
  364. }
  365. //-----------------------------------------------------------------------
  366. Vector3 Camera::getRealDirection(void) const
  367. {
  368. // Direction points down -Z
  369. updateView();
  370. return mRealOrientation * Vector3::NEGATIVE_UNIT_Z;
  371. }
  372. //-----------------------------------------------------------------------
  373. Vector3 Camera::getRealUp(void) const
  374. {
  375. updateView();
  376. return mRealOrientation * Vector3::UNIT_Y;
  377. }
  378. //-----------------------------------------------------------------------
  379. Vector3 Camera::getRealRight(void) const
  380. {
  381. updateView();
  382. return mRealOrientation * Vector3::UNIT_X;
  383. }
  384. //-----------------------------------------------------------------------
  385. Ray Camera::getCameraToViewportRay(float screenX, float screenY) const
  386. {
  387. Ray ret;
  388. getCameraToViewportRay(screenX, screenY, &ret);
  389. return ret;
  390. }
  391. //---------------------------------------------------------------------
  392. void Camera::getCameraToViewportRay(float screenX, float screenY, Ray* outRay) const
  393. {
  394. Matrix4 inverseVP = (getProjectionMatrix() * getViewMatrix(true)).inverse();
  395. float nx = (2.0f * screenX) - 1.0f;
  396. float ny = 1.0f - (2.0f * screenY);
  397. Vector3 nearPoint(nx, ny, -1.f);
  398. // Use midPoint rather than far point to avoid issues with infinite projection
  399. Vector3 midPoint (nx, ny, 0.0f);
  400. // Get ray origin and ray target on near plane in world space
  401. Vector3 rayOrigin, rayTarget;
  402. rayOrigin = inverseVP * nearPoint;
  403. rayTarget = inverseVP * midPoint;
  404. Vector3 rayDirection = rayTarget - rayOrigin;
  405. rayDirection.normalise();
  406. outRay->setOrigin(rayOrigin);
  407. outRay->setDirection(rayDirection);
  408. }
  409. // -------------------------------------------------------------------
  410. void Camera::setWindow (float Left, float Top, float Right, float Bottom)
  411. {
  412. mWLeft = Left;
  413. mWTop = Top;
  414. mWRight = Right;
  415. mWBottom = Bottom;
  416. mWindowSet = true;
  417. mRecalcWindow = true;
  418. }
  419. // -------------------------------------------------------------------
  420. void Camera::resetWindow ()
  421. {
  422. mWindowSet = false;
  423. }
  424. // -------------------------------------------------------------------
  425. void Camera::setWindowImpl() const
  426. {
  427. if (!mWindowSet || !mRecalcWindow)
  428. return;
  429. // Calculate general projection parameters
  430. float vpLeft, vpRight, vpBottom, vpTop;
  431. calcProjectionParameters(vpLeft, vpRight, vpBottom, vpTop);
  432. float vpWidth = vpRight - vpLeft;
  433. float vpHeight = vpTop - vpBottom;
  434. float wvpLeft = vpLeft + mWLeft * vpWidth;
  435. float wvpRight = vpLeft + mWRight * vpWidth;
  436. float wvpTop = vpTop - mWTop * vpHeight;
  437. float wvpBottom = vpTop - mWBottom * vpHeight;
  438. Vector3 vp_ul (wvpLeft, wvpTop, -mNearDist);
  439. Vector3 vp_ur (wvpRight, wvpTop, -mNearDist);
  440. Vector3 vp_bl (wvpLeft, wvpBottom, -mNearDist);
  441. Vector3 vp_br (wvpRight, wvpBottom, -mNearDist);
  442. Matrix4 inv = mViewMatrix.inverseAffine();
  443. Vector3 vw_ul = inv.transformAffine(vp_ul);
  444. Vector3 vw_ur = inv.transformAffine(vp_ur);
  445. Vector3 vw_bl = inv.transformAffine(vp_bl);
  446. Vector3 vw_br = inv.transformAffine(vp_br);
  447. mWindowClipPlanes.clear();
  448. if (mProjType == PT_PERSPECTIVE)
  449. {
  450. Vector3 position = getPositionForViewUpdate();
  451. mWindowClipPlanes.push_back(Plane(position, vw_bl, vw_ul));
  452. mWindowClipPlanes.push_back(Plane(position, vw_ul, vw_ur));
  453. mWindowClipPlanes.push_back(Plane(position, vw_ur, vw_br));
  454. mWindowClipPlanes.push_back(Plane(position, vw_br, vw_bl));
  455. }
  456. else
  457. {
  458. Vector3 x_axis(inv[0][0], inv[0][1], inv[0][2]);
  459. Vector3 y_axis(inv[1][0], inv[1][1], inv[1][2]);
  460. x_axis.normalise();
  461. y_axis.normalise();
  462. mWindowClipPlanes.push_back(Plane( x_axis, vw_bl));
  463. mWindowClipPlanes.push_back(Plane(-x_axis, vw_ur));
  464. mWindowClipPlanes.push_back(Plane( y_axis, vw_bl));
  465. mWindowClipPlanes.push_back(Plane(-y_axis, vw_ur));
  466. }
  467. mRecalcWindow = false;
  468. }
  469. // -------------------------------------------------------------------
  470. const vector<Plane>::type& Camera::getWindowPlanes(void) const
  471. {
  472. updateView();
  473. setWindowImpl();
  474. return mWindowClipPlanes;
  475. }
  476. // -------------------------------------------------------------------
  477. float Camera::getBoundingRadius(void) const
  478. {
  479. // return a little bigger than the near distance
  480. // just to keep things just outside
  481. return mNearDist * 1.5f;
  482. }
  483. //-----------------------------------------------------------------------
  484. const Vector3& Camera::getPositionForViewUpdate(void) const
  485. {
  486. // Note no update, because we're calling this from the update!
  487. return mRealPosition;
  488. }
  489. //-----------------------------------------------------------------------
  490. const Quaternion& Camera::getOrientationForViewUpdate(void) const
  491. {
  492. return mRealOrientation;
  493. }
  494. //-----------------------------------------------------------------------
  495. bool Camera::getAutoAspectRatio(void) const
  496. {
  497. return mAutoAspectRatio;
  498. }
  499. //-----------------------------------------------------------------------
  500. void Camera::setAutoAspectRatio(bool autoratio)
  501. {
  502. mAutoAspectRatio = autoratio;
  503. }
  504. //-----------------------------------------------------------------------
  505. bool Camera::isVisible(const AxisAlignedBox& bound, FrustumPlane* culledBy) const
  506. {
  507. if (mCullFrustum)
  508. {
  509. return mCullFrustum->isVisible(bound, culledBy);
  510. }
  511. else
  512. {
  513. return Frustum::isVisible(bound, culledBy);
  514. }
  515. }
  516. //-----------------------------------------------------------------------
  517. bool Camera::isVisible(const Sphere& bound, FrustumPlane* culledBy) const
  518. {
  519. if (mCullFrustum)
  520. {
  521. return mCullFrustum->isVisible(bound, culledBy);
  522. }
  523. else
  524. {
  525. return Frustum::isVisible(bound, culledBy);
  526. }
  527. }
  528. //-----------------------------------------------------------------------
  529. bool Camera::isVisible(const Vector3& vert, FrustumPlane* culledBy) const
  530. {
  531. if (mCullFrustum)
  532. {
  533. return mCullFrustum->isVisible(vert, culledBy);
  534. }
  535. else
  536. {
  537. return Frustum::isVisible(vert, culledBy);
  538. }
  539. }
  540. //-----------------------------------------------------------------------
  541. const Vector3* Camera::getWorldSpaceCorners(void) const
  542. {
  543. if (mCullFrustum)
  544. {
  545. return mCullFrustum->getWorldSpaceCorners();
  546. }
  547. else
  548. {
  549. return Frustum::getWorldSpaceCorners();
  550. }
  551. }
  552. //-----------------------------------------------------------------------
  553. const Plane& Camera::getFrustumPlane( unsigned short plane ) const
  554. {
  555. if (mCullFrustum)
  556. {
  557. return mCullFrustum->getFrustumPlane(plane);
  558. }
  559. else
  560. {
  561. return Frustum::getFrustumPlane(plane);
  562. }
  563. }
  564. //-----------------------------------------------------------------------
  565. bool Camera::projectSphere(const Sphere& sphere,
  566. float* left, float* top, float* right, float* bottom) const
  567. {
  568. if (mCullFrustum)
  569. {
  570. return mCullFrustum->projectSphere(sphere, left, top, right, bottom);
  571. }
  572. else
  573. {
  574. return Frustum::projectSphere(sphere, left, top, right, bottom);
  575. }
  576. }
  577. //-----------------------------------------------------------------------
  578. float Camera::getNearClipDistance(void) const
  579. {
  580. if (mCullFrustum)
  581. {
  582. return mCullFrustum->getNearClipDistance();
  583. }
  584. else
  585. {
  586. return Frustum::getNearClipDistance();
  587. }
  588. }
  589. //-----------------------------------------------------------------------
  590. float Camera::getFarClipDistance(void) const
  591. {
  592. if (mCullFrustum)
  593. {
  594. return mCullFrustum->getFarClipDistance();
  595. }
  596. else
  597. {
  598. return Frustum::getFarClipDistance();
  599. }
  600. }
  601. //-----------------------------------------------------------------------
  602. const Matrix4& Camera::getViewMatrix(void) const
  603. {
  604. if (mCullFrustum)
  605. {
  606. return mCullFrustum->getViewMatrix();
  607. }
  608. else
  609. {
  610. return Frustum::getViewMatrix();
  611. }
  612. }
  613. //-----------------------------------------------------------------------
  614. const Matrix4& Camera::getViewMatrix(bool ownFrustumOnly) const
  615. {
  616. if (ownFrustumOnly)
  617. {
  618. return Frustum::getViewMatrix();
  619. }
  620. else
  621. {
  622. return getViewMatrix();
  623. }
  624. }
  625. //-----------------------------------------------------------------------
  626. //_______________________________________________________
  627. //| |
  628. //| getRayForwardIntersect |
  629. //| ----------------------------- |
  630. //| get the intersections of frustum rays with a plane |
  631. //| of interest. The plane is assumed to have constant |
  632. //| z. If this is not the case, rays |
  633. //| should be rotated beforehand to work in a |
  634. //| coordinate system in which this is true. |
  635. //|_____________________________________________________|
  636. //
  637. vector<Vector4>::type Camera::getRayForwardIntersect(const Vector3& anchor, const Vector3 *dir, float planeOffset) const
  638. {
  639. vector<Vector4>::type res;
  640. if(!dir)
  641. return res;
  642. int infpt[4] = {0, 0, 0, 0}; // 0=finite, 1=infinite, 2=straddles infinity
  643. Vector3 vec[4];
  644. // find how much the anchor point must be displaced in the plane's
  645. // constant variable
  646. float delta = planeOffset - anchor.z;
  647. // now set the intersection point and note whether it is a
  648. // point at infinity or straddles infinity
  649. unsigned int i;
  650. for (i=0; i<4; i++)
  651. {
  652. float test = dir[i].z * delta;
  653. if (test == 0.0) {
  654. vec[i] = dir[i];
  655. infpt[i] = 1;
  656. }
  657. else {
  658. float lambda = delta / dir[i].z;
  659. vec[i] = anchor + (lambda * dir[i]);
  660. if(test < 0.0)
  661. infpt[i] = 2;
  662. }
  663. }
  664. for (i=0; i<4; i++)
  665. {
  666. // store the finite intersection points
  667. if (infpt[i] == 0)
  668. res.push_back(Vector4(vec[i].x, vec[i].y, vec[i].z, 1.0));
  669. else
  670. {
  671. // handle the infinite points of intersection;
  672. // cases split up into the possible frustum planes
  673. // pieces which may contain a finite intersection point
  674. int nextind = (i+1) % 4;
  675. int prevind = (i+3) % 4;
  676. if ((infpt[prevind] == 0) || (infpt[nextind] == 0))
  677. {
  678. if (infpt[i] == 1)
  679. res.push_back(Vector4(vec[i].x, vec[i].y, vec[i].z, 0.0));
  680. else
  681. {
  682. // handle the intersection points that straddle infinity (back-project)
  683. if(infpt[prevind] == 0)
  684. {
  685. Vector3 temp = vec[prevind] - vec[i];
  686. res.push_back(Vector4(temp.x, temp.y, temp.z, 0.0));
  687. }
  688. if(infpt[nextind] == 0)
  689. {
  690. Vector3 temp = vec[nextind] - vec[i];
  691. res.push_back(Vector4(temp.x, temp.y, temp.z, 0.0));
  692. }
  693. }
  694. } // end if we need to add an intersection point to the list
  695. } // end if infinite point needs to be considered
  696. } // end loop over frustun corners
  697. // we end up with either 0, 3, 4, or 5 intersection points
  698. return res;
  699. }
  700. //_______________________________________________________
  701. //| |
  702. //| forwardIntersect |
  703. //| ----------------------------- |
  704. //| Forward intersect the camera's frustum rays with |
  705. //| a specified plane of interest. |
  706. //| Note that if the frustum rays shoot out and would |
  707. //| back project onto the plane, this means the forward |
  708. //| intersection of the frustum would occur at the |
  709. //| line at infinity. |
  710. //|_____________________________________________________|
  711. //
  712. void Camera::forwardIntersect(const Plane& worldPlane, vector<Vector4>::type* intersect3d) const
  713. {
  714. if(!intersect3d)
  715. return;
  716. Vector3 trCorner = getWorldSpaceCorners()[0];
  717. Vector3 tlCorner = getWorldSpaceCorners()[1];
  718. Vector3 blCorner = getWorldSpaceCorners()[2];
  719. Vector3 brCorner = getWorldSpaceCorners()[3];
  720. // need some sort of rotation that will bring the plane normal to the z axis
  721. Plane pval = worldPlane;
  722. if(pval.normal.z < 0.0)
  723. {
  724. pval.normal *= -1.0;
  725. pval.d *= -1.0;
  726. }
  727. Quaternion invPlaneRot = pval.normal.getRotationTo(Vector3::UNIT_Z);
  728. // get rotated light
  729. Vector3 lPos = invPlaneRot * getDerivedPosition();
  730. Vector3 vec[4];
  731. vec[0] = invPlaneRot * trCorner - lPos;
  732. vec[1] = invPlaneRot * tlCorner - lPos;
  733. vec[2] = invPlaneRot * blCorner - lPos;
  734. vec[3] = invPlaneRot * brCorner - lPos;
  735. // compute intersection points on plane
  736. vector<Vector4>::type iPnt = getRayForwardIntersect(lPos, vec, -pval.d);
  737. // return wanted data
  738. if(intersect3d)
  739. {
  740. Quaternion planeRot = invPlaneRot.Inverse();
  741. (*intersect3d).clear();
  742. for(unsigned int i=0; i<iPnt.size(); i++)
  743. {
  744. Vector3 intersection = planeRot * Vector3(iPnt[i].x, iPnt[i].y, iPnt[i].z);
  745. (*intersect3d).push_back(Vector4(intersection.x, intersection.y, intersection.z, iPnt[i].w));
  746. }
  747. }
  748. }
  749. //-----------------------------------------------------------------------
  750. void Camera::synchroniseBaseSettingsWith(const Camera* cam)
  751. {
  752. this->setPosition(cam->getPosition());
  753. this->setProjectionType(cam->getProjectionType());
  754. this->setOrientation(cam->getOrientation());
  755. this->setAspectRatio(cam->getAspectRatio());
  756. this->setNearClipDistance(cam->getNearClipDistance());
  757. this->setFarClipDistance(cam->getFarClipDistance());
  758. this->setFOVy(cam->getFOVy());
  759. this->setFocalLength(cam->getFocalLength());
  760. // Don't do these, they're not base settings and can cause referencing issues
  761. //this->setLodCamera(cam->getLodCamera());
  762. //this->setCullingFrustum(cam->getCullingFrustum());
  763. }
  764. } // namespace CamelotEngine