CmCamera.cpp 39 KB

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