OgreCamera.cpp 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859
  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 "OgreMath.h"
  26. #include "OgreMatrix3.h"
  27. #include "OgreAxisAlignedBox.h"
  28. #include "OgreSphere.h"
  29. #include "OgreException.h"
  30. #include "OgreRenderSystem.h"
  31. #if CM_PLATFORM == OGRE_PLATFORM_IPHONE
  32. #include "macUtils.h"
  33. #endif
  34. namespace Ogre {
  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(Real x, Real y, Real 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(Real x, Real y, Real 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( Real x, Real y, Real 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(Real screenX, Real screenY) const
  380. {
  381. Ray ret;
  382. getCameraToViewportRay(screenX, screenY, &ret);
  383. return ret;
  384. }
  385. //---------------------------------------------------------------------
  386. void Camera::getCameraToViewportRay(Real screenX, Real screenY, Ray* outRay) const
  387. {
  388. Matrix4 inverseVP = (getProjectionMatrix() * getViewMatrix(true)).inverse();
  389. #if OGRE_NO_VIEWPORT_ORIENTATIONMODE == 0
  390. // We need to convert screen point to our oriented viewport (temp solution)
  391. Real tX = screenX; Real a = getOrientationMode() * Math::HALF_PI;
  392. screenX = Math::Cos(a) * (tX-0.5f) + Math::Sin(a) * (screenY-0.5f) + 0.5f;
  393. screenY = Math::Sin(a) * (tX-0.5f) + Math::Cos(a) * (screenY-0.5f) + 0.5f;
  394. if ((int)getOrientationMode()&1) screenY = 1.f - screenY;
  395. #endif
  396. Real nx = (2.0f * screenX) - 1.0f;
  397. Real ny = 1.0f - (2.0f * screenY);
  398. Vector3 nearPoint(nx, ny, -1.f);
  399. // Use midPoint rather than far point to avoid issues with infinite projection
  400. Vector3 midPoint (nx, ny, 0.0f);
  401. // Get ray origin and ray target on near plane in world space
  402. Vector3 rayOrigin, rayTarget;
  403. rayOrigin = inverseVP * nearPoint;
  404. rayTarget = inverseVP * midPoint;
  405. Vector3 rayDirection = rayTarget - rayOrigin;
  406. rayDirection.normalise();
  407. outRay->setOrigin(rayOrigin);
  408. outRay->setDirection(rayDirection);
  409. }
  410. // -------------------------------------------------------------------
  411. void Camera::setWindow (Real Left, Real Top, Real Right, Real Bottom)
  412. {
  413. mWLeft = Left;
  414. mWTop = Top;
  415. mWRight = Right;
  416. mWBottom = Bottom;
  417. mWindowSet = true;
  418. mRecalcWindow = true;
  419. }
  420. // -------------------------------------------------------------------
  421. void Camera::resetWindow ()
  422. {
  423. mWindowSet = false;
  424. }
  425. // -------------------------------------------------------------------
  426. void Camera::setWindowImpl() const
  427. {
  428. if (!mWindowSet || !mRecalcWindow)
  429. return;
  430. // Calculate general projection parameters
  431. Real vpLeft, vpRight, vpBottom, vpTop;
  432. calcProjectionParameters(vpLeft, vpRight, vpBottom, vpTop);
  433. Real vpWidth = vpRight - vpLeft;
  434. Real vpHeight = vpTop - vpBottom;
  435. Real wvpLeft = vpLeft + mWLeft * vpWidth;
  436. Real wvpRight = vpLeft + mWRight * vpWidth;
  437. Real wvpTop = vpTop - mWTop * vpHeight;
  438. Real wvpBottom = vpTop - mWBottom * vpHeight;
  439. Vector3 vp_ul (wvpLeft, wvpTop, -mNearDist);
  440. Vector3 vp_ur (wvpRight, wvpTop, -mNearDist);
  441. Vector3 vp_bl (wvpLeft, wvpBottom, -mNearDist);
  442. Vector3 vp_br (wvpRight, wvpBottom, -mNearDist);
  443. Matrix4 inv = mViewMatrix.inverseAffine();
  444. Vector3 vw_ul = inv.transformAffine(vp_ul);
  445. Vector3 vw_ur = inv.transformAffine(vp_ur);
  446. Vector3 vw_bl = inv.transformAffine(vp_bl);
  447. Vector3 vw_br = inv.transformAffine(vp_br);
  448. mWindowClipPlanes.clear();
  449. if (mProjType == PT_PERSPECTIVE)
  450. {
  451. Vector3 position = getPositionForViewUpdate();
  452. mWindowClipPlanes.push_back(Plane(position, vw_bl, vw_ul));
  453. mWindowClipPlanes.push_back(Plane(position, vw_ul, vw_ur));
  454. mWindowClipPlanes.push_back(Plane(position, vw_ur, vw_br));
  455. mWindowClipPlanes.push_back(Plane(position, vw_br, vw_bl));
  456. }
  457. else
  458. {
  459. Vector3 x_axis(inv[0][0], inv[0][1], inv[0][2]);
  460. Vector3 y_axis(inv[1][0], inv[1][1], inv[1][2]);
  461. x_axis.normalise();
  462. y_axis.normalise();
  463. mWindowClipPlanes.push_back(Plane( x_axis, vw_bl));
  464. mWindowClipPlanes.push_back(Plane(-x_axis, vw_ur));
  465. mWindowClipPlanes.push_back(Plane( y_axis, vw_bl));
  466. mWindowClipPlanes.push_back(Plane(-y_axis, vw_ur));
  467. }
  468. mRecalcWindow = false;
  469. }
  470. // -------------------------------------------------------------------
  471. const vector<Plane>::type& Camera::getWindowPlanes(void) const
  472. {
  473. updateView();
  474. setWindowImpl();
  475. return mWindowClipPlanes;
  476. }
  477. // -------------------------------------------------------------------
  478. Real Camera::getBoundingRadius(void) const
  479. {
  480. // return a little bigger than the near distance
  481. // just to keep things just outside
  482. return mNearDist * 1.5f;
  483. }
  484. //-----------------------------------------------------------------------
  485. const Vector3& Camera::getPositionForViewUpdate(void) const
  486. {
  487. // Note no update, because we're calling this from the update!
  488. return mRealPosition;
  489. }
  490. //-----------------------------------------------------------------------
  491. const Quaternion& Camera::getOrientationForViewUpdate(void) const
  492. {
  493. return mRealOrientation;
  494. }
  495. //-----------------------------------------------------------------------
  496. bool Camera::getAutoAspectRatio(void) const
  497. {
  498. return mAutoAspectRatio;
  499. }
  500. //-----------------------------------------------------------------------
  501. void Camera::setAutoAspectRatio(bool autoratio)
  502. {
  503. mAutoAspectRatio = autoratio;
  504. }
  505. //-----------------------------------------------------------------------
  506. bool Camera::isVisible(const AxisAlignedBox& bound, FrustumPlane* culledBy) const
  507. {
  508. if (mCullFrustum)
  509. {
  510. return mCullFrustum->isVisible(bound, culledBy);
  511. }
  512. else
  513. {
  514. return Frustum::isVisible(bound, culledBy);
  515. }
  516. }
  517. //-----------------------------------------------------------------------
  518. bool Camera::isVisible(const Sphere& bound, FrustumPlane* culledBy) const
  519. {
  520. if (mCullFrustum)
  521. {
  522. return mCullFrustum->isVisible(bound, culledBy);
  523. }
  524. else
  525. {
  526. return Frustum::isVisible(bound, culledBy);
  527. }
  528. }
  529. //-----------------------------------------------------------------------
  530. bool Camera::isVisible(const Vector3& vert, FrustumPlane* culledBy) const
  531. {
  532. if (mCullFrustum)
  533. {
  534. return mCullFrustum->isVisible(vert, culledBy);
  535. }
  536. else
  537. {
  538. return Frustum::isVisible(vert, culledBy);
  539. }
  540. }
  541. //-----------------------------------------------------------------------
  542. const Vector3* Camera::getWorldSpaceCorners(void) const
  543. {
  544. if (mCullFrustum)
  545. {
  546. return mCullFrustum->getWorldSpaceCorners();
  547. }
  548. else
  549. {
  550. return Frustum::getWorldSpaceCorners();
  551. }
  552. }
  553. //-----------------------------------------------------------------------
  554. const Plane& Camera::getFrustumPlane( unsigned short plane ) const
  555. {
  556. if (mCullFrustum)
  557. {
  558. return mCullFrustum->getFrustumPlane(plane);
  559. }
  560. else
  561. {
  562. return Frustum::getFrustumPlane(plane);
  563. }
  564. }
  565. //-----------------------------------------------------------------------
  566. bool Camera::projectSphere(const Sphere& sphere,
  567. Real* left, Real* top, Real* right, Real* bottom) const
  568. {
  569. if (mCullFrustum)
  570. {
  571. return mCullFrustum->projectSphere(sphere, left, top, right, bottom);
  572. }
  573. else
  574. {
  575. return Frustum::projectSphere(sphere, left, top, right, bottom);
  576. }
  577. }
  578. //-----------------------------------------------------------------------
  579. Real Camera::getNearClipDistance(void) const
  580. {
  581. if (mCullFrustum)
  582. {
  583. return mCullFrustum->getNearClipDistance();
  584. }
  585. else
  586. {
  587. return Frustum::getNearClipDistance();
  588. }
  589. }
  590. //-----------------------------------------------------------------------
  591. Real Camera::getFarClipDistance(void) const
  592. {
  593. if (mCullFrustum)
  594. {
  595. return mCullFrustum->getFarClipDistance();
  596. }
  597. else
  598. {
  599. return Frustum::getFarClipDistance();
  600. }
  601. }
  602. //-----------------------------------------------------------------------
  603. const Matrix4& Camera::getViewMatrix(void) const
  604. {
  605. if (mCullFrustum)
  606. {
  607. return mCullFrustum->getViewMatrix();
  608. }
  609. else
  610. {
  611. return Frustum::getViewMatrix();
  612. }
  613. }
  614. //-----------------------------------------------------------------------
  615. const Matrix4& Camera::getViewMatrix(bool ownFrustumOnly) const
  616. {
  617. if (ownFrustumOnly)
  618. {
  619. return Frustum::getViewMatrix();
  620. }
  621. else
  622. {
  623. return getViewMatrix();
  624. }
  625. }
  626. //-----------------------------------------------------------------------
  627. //_______________________________________________________
  628. //| |
  629. //| getRayForwardIntersect |
  630. //| ----------------------------- |
  631. //| get the intersections of frustum rays with a plane |
  632. //| of interest. The plane is assumed to have constant |
  633. //| z. If this is not the case, rays |
  634. //| should be rotated beforehand to work in a |
  635. //| coordinate system in which this is true. |
  636. //|_____________________________________________________|
  637. //
  638. vector<Vector4>::type Camera::getRayForwardIntersect(const Vector3& anchor, const Vector3 *dir, Real planeOffset) const
  639. {
  640. vector<Vector4>::type res;
  641. if(!dir)
  642. return res;
  643. int infpt[4] = {0, 0, 0, 0}; // 0=finite, 1=infinite, 2=straddles infinity
  644. Vector3 vec[4];
  645. // find how much the anchor point must be displaced in the plane's
  646. // constant variable
  647. Real delta = planeOffset - anchor.z;
  648. // now set the intersection point and note whether it is a
  649. // point at infinity or straddles infinity
  650. unsigned int i;
  651. for (i=0; i<4; i++)
  652. {
  653. Real test = dir[i].z * delta;
  654. if (test == 0.0) {
  655. vec[i] = dir[i];
  656. infpt[i] = 1;
  657. }
  658. else {
  659. Real lambda = delta / dir[i].z;
  660. vec[i] = anchor + (lambda * dir[i]);
  661. if(test < 0.0)
  662. infpt[i] = 2;
  663. }
  664. }
  665. for (i=0; i<4; i++)
  666. {
  667. // store the finite intersection points
  668. if (infpt[i] == 0)
  669. res.push_back(Vector4(vec[i].x, vec[i].y, vec[i].z, 1.0));
  670. else
  671. {
  672. // handle the infinite points of intersection;
  673. // cases split up into the possible frustum planes
  674. // pieces which may contain a finite intersection point
  675. int nextind = (i+1) % 4;
  676. int prevind = (i+3) % 4;
  677. if ((infpt[prevind] == 0) || (infpt[nextind] == 0))
  678. {
  679. if (infpt[i] == 1)
  680. res.push_back(Vector4(vec[i].x, vec[i].y, vec[i].z, 0.0));
  681. else
  682. {
  683. // handle the intersection points that straddle infinity (back-project)
  684. if(infpt[prevind] == 0)
  685. {
  686. Vector3 temp = vec[prevind] - vec[i];
  687. res.push_back(Vector4(temp.x, temp.y, temp.z, 0.0));
  688. }
  689. if(infpt[nextind] == 0)
  690. {
  691. Vector3 temp = vec[nextind] - vec[i];
  692. res.push_back(Vector4(temp.x, temp.y, temp.z, 0.0));
  693. }
  694. }
  695. } // end if we need to add an intersection point to the list
  696. } // end if infinite point needs to be considered
  697. } // end loop over frustun corners
  698. // we end up with either 0, 3, 4, or 5 intersection points
  699. return res;
  700. }
  701. //_______________________________________________________
  702. //| |
  703. //| forwardIntersect |
  704. //| ----------------------------- |
  705. //| Forward intersect the camera's frustum rays with |
  706. //| a specified plane of interest. |
  707. //| Note that if the frustum rays shoot out and would |
  708. //| back project onto the plane, this means the forward |
  709. //| intersection of the frustum would occur at the |
  710. //| line at infinity. |
  711. //|_____________________________________________________|
  712. //
  713. void Camera::forwardIntersect(const Plane& worldPlane, vector<Vector4>::type* intersect3d) const
  714. {
  715. if(!intersect3d)
  716. return;
  717. Vector3 trCorner = getWorldSpaceCorners()[0];
  718. Vector3 tlCorner = getWorldSpaceCorners()[1];
  719. Vector3 blCorner = getWorldSpaceCorners()[2];
  720. Vector3 brCorner = getWorldSpaceCorners()[3];
  721. // need some sort of rotation that will bring the plane normal to the z axis
  722. Plane pval = worldPlane;
  723. if(pval.normal.z < 0.0)
  724. {
  725. pval.normal *= -1.0;
  726. pval.d *= -1.0;
  727. }
  728. Quaternion invPlaneRot = pval.normal.getRotationTo(Vector3::UNIT_Z);
  729. // get rotated light
  730. Vector3 lPos = invPlaneRot * getDerivedPosition();
  731. Vector3 vec[4];
  732. vec[0] = invPlaneRot * trCorner - lPos;
  733. vec[1] = invPlaneRot * tlCorner - lPos;
  734. vec[2] = invPlaneRot * blCorner - lPos;
  735. vec[3] = invPlaneRot * brCorner - lPos;
  736. // compute intersection points on plane
  737. vector<Vector4>::type iPnt = getRayForwardIntersect(lPos, vec, -pval.d);
  738. // return wanted data
  739. if(intersect3d)
  740. {
  741. Quaternion planeRot = invPlaneRot.Inverse();
  742. (*intersect3d).clear();
  743. for(unsigned int i=0; i<iPnt.size(); i++)
  744. {
  745. Vector3 intersection = planeRot * Vector3(iPnt[i].x, iPnt[i].y, iPnt[i].z);
  746. (*intersect3d).push_back(Vector4(intersection.x, intersection.y, intersection.z, iPnt[i].w));
  747. }
  748. }
  749. }
  750. //-----------------------------------------------------------------------
  751. void Camera::synchroniseBaseSettingsWith(const Camera* cam)
  752. {
  753. this->setPosition(cam->getPosition());
  754. this->setProjectionType(cam->getProjectionType());
  755. this->setOrientation(cam->getOrientation());
  756. this->setAspectRatio(cam->getAspectRatio());
  757. this->setNearClipDistance(cam->getNearClipDistance());
  758. this->setFarClipDistance(cam->getFarClipDistance());
  759. this->setFOVy(cam->getFOVy());
  760. this->setFocalLength(cam->getFocalLength());
  761. // Don't do these, they're not base settings and can cause referencing issues
  762. //this->setLodCamera(cam->getLodCamera());
  763. //this->setCullingFrustum(cam->getCullingFrustum());
  764. }
  765. } // namespace Ogre