OgreCamera.cpp 27 KB

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