CmCamera.cpp 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344
  1. /*
  2. -----------------------------------------------------------------------------
  3. This source file is part of OGRE
  4. (Object-oriented Graphics Rendering Engine)
  5. For the latest info, see http://www.ogre3d.org
  6. Copyright (c) 2000-2011 Torus Knot Software Ltd
  7. Permission is hereby granted, free of charge, to any person obtaining a copy
  8. of this software and associated documentation files (the "Software"), to deal
  9. in the Software without restriction, including without limitation the rights
  10. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. copies of the Software, and to permit persons to whom the Software is
  12. furnished to do so, subject to the following conditions:
  13. The above copyright notice and this permission notice shall be included in
  14. all copies or substantial portions of the Software.
  15. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. THE SOFTWARE.
  22. -----------------------------------------------------------------------------
  23. */
  24. #include "CmCamera.h"
  25. #include "CmMath.h"
  26. #include "CmMatrix3.h"
  27. #include "CmVector2.h"
  28. #include "CmAxisAlignedBox.h"
  29. #include "CmSphere.h"
  30. #include "CmHardwareBufferManager.h"
  31. #include "CmHardwareVertexBuffer.h"
  32. #include "CmHardwareIndexBuffer.h"
  33. #include "CmRenderSystemManager.h"
  34. #include "CmException.h"
  35. #include "CmRenderSystem.h"
  36. #include "CmGameObject.h"
  37. #if CM_PLATFORM == OGRE_PLATFORM_IPHONE
  38. #include "macUtils.h"
  39. #endif
  40. namespace CamelotEngine {
  41. const float Camera::INFINITE_FAR_PLANE_ADJUST = 0.00001f;
  42. //-----------------------------------------------------------------------
  43. Camera::Camera(GameObjectPtr parent)
  44. : Component(parent),
  45. mProjType(PT_PERSPECTIVE),
  46. mFOVy(Radian(Math::PI/4.0f)),
  47. mFarDist(100000.0f),
  48. mNearDist(100.0f),
  49. mAspect(1.33333333333333f),
  50. mOrthoHeight(1000),
  51. mFrustumOffset(Vector2::ZERO),
  52. mFocalLength(1.0f),
  53. mLastParentOrientation(Quaternion::IDENTITY),
  54. mLastParentPosition(Vector3::ZERO),
  55. mRecalcFrustum(true),
  56. mRecalcFrustumPlanes(true),
  57. mRecalcWorldSpaceCorners(true),
  58. mRecalcVertexData(true),
  59. mCustomViewMatrix(false),
  60. mCustomProjMatrix(false),
  61. mFrustumExtentsManuallySet(false),
  62. mSceneDetail(PM_SOLID),
  63. mWindowSet(false),
  64. mAutoAspectRatio(false),
  65. mViewport(nullptr)
  66. {
  67. updateView();
  68. updateFrustum();
  69. // Reasonable defaults to camera params
  70. mFOVy = Radian(Math::PI/4.0f);
  71. mNearDist = 100.0f;
  72. mFarDist = 100000.0f;
  73. mAspect = 1.33333333333333f;
  74. mProjType = PT_PERSPECTIVE;
  75. invalidateFrustum();
  76. // Init matrices
  77. mViewMatrix = Matrix4::ZERO;
  78. mProjMatrixRS = Matrix4::ZERO;
  79. }
  80. //-----------------------------------------------------------------------
  81. Camera::~Camera()
  82. {
  83. if(mViewport != nullptr)
  84. delete mViewport;
  85. }
  86. void Camera::init(RenderTarget* target, float left, float top, float width, float height, int ZOrder)
  87. {
  88. mViewport = new Viewport(target, left, top, width, height, ZOrder);
  89. }
  90. //-----------------------------------------------------------------------
  91. void Camera::setFOVy(const Radian& fov)
  92. {
  93. mFOVy = fov;
  94. invalidateFrustum();
  95. }
  96. //-----------------------------------------------------------------------
  97. const Radian& Camera::getFOVy(void) const
  98. {
  99. return mFOVy;
  100. }
  101. //-----------------------------------------------------------------------
  102. void Camera::setFarClipDistance(float farPlane)
  103. {
  104. mFarDist = farPlane;
  105. invalidateFrustum();
  106. }
  107. //-----------------------------------------------------------------------
  108. float Camera::getFarClipDistance(void) const
  109. {
  110. return mFarDist;
  111. }
  112. //-----------------------------------------------------------------------
  113. void Camera::setNearClipDistance(float nearPlane)
  114. {
  115. if (nearPlane <= 0)
  116. {
  117. CM_EXCEPT(InvalidParametersException, "Near clip distance must be greater than zero.");
  118. }
  119. mNearDist = nearPlane;
  120. invalidateFrustum();
  121. }
  122. //-----------------------------------------------------------------------
  123. float Camera::getNearClipDistance(void) const
  124. {
  125. return mNearDist;
  126. }
  127. //---------------------------------------------------------------------
  128. void Camera::setFrustumOffset(const Vector2& offset)
  129. {
  130. mFrustumOffset = offset;
  131. invalidateFrustum();
  132. }
  133. //---------------------------------------------------------------------
  134. void Camera::setFrustumOffset(float horizontal, float vertical)
  135. {
  136. setFrustumOffset(Vector2(horizontal, vertical));
  137. }
  138. //---------------------------------------------------------------------
  139. const Vector2& Camera::getFrustumOffset() const
  140. {
  141. return mFrustumOffset;
  142. }
  143. //---------------------------------------------------------------------
  144. void Camera::setFocalLength(float focalLength)
  145. {
  146. if (focalLength <= 0)
  147. {
  148. CM_EXCEPT(InvalidParametersException,
  149. "Focal length must be greater than zero.");
  150. }
  151. mFocalLength = focalLength;
  152. invalidateFrustum();
  153. }
  154. //---------------------------------------------------------------------
  155. float Camera::getFocalLength() const
  156. {
  157. return mFocalLength;
  158. }
  159. //-----------------------------------------------------------------------
  160. const Matrix4& Camera::getProjectionMatrix(void) const
  161. {
  162. updateFrustum();
  163. return mProjMatrix;
  164. }
  165. //-----------------------------------------------------------------------
  166. const Matrix4& Camera::getProjectionMatrixWithRSDepth(void) const
  167. {
  168. updateFrustum();
  169. return mProjMatrixRSDepth;
  170. }
  171. //-----------------------------------------------------------------------
  172. const Matrix4& Camera::getProjectionMatrixRS(void) const
  173. {
  174. updateFrustum();
  175. return mProjMatrixRS;
  176. }
  177. //-----------------------------------------------------------------------
  178. const Matrix4& Camera::getViewMatrix(void) const
  179. {
  180. updateView();
  181. return mViewMatrix;
  182. }
  183. //-----------------------------------------------------------------------
  184. const Plane* Camera::getFrustumPlanes(void) const
  185. {
  186. // Make any pending updates to the calculated frustum planes
  187. updateFrustumPlanes();
  188. return mFrustumPlanes;
  189. }
  190. //-----------------------------------------------------------------------
  191. const Plane& Camera::getFrustumPlane(unsigned short plane) const
  192. {
  193. // Make any pending updates to the calculated frustum planes
  194. updateFrustumPlanes();
  195. return mFrustumPlanes[plane];
  196. }
  197. //-----------------------------------------------------------------------
  198. bool Camera::isVisible(const AxisAlignedBox& bound, FrustumPlane* culledBy) const
  199. {
  200. // Null boxes always invisible
  201. if (bound.isNull()) return false;
  202. // Infinite boxes always visible
  203. if (bound.isInfinite()) return true;
  204. // Make any pending updates to the calculated frustum planes
  205. updateFrustumPlanes();
  206. // Get centre of the box
  207. Vector3 centre = bound.getCenter();
  208. // Get the half-size of the box
  209. Vector3 halfSize = bound.getHalfSize();
  210. // For each plane, see if all points are on the negative side
  211. // If so, object is not visible
  212. for (int plane = 0; plane < 6; ++plane)
  213. {
  214. // Skip far plane if infinite view frustum
  215. if (plane == FRUSTUM_PLANE_FAR && mFarDist == 0)
  216. continue;
  217. Plane::Side side = mFrustumPlanes[plane].getSide(centre, halfSize);
  218. if (side == Plane::NEGATIVE_SIDE)
  219. {
  220. // ALL corners on negative side therefore out of view
  221. if (culledBy)
  222. *culledBy = (FrustumPlane)plane;
  223. return false;
  224. }
  225. }
  226. return true;
  227. }
  228. //-----------------------------------------------------------------------
  229. bool Camera::isVisible(const Vector3& vert, FrustumPlane* culledBy) const
  230. {
  231. // Make any pending updates to the calculated frustum planes
  232. updateFrustumPlanes();
  233. // For each plane, see if all points are on the negative side
  234. // If so, object is not visible
  235. for (int plane = 0; plane < 6; ++plane)
  236. {
  237. // Skip far plane if infinite view frustum
  238. if (plane == FRUSTUM_PLANE_FAR && mFarDist == 0)
  239. continue;
  240. if (mFrustumPlanes[plane].getSide(vert) == Plane::NEGATIVE_SIDE)
  241. {
  242. // ALL corners on negative side therefore out of view
  243. if (culledBy)
  244. *culledBy = (FrustumPlane)plane;
  245. return false;
  246. }
  247. }
  248. return true;
  249. }
  250. //-----------------------------------------------------------------------
  251. bool Camera::isVisible(const Sphere& sphere, FrustumPlane* culledBy) const
  252. {
  253. // Make any pending updates to the calculated frustum planes
  254. updateFrustumPlanes();
  255. // For each plane, see if sphere is on negative side
  256. // If so, object is not visible
  257. for (int plane = 0; plane < 6; ++plane)
  258. {
  259. // Skip far plane if infinite view frustum
  260. if (plane == FRUSTUM_PLANE_FAR && mFarDist == 0)
  261. continue;
  262. // If the distance from sphere center to plane is negative, and 'more negative'
  263. // than the radius of the sphere, sphere is outside frustum
  264. if (mFrustumPlanes[plane].getDistance(sphere.getCenter()) < -sphere.getRadius())
  265. {
  266. // ALL corners on negative side therefore out of view
  267. if (culledBy)
  268. *culledBy = (FrustumPlane)plane;
  269. return false;
  270. }
  271. }
  272. return true;
  273. }
  274. //-----------------------------------------------------------------------
  275. void Camera::calcProjectionParameters(float& left, float& right, float& bottom, float& top) const
  276. {
  277. if (mCustomProjMatrix)
  278. {
  279. // Convert clipspace corners to camera space
  280. Matrix4 invProj = mProjMatrix.inverse();
  281. Vector3 topLeft(-0.5f, 0.5f, 0.0f);
  282. Vector3 bottomRight(0.5f, -0.5f, 0.0f);
  283. topLeft = invProj * topLeft;
  284. bottomRight = invProj * bottomRight;
  285. left = topLeft.x;
  286. top = topLeft.y;
  287. right = bottomRight.x;
  288. bottom = bottomRight.y;
  289. }
  290. else
  291. {
  292. if (mFrustumExtentsManuallySet)
  293. {
  294. left = mLeft;
  295. right = mRight;
  296. top = mTop;
  297. bottom = mBottom;
  298. }
  299. // Calculate general projection parameters
  300. else if (mProjType == PT_PERSPECTIVE)
  301. {
  302. Radian thetaY (mFOVy * 0.5f);
  303. float tanThetaY = Math::Tan(thetaY);
  304. float tanThetaX = tanThetaY * mAspect;
  305. float nearFocal = mNearDist / mFocalLength;
  306. float nearOffsetX = mFrustumOffset.x * nearFocal;
  307. float nearOffsetY = mFrustumOffset.y * nearFocal;
  308. float half_w = tanThetaX * mNearDist;
  309. float half_h = tanThetaY * mNearDist;
  310. left = - half_w + nearOffsetX;
  311. right = + half_w + nearOffsetX;
  312. bottom = - half_h + nearOffsetY;
  313. top = + half_h + nearOffsetY;
  314. mLeft = left;
  315. mRight = right;
  316. mTop = top;
  317. mBottom = bottom;
  318. }
  319. else
  320. {
  321. // Unknown how to apply frustum offset to orthographic camera, just ignore here
  322. float half_w = getOrthoWindowWidth() * 0.5f;
  323. float half_h = getOrthoWindowHeight() * 0.5f;
  324. left = - half_w;
  325. right = + half_w;
  326. bottom = - half_h;
  327. top = + half_h;
  328. mLeft = left;
  329. mRight = right;
  330. mTop = top;
  331. mBottom = bottom;
  332. }
  333. }
  334. }
  335. //-----------------------------------------------------------------------
  336. void Camera::updateFrustumImpl(void) const
  337. {
  338. // Common calcs
  339. float left, right, bottom, top;
  340. calcProjectionParameters(left, right, bottom, top);
  341. if (!mCustomProjMatrix)
  342. {
  343. // The code below will dealing with general projection
  344. // parameters, similar glFrustum and glOrtho.
  345. // Doesn't optimise manually except division operator, so the
  346. // code more self-explaining.
  347. float inv_w = 1 / (right - left);
  348. float inv_h = 1 / (top - bottom);
  349. float inv_d = 1 / (mFarDist - mNearDist);
  350. // Recalc if frustum params changed
  351. if (mProjType == PT_PERSPECTIVE)
  352. {
  353. // Calc matrix elements
  354. float A = 2 * mNearDist * inv_w;
  355. float B = 2 * mNearDist * inv_h;
  356. float C = (right + left) * inv_w;
  357. float D = (top + bottom) * inv_h;
  358. float q, qn;
  359. if (mFarDist == 0)
  360. {
  361. // Infinite far plane
  362. q = Camera::INFINITE_FAR_PLANE_ADJUST - 1;
  363. qn = mNearDist * (Camera::INFINITE_FAR_PLANE_ADJUST - 2);
  364. }
  365. else
  366. {
  367. q = - (mFarDist + mNearDist) * inv_d;
  368. qn = -2 * (mFarDist * mNearDist) * inv_d;
  369. }
  370. // NB: This creates 'uniform' perspective projection matrix,
  371. // which depth range [-1,1], right-handed rules
  372. //
  373. // [ A 0 C 0 ]
  374. // [ 0 B D 0 ]
  375. // [ 0 0 q qn ]
  376. // [ 0 0 -1 0 ]
  377. //
  378. // A = 2 * near / (right - left)
  379. // B = 2 * near / (top - bottom)
  380. // C = (right + left) / (right - left)
  381. // D = (top + bottom) / (top - bottom)
  382. // q = - (far + near) / (far - near)
  383. // qn = - 2 * (far * near) / (far - near)
  384. mProjMatrix = Matrix4::ZERO;
  385. mProjMatrix[0][0] = A;
  386. mProjMatrix[0][2] = C;
  387. mProjMatrix[1][1] = B;
  388. mProjMatrix[1][2] = D;
  389. mProjMatrix[2][2] = q;
  390. mProjMatrix[2][3] = qn;
  391. mProjMatrix[3][2] = -1;
  392. } // perspective
  393. else if (mProjType == PT_ORTHOGRAPHIC)
  394. {
  395. float A = 2 * inv_w;
  396. float B = 2 * inv_h;
  397. float C = - (right + left) * inv_w;
  398. float D = - (top + bottom) * inv_h;
  399. float q, qn;
  400. if (mFarDist == 0)
  401. {
  402. // Can not do infinite far plane here, avoid divided zero only
  403. q = - Camera::INFINITE_FAR_PLANE_ADJUST / mNearDist;
  404. qn = - Camera::INFINITE_FAR_PLANE_ADJUST - 1;
  405. }
  406. else
  407. {
  408. q = - 2 * inv_d;
  409. qn = - (mFarDist + mNearDist) * inv_d;
  410. }
  411. // NB: This creates 'uniform' orthographic projection matrix,
  412. // which depth range [-1,1], right-handed rules
  413. //
  414. // [ A 0 0 C ]
  415. // [ 0 B 0 D ]
  416. // [ 0 0 q qn ]
  417. // [ 0 0 0 1 ]
  418. //
  419. // A = 2 * / (right - left)
  420. // B = 2 * / (top - bottom)
  421. // C = - (right + left) / (right - left)
  422. // D = - (top + bottom) / (top - bottom)
  423. // q = - 2 / (far - near)
  424. // qn = - (far + near) / (far - near)
  425. mProjMatrix = Matrix4::ZERO;
  426. mProjMatrix[0][0] = A;
  427. mProjMatrix[0][3] = C;
  428. mProjMatrix[1][1] = B;
  429. mProjMatrix[1][3] = D;
  430. mProjMatrix[2][2] = q;
  431. mProjMatrix[2][3] = qn;
  432. mProjMatrix[3][3] = 1;
  433. } // ortho
  434. } // !mCustomProjMatrix
  435. RenderSystem* renderSystem = CamelotEngine::RenderSystemManager::getActive();
  436. // API specific
  437. renderSystem->_convertProjectionMatrix(mProjMatrix, mProjMatrixRS);
  438. // API specific for Gpu Programs
  439. renderSystem->_convertProjectionMatrix(mProjMatrix, mProjMatrixRSDepth, true);
  440. // Calculate bounding box (local)
  441. // Box is from 0, down -Z, max dimensions as determined from far plane
  442. // If infinite view frustum just pick a far value
  443. float farDist = (mFarDist == 0) ? 100000 : mFarDist;
  444. // Near plane bounds
  445. Vector3 min(left, bottom, -farDist);
  446. Vector3 max(right, top, 0);
  447. if (mCustomProjMatrix)
  448. {
  449. // Some custom projection matrices can have unusual inverted settings
  450. // So make sure the AABB is the right way around to start with
  451. Vector3 tmp = min;
  452. min.makeFloor(max);
  453. max.makeCeil(tmp);
  454. }
  455. if (mProjType == PT_PERSPECTIVE)
  456. {
  457. // Merge with far plane bounds
  458. float radio = farDist / mNearDist;
  459. min.makeFloor(Vector3(left * radio, bottom * radio, -farDist));
  460. max.makeCeil(Vector3(right * radio, top * radio, 0));
  461. }
  462. mBoundingBox.setExtents(min, max);
  463. mRecalcFrustum = false;
  464. // Signal to update frustum clipping planes
  465. mRecalcFrustumPlanes = true;
  466. }
  467. //-----------------------------------------------------------------------
  468. void Camera::updateFrustum(void) const
  469. {
  470. if (isFrustumOutOfDate())
  471. {
  472. updateFrustumImpl();
  473. }
  474. }
  475. //-----------------------------------------------------------------------
  476. void Camera::updateVertexData(void) const
  477. {
  478. if (mRecalcVertexData)
  479. {
  480. if (mVertexData.vertexBufferBinding->getBufferCount() <= 0)
  481. {
  482. // Initialise vertex & index data
  483. mVertexData.vertexDeclaration->addElement(0, 0, VET_FLOAT3, VES_POSITION);
  484. mVertexData.vertexCount = 32;
  485. mVertexData.vertexStart = 0;
  486. mVertexData.vertexBufferBinding->setBinding( 0,
  487. HardwareBufferManager::instance().createVertexBuffer(
  488. sizeof(float)*3, 32, HardwareBuffer::HBU_DYNAMIC_WRITE_ONLY) );
  489. }
  490. // Note: Even though we can dealing with general projection matrix here,
  491. // but because it's incompatibly with infinite far plane, thus, we
  492. // still need to working with projection parameters.
  493. // Calc near plane corners
  494. float vpLeft, vpRight, vpBottom, vpTop;
  495. calcProjectionParameters(vpLeft, vpRight, vpBottom, vpTop);
  496. // Treat infinite fardist as some arbitrary far value
  497. float farDist = (mFarDist == 0) ? 100000 : mFarDist;
  498. // Calc far plane corners
  499. float radio = mProjType == PT_PERSPECTIVE ? farDist / mNearDist : 1;
  500. float farLeft = vpLeft * radio;
  501. float farRight = vpRight * radio;
  502. float farBottom = vpBottom * radio;
  503. float farTop = vpTop * radio;
  504. // Calculate vertex positions (local)
  505. // 0 is the origin
  506. // 1, 2, 3, 4 are the points on the near plane, top left first, clockwise
  507. // 5, 6, 7, 8 are the points on the far plane, top left first, clockwise
  508. HardwareVertexBufferPtr vbuf = mVertexData.vertexBufferBinding->getBuffer(0);
  509. float* pFloat = static_cast<float*>(vbuf->lock(HardwareBuffer::HBL_DISCARD));
  510. // near plane (remember frustum is going in -Z direction)
  511. *pFloat++ = vpLeft; *pFloat++ = vpTop; *pFloat++ = -mNearDist;
  512. *pFloat++ = vpRight; *pFloat++ = vpTop; *pFloat++ = -mNearDist;
  513. *pFloat++ = vpRight; *pFloat++ = vpTop; *pFloat++ = -mNearDist;
  514. *pFloat++ = vpRight; *pFloat++ = vpBottom; *pFloat++ = -mNearDist;
  515. *pFloat++ = vpRight; *pFloat++ = vpBottom; *pFloat++ = -mNearDist;
  516. *pFloat++ = vpLeft; *pFloat++ = vpBottom; *pFloat++ = -mNearDist;
  517. *pFloat++ = vpLeft; *pFloat++ = vpBottom; *pFloat++ = -mNearDist;
  518. *pFloat++ = vpLeft; *pFloat++ = vpTop; *pFloat++ = -mNearDist;
  519. // far plane (remember frustum is going in -Z direction)
  520. *pFloat++ = farLeft; *pFloat++ = farTop; *pFloat++ = -farDist;
  521. *pFloat++ = farRight; *pFloat++ = farTop; *pFloat++ = -farDist;
  522. *pFloat++ = farRight; *pFloat++ = farTop; *pFloat++ = -farDist;
  523. *pFloat++ = farRight; *pFloat++ = farBottom; *pFloat++ = -farDist;
  524. *pFloat++ = farRight; *pFloat++ = farBottom; *pFloat++ = -farDist;
  525. *pFloat++ = farLeft; *pFloat++ = farBottom; *pFloat++ = -farDist;
  526. *pFloat++ = farLeft; *pFloat++ = farBottom; *pFloat++ = -farDist;
  527. *pFloat++ = farLeft; *pFloat++ = farTop; *pFloat++ = -farDist;
  528. // Sides of the pyramid
  529. *pFloat++ = 0.0f; *pFloat++ = 0.0f; *pFloat++ = 0.0f;
  530. *pFloat++ = vpLeft; *pFloat++ = vpTop; *pFloat++ = -mNearDist;
  531. *pFloat++ = 0.0f; *pFloat++ = 0.0f; *pFloat++ = 0.0f;
  532. *pFloat++ = vpRight; *pFloat++ = vpTop; *pFloat++ = -mNearDist;
  533. *pFloat++ = 0.0f; *pFloat++ = 0.0f; *pFloat++ = 0.0f;
  534. *pFloat++ = vpRight; *pFloat++ = vpBottom; *pFloat++ = -mNearDist;
  535. *pFloat++ = 0.0f; *pFloat++ = 0.0f; *pFloat++ = 0.0f;
  536. *pFloat++ = vpLeft; *pFloat++ = vpBottom; *pFloat++ = -mNearDist;
  537. // Sides of the box
  538. *pFloat++ = vpLeft; *pFloat++ = vpTop; *pFloat++ = -mNearDist;
  539. *pFloat++ = farLeft; *pFloat++ = farTop; *pFloat++ = -farDist;
  540. *pFloat++ = vpRight; *pFloat++ = vpTop; *pFloat++ = -mNearDist;
  541. *pFloat++ = farRight; *pFloat++ = farTop; *pFloat++ = -farDist;
  542. *pFloat++ = vpRight; *pFloat++ = vpBottom; *pFloat++ = -mNearDist;
  543. *pFloat++ = farRight; *pFloat++ = farBottom; *pFloat++ = -farDist;
  544. *pFloat++ = vpLeft; *pFloat++ = vpBottom; *pFloat++ = -mNearDist;
  545. *pFloat++ = farLeft; *pFloat++ = farBottom; *pFloat++ = -farDist;
  546. vbuf->unlock();
  547. mRecalcVertexData = false;
  548. }
  549. }
  550. //-----------------------------------------------------------------------
  551. bool Camera::isFrustumOutOfDate(void) const
  552. {
  553. return mRecalcFrustum;
  554. }
  555. //-----------------------------------------------------------------------
  556. void Camera::updateView(void) const
  557. {
  558. if (!mCustomViewMatrix)
  559. {
  560. Matrix3 rot;
  561. const Quaternion& orientation = getGameObject()->getWorldRotation();
  562. const Vector3& position = getGameObject()->getWorldPosition();
  563. mViewMatrix = Math::makeViewMatrix(position, orientation, 0);
  564. }
  565. }
  566. //-----------------------------------------------------------------------
  567. void Camera::updateFrustumPlanesImpl(void) const
  568. {
  569. // -------------------------
  570. // Update the frustum planes
  571. // -------------------------
  572. Matrix4 combo = mProjMatrix * mViewMatrix;
  573. mFrustumPlanes[FRUSTUM_PLANE_LEFT].normal.x = combo[3][0] + combo[0][0];
  574. mFrustumPlanes[FRUSTUM_PLANE_LEFT].normal.y = combo[3][1] + combo[0][1];
  575. mFrustumPlanes[FRUSTUM_PLANE_LEFT].normal.z = combo[3][2] + combo[0][2];
  576. mFrustumPlanes[FRUSTUM_PLANE_LEFT].d = combo[3][3] + combo[0][3];
  577. mFrustumPlanes[FRUSTUM_PLANE_RIGHT].normal.x = combo[3][0] - combo[0][0];
  578. mFrustumPlanes[FRUSTUM_PLANE_RIGHT].normal.y = combo[3][1] - combo[0][1];
  579. mFrustumPlanes[FRUSTUM_PLANE_RIGHT].normal.z = combo[3][2] - combo[0][2];
  580. mFrustumPlanes[FRUSTUM_PLANE_RIGHT].d = combo[3][3] - combo[0][3];
  581. mFrustumPlanes[FRUSTUM_PLANE_TOP].normal.x = combo[3][0] - combo[1][0];
  582. mFrustumPlanes[FRUSTUM_PLANE_TOP].normal.y = combo[3][1] - combo[1][1];
  583. mFrustumPlanes[FRUSTUM_PLANE_TOP].normal.z = combo[3][2] - combo[1][2];
  584. mFrustumPlanes[FRUSTUM_PLANE_TOP].d = combo[3][3] - combo[1][3];
  585. mFrustumPlanes[FRUSTUM_PLANE_BOTTOM].normal.x = combo[3][0] + combo[1][0];
  586. mFrustumPlanes[FRUSTUM_PLANE_BOTTOM].normal.y = combo[3][1] + combo[1][1];
  587. mFrustumPlanes[FRUSTUM_PLANE_BOTTOM].normal.z = combo[3][2] + combo[1][2];
  588. mFrustumPlanes[FRUSTUM_PLANE_BOTTOM].d = combo[3][3] + combo[1][3];
  589. mFrustumPlanes[FRUSTUM_PLANE_NEAR].normal.x = combo[3][0] + combo[2][0];
  590. mFrustumPlanes[FRUSTUM_PLANE_NEAR].normal.y = combo[3][1] + combo[2][1];
  591. mFrustumPlanes[FRUSTUM_PLANE_NEAR].normal.z = combo[3][2] + combo[2][2];
  592. mFrustumPlanes[FRUSTUM_PLANE_NEAR].d = combo[3][3] + combo[2][3];
  593. mFrustumPlanes[FRUSTUM_PLANE_FAR].normal.x = combo[3][0] - combo[2][0];
  594. mFrustumPlanes[FRUSTUM_PLANE_FAR].normal.y = combo[3][1] - combo[2][1];
  595. mFrustumPlanes[FRUSTUM_PLANE_FAR].normal.z = combo[3][2] - combo[2][2];
  596. mFrustumPlanes[FRUSTUM_PLANE_FAR].d = combo[3][3] - combo[2][3];
  597. // Renormalise any normals which were not unit length
  598. for(int i=0; i<6; i++ )
  599. {
  600. float length = mFrustumPlanes[i].normal.normalise();
  601. mFrustumPlanes[i].d /= length;
  602. }
  603. mRecalcFrustumPlanes = false;
  604. }
  605. //-----------------------------------------------------------------------
  606. void Camera::updateFrustumPlanes(void) const
  607. {
  608. updateView();
  609. updateFrustum();
  610. if (mRecalcFrustumPlanes)
  611. {
  612. updateFrustumPlanesImpl();
  613. }
  614. }
  615. //-----------------------------------------------------------------------
  616. void Camera::updateWorldSpaceCornersImpl(void) const
  617. {
  618. Matrix4 eyeToWorld = mViewMatrix.inverseAffine();
  619. // Note: Even though we can dealing with general projection matrix here,
  620. // but because it's incompatibly with infinite far plane, thus, we
  621. // still need to working with projection parameters.
  622. // Calc near plane corners
  623. float nearLeft, nearRight, nearBottom, nearTop;
  624. calcProjectionParameters(nearLeft, nearRight, nearBottom, nearTop);
  625. // Treat infinite fardist as some arbitrary far value
  626. float farDist = (mFarDist == 0) ? 100000 : mFarDist;
  627. // Calc far palne corners
  628. float radio = mProjType == PT_PERSPECTIVE ? farDist / mNearDist : 1;
  629. float farLeft = nearLeft * radio;
  630. float farRight = nearRight * radio;
  631. float farBottom = nearBottom * radio;
  632. float farTop = nearTop * radio;
  633. // near
  634. mWorldSpaceCorners[0] = eyeToWorld.transformAffine(Vector3(nearRight, nearTop, -mNearDist));
  635. mWorldSpaceCorners[1] = eyeToWorld.transformAffine(Vector3(nearLeft, nearTop, -mNearDist));
  636. mWorldSpaceCorners[2] = eyeToWorld.transformAffine(Vector3(nearLeft, nearBottom, -mNearDist));
  637. mWorldSpaceCorners[3] = eyeToWorld.transformAffine(Vector3(nearRight, nearBottom, -mNearDist));
  638. // far
  639. mWorldSpaceCorners[4] = eyeToWorld.transformAffine(Vector3(farRight, farTop, -farDist));
  640. mWorldSpaceCorners[5] = eyeToWorld.transformAffine(Vector3(farLeft, farTop, -farDist));
  641. mWorldSpaceCorners[6] = eyeToWorld.transformAffine(Vector3(farLeft, farBottom, -farDist));
  642. mWorldSpaceCorners[7] = eyeToWorld.transformAffine(Vector3(farRight, farBottom, -farDist));
  643. mRecalcWorldSpaceCorners = false;
  644. }
  645. //-----------------------------------------------------------------------
  646. void Camera::updateWorldSpaceCorners(void) const
  647. {
  648. updateView();
  649. if (mRecalcWorldSpaceCorners)
  650. {
  651. updateWorldSpaceCornersImpl();
  652. }
  653. }
  654. //-----------------------------------------------------------------------
  655. float Camera::getAspectRatio(void) const
  656. {
  657. return mAspect;
  658. }
  659. //-----------------------------------------------------------------------
  660. void Camera::setAspectRatio(float r)
  661. {
  662. mAspect = r;
  663. invalidateFrustum();
  664. }
  665. //-----------------------------------------------------------------------
  666. const AxisAlignedBox& Camera::getBoundingBox(void) const
  667. {
  668. return mBoundingBox;
  669. }
  670. // -------------------------------------------------------------------
  671. const Vector3* Camera::getWorldSpaceCorners(void) const
  672. {
  673. updateWorldSpaceCorners();
  674. return mWorldSpaceCorners;
  675. }
  676. //-----------------------------------------------------------------------
  677. void Camera::setProjectionType(ProjectionType pt)
  678. {
  679. mProjType = pt;
  680. invalidateFrustum();
  681. }
  682. //-----------------------------------------------------------------------
  683. ProjectionType Camera::getProjectionType(void) const
  684. {
  685. return mProjType;
  686. }
  687. //---------------------------------------------------------------------
  688. bool Camera::projectSphere(const Sphere& sphere,
  689. float* left, float* top, float* right, float* bottom) const
  690. {
  691. // See http://www.gamasutra.com/features/20021011/lengyel_06.htm
  692. // Transform light position into camera space
  693. updateView();
  694. Vector3 eyeSpacePos = mViewMatrix.transformAffine(sphere.getCenter());
  695. // initialise
  696. *left = *bottom = -1.0f;
  697. *right = *top = 1.0f;
  698. if (eyeSpacePos.z < 0)
  699. {
  700. updateFrustum();
  701. const Matrix4& projMatrix = getProjectionMatrix();
  702. float r = sphere.getRadius();
  703. float rsq = r * r;
  704. // early-exit
  705. if (eyeSpacePos.squaredLength() <= rsq)
  706. return false;
  707. float Lxz = Math::Sqr(eyeSpacePos.x) + Math::Sqr(eyeSpacePos.z);
  708. float Lyz = Math::Sqr(eyeSpacePos.y) + Math::Sqr(eyeSpacePos.z);
  709. // Find the tangent planes to the sphere
  710. // XZ first
  711. // calculate quadratic discriminant: b*b - 4ac
  712. // x = Nx
  713. // a = Lx^2 + Lz^2
  714. // b = -2rLx
  715. // c = r^2 - Lz^2
  716. float a = Lxz;
  717. float b = -2.0f * r * eyeSpacePos.x;
  718. float c = rsq - Math::Sqr(eyeSpacePos.z);
  719. float D = b*b - 4.0f*a*c;
  720. // two roots?
  721. if (D > 0)
  722. {
  723. float sqrootD = Math::Sqrt(D);
  724. // solve the quadratic to get the components of the normal
  725. float Nx0 = (-b + sqrootD) / (2 * a);
  726. float Nx1 = (-b - sqrootD) / (2 * a);
  727. // Derive Z from this
  728. float Nz0 = (r - Nx0 * eyeSpacePos.x) / eyeSpacePos.z;
  729. float Nz1 = (r - Nx1 * eyeSpacePos.x) / eyeSpacePos.z;
  730. // Get the point of tangency
  731. // Only consider points of tangency in front of the camera
  732. float Pz0 = (Lxz - rsq) / (eyeSpacePos.z - ((Nz0 / Nx0) * eyeSpacePos.x));
  733. if (Pz0 < 0)
  734. {
  735. // Project point onto near plane in worldspace
  736. float nearx0 = (Nz0 * mNearDist) / Nx0;
  737. // now we need to map this to viewport coords
  738. // use projection matrix since that will take into account all factors
  739. Vector3 relx0 = projMatrix * Vector3(nearx0, 0, -mNearDist);
  740. // find out whether this is a left side or right side
  741. float Px0 = -(Pz0 * Nz0) / Nx0;
  742. if (Px0 > eyeSpacePos.x)
  743. {
  744. *right = std::min(*right, relx0.x);
  745. }
  746. else
  747. {
  748. *left = std::max(*left, relx0.x);
  749. }
  750. }
  751. float Pz1 = (Lxz - rsq) / (eyeSpacePos.z - ((Nz1 / Nx1) * eyeSpacePos.x));
  752. if (Pz1 < 0)
  753. {
  754. // Project point onto near plane in worldspace
  755. float nearx1 = (Nz1 * mNearDist) / Nx1;
  756. // now we need to map this to viewport coords
  757. // use projection matrix since that will take into account all factors
  758. Vector3 relx1 = projMatrix * Vector3(nearx1, 0, -mNearDist);
  759. // find out whether this is a left side or right side
  760. float Px1 = -(Pz1 * Nz1) / Nx1;
  761. if (Px1 > eyeSpacePos.x)
  762. {
  763. *right = std::min(*right, relx1.x);
  764. }
  765. else
  766. {
  767. *left = std::max(*left, relx1.x);
  768. }
  769. }
  770. }
  771. // Now YZ
  772. // calculate quadratic discriminant: b*b - 4ac
  773. // x = Ny
  774. // a = Ly^2 + Lz^2
  775. // b = -2rLy
  776. // c = r^2 - Lz^2
  777. a = Lyz;
  778. b = -2.0f * r * eyeSpacePos.y;
  779. c = rsq - Math::Sqr(eyeSpacePos.z);
  780. D = b*b - 4.0f*a*c;
  781. // two roots?
  782. if (D > 0)
  783. {
  784. float sqrootD = Math::Sqrt(D);
  785. // solve the quadratic to get the components of the normal
  786. float Ny0 = (-b + sqrootD) / (2 * a);
  787. float Ny1 = (-b - sqrootD) / (2 * a);
  788. // Derive Z from this
  789. float Nz0 = (r - Ny0 * eyeSpacePos.y) / eyeSpacePos.z;
  790. float Nz1 = (r - Ny1 * eyeSpacePos.y) / eyeSpacePos.z;
  791. // Get the point of tangency
  792. // Only consider points of tangency in front of the camera
  793. float Pz0 = (Lyz - rsq) / (eyeSpacePos.z - ((Nz0 / Ny0) * eyeSpacePos.y));
  794. if (Pz0 < 0)
  795. {
  796. // Project point onto near plane in worldspace
  797. float neary0 = (Nz0 * mNearDist) / Ny0;
  798. // now we need to map this to viewport coords
  799. // use projection matriy since that will take into account all factors
  800. Vector3 rely0 = projMatrix * Vector3(0, neary0, -mNearDist);
  801. // find out whether this is a top side or bottom side
  802. float Py0 = -(Pz0 * Nz0) / Ny0;
  803. if (Py0 > eyeSpacePos.y)
  804. {
  805. *top = std::min(*top, rely0.y);
  806. }
  807. else
  808. {
  809. *bottom = std::max(*bottom, rely0.y);
  810. }
  811. }
  812. float Pz1 = (Lyz - rsq) / (eyeSpacePos.z - ((Nz1 / Ny1) * eyeSpacePos.y));
  813. if (Pz1 < 0)
  814. {
  815. // Project point onto near plane in worldspace
  816. float neary1 = (Nz1 * mNearDist) / Ny1;
  817. // now we need to map this to viewport coords
  818. // use projection matriy since that will take into account all factors
  819. Vector3 rely1 = projMatrix * Vector3(0, neary1, -mNearDist);
  820. // find out whether this is a top side or bottom side
  821. float Py1 = -(Pz1 * Nz1) / Ny1;
  822. if (Py1 > eyeSpacePos.y)
  823. {
  824. *top = std::min(*top, rely1.y);
  825. }
  826. else
  827. {
  828. *bottom = std::max(*bottom, rely1.y);
  829. }
  830. }
  831. }
  832. }
  833. return (*left != -1.0f) || (*top != 1.0f) || (*right != 1.0f) || (*bottom != -1.0f);
  834. }
  835. //---------------------------------------------------------------------
  836. void Camera::setCustomViewMatrix(bool enable, const Matrix4& viewMatrix)
  837. {
  838. mCustomViewMatrix = enable;
  839. if (enable)
  840. {
  841. assert(viewMatrix.isAffine());
  842. mViewMatrix = viewMatrix;
  843. }
  844. }
  845. //---------------------------------------------------------------------
  846. void Camera::setCustomProjectionMatrix(bool enable, const Matrix4& projMatrix)
  847. {
  848. mCustomProjMatrix = enable;
  849. if (enable)
  850. {
  851. mProjMatrix = projMatrix;
  852. }
  853. invalidateFrustum();
  854. }
  855. //---------------------------------------------------------------------
  856. void Camera::setOrthoWindow(float w, float h)
  857. {
  858. mOrthoHeight = h;
  859. mAspect = w / h;
  860. invalidateFrustum();
  861. }
  862. //---------------------------------------------------------------------
  863. void Camera::setOrthoWindowHeight(float h)
  864. {
  865. mOrthoHeight = h;
  866. invalidateFrustum();
  867. }
  868. //---------------------------------------------------------------------
  869. void Camera::setOrthoWindowWidth(float w)
  870. {
  871. mOrthoHeight = w / mAspect;
  872. invalidateFrustum();
  873. }
  874. //---------------------------------------------------------------------
  875. float Camera::getOrthoWindowHeight() const
  876. {
  877. return mOrthoHeight;
  878. }
  879. //---------------------------------------------------------------------
  880. float Camera::getOrthoWindowWidth() const
  881. {
  882. return mOrthoHeight * mAspect;
  883. }
  884. //---------------------------------------------------------------------
  885. void Camera::setFrustumExtents(float left, float right, float top, float bottom)
  886. {
  887. mFrustumExtentsManuallySet = true;
  888. mLeft = left;
  889. mRight = right;
  890. mTop = top;
  891. mBottom = bottom;
  892. invalidateFrustum();
  893. }
  894. //---------------------------------------------------------------------
  895. void Camera::resetFrustumExtents()
  896. {
  897. mFrustumExtentsManuallySet = false;
  898. invalidateFrustum();
  899. }
  900. //---------------------------------------------------------------------
  901. void Camera::getFrustumExtents(float& outleft, float& outright, float& outtop, float& outbottom) const
  902. {
  903. updateFrustum();
  904. outleft = mLeft;
  905. outright = mRight;
  906. outtop = mTop;
  907. outbottom = mBottom;
  908. }
  909. //-----------------------------------------------------------------------
  910. void Camera::setPolygonMode(PolygonMode sd)
  911. {
  912. mSceneDetail = sd;
  913. }
  914. //-----------------------------------------------------------------------
  915. PolygonMode Camera::getPolygonMode(void) const
  916. {
  917. return mSceneDetail;
  918. }
  919. // -------------------------------------------------------------------
  920. void Camera::invalidateFrustum(void) const
  921. {
  922. mRecalcWindow = true;
  923. mRecalcFrustum = true;
  924. mRecalcFrustumPlanes = true;
  925. mRecalcWorldSpaceCorners = true;
  926. mRecalcVertexData = true;
  927. }
  928. //-----------------------------------------------------------------------
  929. void Camera::_renderScene(Viewport *vp, bool includeOverlays)
  930. {
  931. // TODO PORT - I'm not going to be rendering the scene like this (yet), but I think I will do it eventually
  932. //mSceneMgr->_renderScene(this, vp, includeOverlays);
  933. }
  934. //-----------------------------------------------------------------------
  935. Ray Camera::getCameraToViewportRay(float screenX, float screenY) const
  936. {
  937. Ray ret;
  938. getCameraToViewportRay(screenX, screenY, &ret);
  939. return ret;
  940. }
  941. //---------------------------------------------------------------------
  942. void Camera::getCameraToViewportRay(float screenX, float screenY, Ray* outRay) const
  943. {
  944. Matrix4 inverseVP = (getProjectionMatrix() * getViewMatrix()).inverse();
  945. float nx = (2.0f * screenX) - 1.0f;
  946. float ny = 1.0f - (2.0f * screenY);
  947. Vector3 nearPoint(nx, ny, -1.f);
  948. // Use midPoint rather than far point to avoid issues with infinite projection
  949. Vector3 midPoint (nx, ny, 0.0f);
  950. // Get ray origin and ray target on near plane in world space
  951. Vector3 rayOrigin, rayTarget;
  952. rayOrigin = inverseVP * nearPoint;
  953. rayTarget = inverseVP * midPoint;
  954. Vector3 rayDirection = rayTarget - rayOrigin;
  955. rayDirection.normalise();
  956. outRay->setOrigin(rayOrigin);
  957. outRay->setDirection(rayDirection);
  958. }
  959. // -------------------------------------------------------------------
  960. void Camera::setWindow (float Left, float Top, float Right, float Bottom)
  961. {
  962. mWLeft = Left;
  963. mWTop = Top;
  964. mWRight = Right;
  965. mWBottom = Bottom;
  966. mWindowSet = true;
  967. mRecalcWindow = true;
  968. }
  969. // -------------------------------------------------------------------
  970. void Camera::resetWindow ()
  971. {
  972. mWindowSet = false;
  973. }
  974. // -------------------------------------------------------------------
  975. void Camera::setWindowImpl() const
  976. {
  977. if (!mWindowSet || !mRecalcWindow)
  978. return;
  979. // Calculate general projection parameters
  980. float vpLeft, vpRight, vpBottom, vpTop;
  981. calcProjectionParameters(vpLeft, vpRight, vpBottom, vpTop);
  982. float vpWidth = vpRight - vpLeft;
  983. float vpHeight = vpTop - vpBottom;
  984. float wvpLeft = vpLeft + mWLeft * vpWidth;
  985. float wvpRight = vpLeft + mWRight * vpWidth;
  986. float wvpTop = vpTop - mWTop * vpHeight;
  987. float wvpBottom = vpTop - mWBottom * vpHeight;
  988. Vector3 vp_ul (wvpLeft, wvpTop, -mNearDist);
  989. Vector3 vp_ur (wvpRight, wvpTop, -mNearDist);
  990. Vector3 vp_bl (wvpLeft, wvpBottom, -mNearDist);
  991. Vector3 vp_br (wvpRight, wvpBottom, -mNearDist);
  992. Matrix4 inv = mViewMatrix.inverseAffine();
  993. Vector3 vw_ul = inv.transformAffine(vp_ul);
  994. Vector3 vw_ur = inv.transformAffine(vp_ur);
  995. Vector3 vw_bl = inv.transformAffine(vp_bl);
  996. Vector3 vw_br = inv.transformAffine(vp_br);
  997. mWindowClipPlanes.clear();
  998. if (mProjType == PT_PERSPECTIVE)
  999. {
  1000. Vector3 position = getGameObject()->getWorldPosition();
  1001. mWindowClipPlanes.push_back(Plane(position, vw_bl, vw_ul));
  1002. mWindowClipPlanes.push_back(Plane(position, vw_ul, vw_ur));
  1003. mWindowClipPlanes.push_back(Plane(position, vw_ur, vw_br));
  1004. mWindowClipPlanes.push_back(Plane(position, vw_br, vw_bl));
  1005. }
  1006. else
  1007. {
  1008. Vector3 x_axis(inv[0][0], inv[0][1], inv[0][2]);
  1009. Vector3 y_axis(inv[1][0], inv[1][1], inv[1][2]);
  1010. x_axis.normalise();
  1011. y_axis.normalise();
  1012. mWindowClipPlanes.push_back(Plane( x_axis, vw_bl));
  1013. mWindowClipPlanes.push_back(Plane(-x_axis, vw_ur));
  1014. mWindowClipPlanes.push_back(Plane( y_axis, vw_bl));
  1015. mWindowClipPlanes.push_back(Plane(-y_axis, vw_ur));
  1016. }
  1017. mRecalcWindow = false;
  1018. }
  1019. // -------------------------------------------------------------------
  1020. const vector<Plane>::type& Camera::getWindowPlanes(void) const
  1021. {
  1022. updateView();
  1023. setWindowImpl();
  1024. return mWindowClipPlanes;
  1025. }
  1026. // -------------------------------------------------------------------
  1027. float Camera::getBoundingRadius(void) const
  1028. {
  1029. // return a little bigger than the near distance
  1030. // just to keep things just outside
  1031. return mNearDist * 1.5f;
  1032. }
  1033. //-----------------------------------------------------------------------
  1034. bool Camera::getAutoAspectRatio(void) const
  1035. {
  1036. return mAutoAspectRatio;
  1037. }
  1038. //-----------------------------------------------------------------------
  1039. void Camera::setAutoAspectRatio(bool autoratio)
  1040. {
  1041. mAutoAspectRatio = autoratio;
  1042. }
  1043. //-----------------------------------------------------------------------
  1044. //_______________________________________________________
  1045. //| |
  1046. //| getRayForwardIntersect |
  1047. //| ----------------------------- |
  1048. //| get the intersections of frustum rays with a plane |
  1049. //| of interest. The plane is assumed to have constant |
  1050. //| z. If this is not the case, rays |
  1051. //| should be rotated beforehand to work in a |
  1052. //| coordinate system in which this is true. |
  1053. //|_____________________________________________________|
  1054. //
  1055. vector<Vector4>::type Camera::getRayForwardIntersect(const Vector3& anchor, const Vector3 *dir, float planeOffset) const
  1056. {
  1057. vector<Vector4>::type res;
  1058. if(!dir)
  1059. return res;
  1060. int infpt[4] = {0, 0, 0, 0}; // 0=finite, 1=infinite, 2=straddles infinity
  1061. Vector3 vec[4];
  1062. // find how much the anchor point must be displaced in the plane's
  1063. // constant variable
  1064. float delta = planeOffset - anchor.z;
  1065. // now set the intersection point and note whether it is a
  1066. // point at infinity or straddles infinity
  1067. unsigned int i;
  1068. for (i=0; i<4; i++)
  1069. {
  1070. float test = dir[i].z * delta;
  1071. if (test == 0.0) {
  1072. vec[i] = dir[i];
  1073. infpt[i] = 1;
  1074. }
  1075. else {
  1076. float lambda = delta / dir[i].z;
  1077. vec[i] = anchor + (lambda * dir[i]);
  1078. if(test < 0.0)
  1079. infpt[i] = 2;
  1080. }
  1081. }
  1082. for (i=0; i<4; i++)
  1083. {
  1084. // store the finite intersection points
  1085. if (infpt[i] == 0)
  1086. res.push_back(Vector4(vec[i].x, vec[i].y, vec[i].z, 1.0));
  1087. else
  1088. {
  1089. // handle the infinite points of intersection;
  1090. // cases split up into the possible frustum planes
  1091. // pieces which may contain a finite intersection point
  1092. int nextind = (i+1) % 4;
  1093. int prevind = (i+3) % 4;
  1094. if ((infpt[prevind] == 0) || (infpt[nextind] == 0))
  1095. {
  1096. if (infpt[i] == 1)
  1097. res.push_back(Vector4(vec[i].x, vec[i].y, vec[i].z, 0.0));
  1098. else
  1099. {
  1100. // handle the intersection points that straddle infinity (back-project)
  1101. if(infpt[prevind] == 0)
  1102. {
  1103. Vector3 temp = vec[prevind] - vec[i];
  1104. res.push_back(Vector4(temp.x, temp.y, temp.z, 0.0));
  1105. }
  1106. if(infpt[nextind] == 0)
  1107. {
  1108. Vector3 temp = vec[nextind] - vec[i];
  1109. res.push_back(Vector4(temp.x, temp.y, temp.z, 0.0));
  1110. }
  1111. }
  1112. } // end if we need to add an intersection point to the list
  1113. } // end if infinite point needs to be considered
  1114. } // end loop over frustun corners
  1115. // we end up with either 0, 3, 4, or 5 intersection points
  1116. return res;
  1117. }
  1118. //_______________________________________________________
  1119. //| |
  1120. //| forwardIntersect |
  1121. //| ----------------------------- |
  1122. //| Forward intersect the camera's frustum rays with |
  1123. //| a specified plane of interest. |
  1124. //| Note that if the frustum rays shoot out and would |
  1125. //| back project onto the plane, this means the forward |
  1126. //| intersection of the frustum would occur at the |
  1127. //| line at infinity. |
  1128. //|_____________________________________________________|
  1129. //
  1130. void Camera::forwardIntersect(const Plane& worldPlane, vector<Vector4>::type* intersect3d) const
  1131. {
  1132. if(!intersect3d)
  1133. return;
  1134. Vector3 trCorner = getWorldSpaceCorners()[0];
  1135. Vector3 tlCorner = getWorldSpaceCorners()[1];
  1136. Vector3 blCorner = getWorldSpaceCorners()[2];
  1137. Vector3 brCorner = getWorldSpaceCorners()[3];
  1138. // need some sort of rotation that will bring the plane normal to the z axis
  1139. Plane pval = worldPlane;
  1140. if(pval.normal.z < 0.0)
  1141. {
  1142. pval.normal *= -1.0;
  1143. pval.d *= -1.0;
  1144. }
  1145. Quaternion invPlaneRot = pval.normal.getRotationTo(Vector3::UNIT_Z);
  1146. // get rotated light
  1147. Vector3 lPos = invPlaneRot * getGameObject()->getWorldPosition();
  1148. Vector3 vec[4];
  1149. vec[0] = invPlaneRot * trCorner - lPos;
  1150. vec[1] = invPlaneRot * tlCorner - lPos;
  1151. vec[2] = invPlaneRot * blCorner - lPos;
  1152. vec[3] = invPlaneRot * brCorner - lPos;
  1153. // compute intersection points on plane
  1154. vector<Vector4>::type iPnt = getRayForwardIntersect(lPos, vec, -pval.d);
  1155. // return wanted data
  1156. if(intersect3d)
  1157. {
  1158. Quaternion planeRot = invPlaneRot.Inverse();
  1159. (*intersect3d).clear();
  1160. for(unsigned int i=0; i<iPnt.size(); i++)
  1161. {
  1162. Vector3 intersection = planeRot * Vector3(iPnt[i].x, iPnt[i].y, iPnt[i].z);
  1163. (*intersect3d).push_back(Vector4(intersection.x, intersection.y, intersection.z, iPnt[i].w));
  1164. }
  1165. }
  1166. }
  1167. } // namespace CamelotEngine