OgreCamera.cpp 32 KB

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