frustum.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #include "platform/platform.h"
  23. #include "math/util/frustum.h"
  24. #include "math/mMathFn.h"
  25. #include "math/mathUtils.h"
  26. #include "math/mSphere.h"
  27. #include "platform/profiler.h"
  28. static const MatrixF sGFXProjRotMatrix( EulerF( (M_PI_F / 2.0f), 0.0f, 0.0f ) );
  29. //TODO: For OBB/frustum intersections and ortho frustums, we can resort to a much quicker AABB/OBB test
  30. // Must be CW ordered for face[0] of each edge! Keep in mind that normals
  31. // are pointing *inwards* and thus what appears CCW outside is CW inside.
  32. FrustumData::EdgeListType FrustumData::smEdges
  33. (
  34. PolyhedronData::Edge( PlaneNear, PlaneTop, NearTopRight, NearTopLeft ),
  35. PolyhedronData::Edge( PlaneNear, PlaneBottom, NearBottomLeft, NearBottomRight ),
  36. PolyhedronData::Edge( PlaneNear, PlaneLeft, NearTopLeft, NearBottomLeft ),
  37. PolyhedronData::Edge( PlaneNear, PlaneRight, NearTopRight, NearBottomRight ),
  38. PolyhedronData::Edge( PlaneFar, PlaneTop, FarTopLeft, FarTopRight ),
  39. PolyhedronData::Edge( PlaneFar, PlaneBottom, FarBottomRight, FarBottomLeft ),
  40. PolyhedronData::Edge( PlaneFar, PlaneLeft, FarBottomLeft, FarTopLeft ),
  41. PolyhedronData::Edge( PlaneFar, PlaneRight, FarTopRight, FarBottomRight ),
  42. PolyhedronData::Edge( PlaneTop, PlaneLeft, FarTopLeft, NearTopLeft ),
  43. PolyhedronData::Edge( PlaneTop, PlaneRight, NearTopRight, FarTopRight ),
  44. PolyhedronData::Edge( PlaneBottom, PlaneLeft, NearBottomLeft, FarBottomLeft ),
  45. PolyhedronData::Edge( PlaneBottom, PlaneRight, FarBottomRight, NearBottomRight )
  46. );
  47. //-----------------------------------------------------------------------------
  48. Frustum::Frustum( bool isOrtho,
  49. F32 nearLeft,
  50. F32 nearRight,
  51. F32 nearTop,
  52. F32 nearBottom,
  53. F32 nearDist,
  54. F32 farDist,
  55. const MatrixF &transform )
  56. {
  57. mTransform = transform;
  58. mPosition = transform.getPosition();
  59. mNearLeft = nearLeft;
  60. mNearRight = nearRight;
  61. mNearTop = nearTop;
  62. mNearBottom = nearBottom;
  63. mNearDist = nearDist;
  64. mFarDist = farDist;
  65. mIsOrtho = isOrtho;
  66. mNumTiles = 1;
  67. mCurrTile.set(0,0);
  68. mTileOverlap.set(0.0f, 0.0f);
  69. mProjectionOffset.zero();
  70. mProjectionOffsetMatrix.identity();
  71. }
  72. //-----------------------------------------------------------------------------
  73. void Frustum::set( bool isOrtho,
  74. F32 fovYInRadians,
  75. F32 aspectRatio,
  76. F32 nearDist,
  77. F32 farDist,
  78. const MatrixF &transform )
  79. {
  80. F32 left, right, top, bottom;
  81. MathUtils::makeFrustum( &left, &right, &top, &bottom, fovYInRadians, aspectRatio, nearDist );
  82. tile( &left, &right, &top, &bottom, mNumTiles, mCurrTile, mTileOverlap );
  83. set( isOrtho, left, right, top, bottom, nearDist, farDist, transform );
  84. }
  85. //-----------------------------------------------------------------------------
  86. void Frustum::set( bool isOrtho,
  87. F32 nearLeft,
  88. F32 nearRight,
  89. F32 nearTop,
  90. F32 nearBottom,
  91. F32 nearDist,
  92. F32 farDist,
  93. const MatrixF &transform )
  94. {
  95. mTransform = transform;
  96. mPosition = mTransform.getPosition();
  97. mNearLeft = nearLeft;
  98. mNearRight = nearRight;
  99. mNearTop = nearTop;
  100. mNearBottom = nearBottom;
  101. mNearDist = nearDist;
  102. mFarDist = farDist;
  103. mIsOrtho = isOrtho;
  104. mDirty = true;
  105. }
  106. //-----------------------------------------------------------------------------
  107. #if 0
  108. void Frustum::set( const MatrixF &projMat, bool normalize )
  109. {
  110. // From "Fast Extraction of Viewing Frustum Planes from the World-View-Projection Matrix"
  111. // by Gil Gribb and Klaus Hartmann.
  112. //
  113. // http://www2.ravensoft.com/users/ggribb/plane%20extraction.pdf
  114. // Right clipping plane.
  115. mPlanes[ PlaneRight ].set( projMat[3] - projMat[0],
  116. projMat[7] - projMat[4],
  117. projMat[11] - projMat[8],
  118. projMat[15] - projMat[12] );
  119. // Left clipping plane.
  120. mPlanes[ PlaneLeft ].set( projMat[3] + projMat[0],
  121. projMat[7] + projMat[4],
  122. projMat[11] + projMat[8],
  123. projMat[15] + projMat[12] );
  124. // Bottom clipping plane.
  125. mPlanes[ PlaneBottom ].set( projMat[3] + projMat[1],
  126. projMat[7] + projMat[5],
  127. projMat[11] + projMat[9],
  128. projMat[15] + projMat[13] );
  129. // Top clipping plane.
  130. mPlanes[ PlaneTop ].set( projMat[3] - projMat[1],
  131. projMat[7] - projMat[5],
  132. projMat[11] - projMat[9],
  133. projMat[15] - projMat[13] );
  134. // Near clipping plane
  135. mPlanes[ PlaneNear ].set( projMat[3] + projMat[2],
  136. projMat[7] + projMat[6],
  137. projMat[11] + projMat[10],
  138. projMat[15] + projMat[14] );
  139. // Far clipping plane.
  140. mPlanes[ PlaneFar ].set( projMat[3] - projMat[2],
  141. projMat[7] - projMat[6],
  142. projMat[11] - projMat[10],
  143. projMat[15] - projMat[14] );
  144. if( normalize )
  145. {
  146. for( S32 i = 0; i < PlaneCount; ++ i )
  147. mPlanes[ i ].normalize();
  148. }
  149. /*// Create the corner points via plane intersections.
  150. mPlanes[ PlaneNear ].intersect( mPlanes[ PlaneTop ], mPlanes[ PlaneLeft ], &mPoints[ NearTopLeft ] );
  151. mPlanes[ PlaneNear ].intersect( mPlanes[ PlaneTop ], mPlanes[ PlaneRight ], &mPoints[ NearTopRight ] );
  152. mPlanes[ PlaneNear ].intersect( mPlanes[ PlaneBottom ], mPlanes[ PlaneLeft ], &mPoints[ NearBottomLeft ] );
  153. mPlanes[ PlaneNear ].intersect( mPlanes[ PlaneBottom ], mPlanes[ PlaneRight ], &mPoints[ NearBottomRight ] );
  154. mPlanes[ PlaneFar ].intersect( mPlanes[ PlaneTop ], mPlanes[ PlaneLeft ], &mPoints[ FarTopLeft ] );
  155. mPlanes[ PlaneFar ].intersect( mPlanes[ PlaneTop ], mPlanes[ PlaneRight ], &mPoints[ FarTopRight ] );
  156. mPlanes[ PlaneFar ].intersect( mPlanes[ PlaneBottom ], mPlanes[ PlaneLeft ], &mPoints[ FarBottomLeft ] );
  157. mPlanes[ PlaneFar ].intersect( mPlanes[ PlaneBottom ], mPlanes[ PlaneRight ], &mPoints[ FarBottomRight ] );
  158. */
  159. // Update the axis aligned bounding box.
  160. _updateBounds();
  161. }
  162. #endif
  163. //-----------------------------------------------------------------------------
  164. void Frustum::setNearDist( F32 nearDist )
  165. {
  166. setNearFarDist( nearDist, mFarDist );
  167. }
  168. //-----------------------------------------------------------------------------
  169. void Frustum::setFarDist( F32 farDist )
  170. {
  171. setNearFarDist( mNearDist, farDist );
  172. }
  173. //-----------------------------------------------------------------------------
  174. void Frustum::setNearFarDist( F32 nearDist, F32 farDist )
  175. {
  176. if( mNearDist == nearDist && mFarDist == farDist )
  177. return;
  178. // Recalculate the frustum.
  179. MatrixF xfm( mTransform );
  180. const F32 CENTER_EPSILON = 0.001f;
  181. F32 centerX = mNearLeft + (mNearRight - mNearLeft) * 0.5;
  182. F32 centerY = mNearBottom + (mNearTop - mNearBottom) * 0.5;
  183. if ((centerX > CENTER_EPSILON || centerX < -CENTER_EPSILON) || (centerY > CENTER_EPSILON || centerY < -CENTER_EPSILON) )
  184. {
  185. // Off-center projection, so re-calc use the new distances
  186. FovPort expectedFovPort;
  187. expectedFovPort.leftTan = -(mNearLeft / mNearDist);
  188. expectedFovPort.rightTan = (mNearRight / mNearDist);
  189. expectedFovPort.upTan = (mNearTop / mNearDist);
  190. expectedFovPort.downTan = -(mNearBottom / mNearDist);
  191. MathUtils::makeFovPortFrustum(this, mIsOrtho, nearDist, farDist, expectedFovPort);
  192. }
  193. else
  194. {
  195. // Projection is not off-center, use the normal code
  196. set(mIsOrtho, getFov(), getAspectRatio(), nearDist, farDist, xfm);
  197. }
  198. }
  199. //-----------------------------------------------------------------------------
  200. void Frustum::cropNearFar(F32 newNearDist, F32 newFarDist)
  201. {
  202. const F32 newOverOld = newNearDist / mNearDist;
  203. set( mIsOrtho, mNearLeft * newOverOld, mNearRight * newOverOld, mNearTop * newOverOld, mNearBottom * newOverOld,
  204. newNearDist, newFarDist, mTransform);
  205. }
  206. //-----------------------------------------------------------------------------
  207. bool Frustum::bakeProjectionOffset()
  208. {
  209. // Nothing to bake if ortho
  210. if( mIsOrtho )
  211. return false;
  212. // Nothing to bake if no offset
  213. if( mProjectionOffset.isZero() )
  214. return false;
  215. // Near plane points in camera space
  216. Point3F np[4];
  217. np[0].set( mNearLeft, mNearDist, mNearTop ); // NearTopLeft
  218. np[1].set( mNearRight, mNearDist, mNearTop ); // NearTopRight
  219. np[2].set( mNearLeft, mNearDist, mNearBottom ); // NearBottomLeft
  220. np[3].set( mNearRight, mNearDist, mNearBottom ); // NearBottomRight
  221. // Generate the near plane
  222. PlaneF nearPlane( np[0], np[1], np[3] );
  223. // Far plane points in camera space
  224. const F32 farOverNear = mFarDist / mNearDist;
  225. Point3F fp0( mNearLeft * farOverNear, mFarDist, mNearTop * farOverNear ); // FarTopLeft
  226. Point3F fp1( mNearRight * farOverNear, mFarDist, mNearTop * farOverNear ); // FarTopRight
  227. Point3F fp2( mNearLeft * farOverNear, mFarDist, mNearBottom * farOverNear ); // FarBottomLeft
  228. Point3F fp3( mNearRight * farOverNear, mFarDist, mNearBottom * farOverNear ); // FarBottomRight
  229. // Generate the far plane
  230. PlaneF farPlane( fp0, fp1, fp3 );
  231. // The offset camera point
  232. Point3F offsetCamera( mProjectionOffset.x, 0.0f, mProjectionOffset.y );
  233. // The near plane point we'll be using for our calculations below
  234. U32 nIndex = 0;
  235. if( mProjectionOffset.x < 0.0 )
  236. {
  237. // Offset to the left so we'll need to use the near plane point on the right
  238. nIndex = 1;
  239. }
  240. if( mProjectionOffset.y > 0.0 )
  241. {
  242. // Offset to the top so we'll need to use the near plane point at the bottom
  243. nIndex += 2;
  244. }
  245. // Begin by calculating the offset point on the far plane as it goes
  246. // from the offset camera to the edge of the near plane.
  247. Point3F farPoint;
  248. Point3F fdir = np[nIndex] - offsetCamera;
  249. fdir.normalize();
  250. if( farPlane.intersect(offsetCamera, fdir, &farPoint) )
  251. {
  252. // Calculate the new near plane edge from the non-offset camera position
  253. // to the far plane point from above.
  254. Point3F nearPoint;
  255. Point3F ndir = farPoint;
  256. ndir.normalize();
  257. if( nearPlane.intersect( Point3F::Zero, ndir, &nearPoint) )
  258. {
  259. // Handle a x offset
  260. if( mProjectionOffset.x < 0.0 )
  261. {
  262. // The new near plane right side
  263. mNearRight = nearPoint.x;
  264. }
  265. else if( mProjectionOffset.x > 0.0 )
  266. {
  267. // The new near plane left side
  268. mNearLeft = nearPoint.x;
  269. }
  270. // Handle a y offset
  271. if( mProjectionOffset.y < 0.0 )
  272. {
  273. // The new near plane top side
  274. mNearTop = nearPoint.y;
  275. }
  276. else if( mProjectionOffset.y > 0.0 )
  277. {
  278. // The new near plane bottom side
  279. mNearBottom = nearPoint.y;
  280. }
  281. }
  282. }
  283. mDirty = true;
  284. // Indicate that we've modified the frustum
  285. return true;
  286. }
  287. //-----------------------------------------------------------------------------
  288. void FrustumData::_update() const
  289. {
  290. if( !mDirty )
  291. return;
  292. PROFILE_SCOPE( Frustum_update );
  293. const Point3F& cameraPos = mPosition;
  294. // Build the frustum points in camera space first.
  295. if( mIsOrtho )
  296. {
  297. mPoints[ NearTopLeft ].set( mNearLeft, mNearDist, mNearTop );
  298. mPoints[ NearTopRight ].set( mNearRight, mNearDist, mNearTop );
  299. mPoints[ NearBottomLeft ].set( mNearLeft, mNearDist, mNearBottom );
  300. mPoints[ NearBottomRight ].set( mNearRight, mNearDist, mNearBottom );
  301. mPoints[ FarTopLeft ].set( mNearLeft, mFarDist, mNearTop );
  302. mPoints[ FarTopRight ].set( mNearRight, mFarDist, mNearTop );
  303. mPoints[ FarBottomLeft ].set( mNearLeft, mFarDist, mNearBottom );
  304. mPoints[ FarBottomRight ].set( mNearRight, mFarDist, mNearBottom );
  305. }
  306. else
  307. {
  308. const F32 farOverNear = mFarDist / mNearDist;
  309. mPoints[ NearTopLeft ].set( mNearLeft, mNearDist, mNearTop );
  310. mPoints[ NearTopRight ].set( mNearRight, mNearDist, mNearTop );
  311. mPoints[ NearBottomLeft ].set( mNearLeft, mNearDist, mNearBottom );
  312. mPoints[ NearBottomRight ].set( mNearRight, mNearDist, mNearBottom );
  313. mPoints[ FarTopLeft ].set( mNearLeft * farOverNear, mFarDist, mNearTop * farOverNear );
  314. mPoints[ FarTopRight ].set( mNearRight * farOverNear, mFarDist, mNearTop * farOverNear );
  315. mPoints[ FarBottomLeft ].set( mNearLeft * farOverNear, mFarDist, mNearBottom * farOverNear );
  316. mPoints[ FarBottomRight ].set( mNearRight * farOverNear, mFarDist, mNearBottom * farOverNear );
  317. }
  318. // Transform the points into the desired culling space.
  319. for( U32 i = 0; i < mPoints.size(); ++ i )
  320. mTransform.mulP( mPoints[ i ] );
  321. // Update the axis aligned bounding box from
  322. // the newly transformed points.
  323. mBounds = Box3F::aroundPoints( mPoints.address(), mPoints.size() );
  324. // Finally build the planes.
  325. if( mIsOrtho )
  326. {
  327. mPlanes[ PlaneLeft ].set( mPoints[ NearBottomLeft ],
  328. mPoints[ FarTopLeft ],
  329. mPoints[ FarBottomLeft ] );
  330. mPlanes[ PlaneRight ].set( mPoints[ NearTopRight ],
  331. mPoints[ FarBottomRight ],
  332. mPoints[ FarTopRight ] );
  333. mPlanes[ PlaneTop ].set( mPoints[ FarTopRight ],
  334. mPoints[ NearTopLeft ],
  335. mPoints[ NearTopRight ] );
  336. mPlanes[ PlaneBottom ].set( mPoints[ NearBottomRight ],
  337. mPoints[ FarBottomLeft ],
  338. mPoints[ FarBottomRight ] );
  339. mPlanes[ PlaneNear ].set( mPoints[ NearTopLeft ],
  340. mPoints[ NearBottomLeft ],
  341. mPoints[ NearTopRight ] );
  342. mPlanes[ PlaneFar ].set( mPoints[ FarTopLeft ],
  343. mPoints[ FarTopRight ],
  344. mPoints[ FarBottomLeft ] );
  345. }
  346. else
  347. {
  348. mPlanes[ PlaneLeft ].set( cameraPos,
  349. mPoints[ NearTopLeft ],
  350. mPoints[ NearBottomLeft ] );
  351. mPlanes[ PlaneRight ].set( cameraPos,
  352. mPoints[ NearBottomRight ],
  353. mPoints[ NearTopRight ] );
  354. mPlanes[ PlaneTop ].set( cameraPos,
  355. mPoints[ NearTopRight ],
  356. mPoints[ NearTopLeft ] );
  357. mPlanes[ PlaneBottom ].set( cameraPos,
  358. mPoints[ NearBottomLeft ],
  359. mPoints[ NearBottomRight ] );
  360. mPlanes[ PlaneNear ].set( mPoints[ NearTopLeft ],
  361. mPoints[ NearBottomLeft ],
  362. mPoints[ NearTopRight ] );
  363. mPlanes[ PlaneFar ].set( mPoints[ FarTopLeft ],
  364. mPoints[ FarTopRight ],
  365. mPoints[ FarBottomLeft ] );
  366. }
  367. // If the frustum plane orientation doesn't match mIsInverted
  368. // now, invert all the plane normals.
  369. //
  370. // Note that if we have a transform matrix with a negative scale,
  371. // then the initial planes we have computed will always be inverted.
  372. const bool inverted = mPlanes[ PlaneNear ].whichSide( cameraPos ) == PlaneF::Front;
  373. if( inverted != mIsInverted )
  374. {
  375. for( U32 i = 0; i < mPlanes.size(); ++ i )
  376. mPlanes[ i ].invert();
  377. }
  378. AssertFatal( mPlanes[ PlaneNear ].whichSide( cameraPos ) != PlaneF::Front,
  379. "Frustum::_update - Viewpoint lies on front side of near plane!" );
  380. // And now the center points which are mostly just used in debug rendering.
  381. mPlaneCenters[ PlaneLeftCenter ] = ( mPoints[ NearTopLeft ] +
  382. mPoints[ NearBottomLeft ] +
  383. mPoints[ FarTopLeft ] +
  384. mPoints[ FarBottomLeft ] ) / 4.0f;
  385. mPlaneCenters[ PlaneRightCenter ] = ( mPoints[ NearTopRight ] +
  386. mPoints[ NearBottomRight ] +
  387. mPoints[ FarTopRight ] +
  388. mPoints[ FarBottomRight ] ) / 4.0f;
  389. mPlaneCenters[ PlaneTopCenter ] = ( mPoints[ NearTopLeft ] +
  390. mPoints[ NearTopRight ] +
  391. mPoints[ FarTopLeft ] +
  392. mPoints[ FarTopRight ] ) / 4.0f;
  393. mPlaneCenters[ PlaneBottomCenter ] = ( mPoints[ NearBottomLeft ] +
  394. mPoints[ NearBottomRight ] +
  395. mPoints[ FarBottomLeft ] +
  396. mPoints[ FarBottomRight ] ) / 4.0f;
  397. mPlaneCenters[ PlaneNearCenter ] = ( mPoints[ NearTopLeft ] +
  398. mPoints[ NearTopRight ] +
  399. mPoints[ NearBottomLeft ] +
  400. mPoints[ NearBottomRight ] ) / 4.0f;
  401. mPlaneCenters[ PlaneFarCenter ] = ( mPoints[ FarTopLeft ] +
  402. mPoints[ FarTopRight ] +
  403. mPoints[ FarBottomLeft ] +
  404. mPoints[ FarBottomRight ] ) / 4.0f;
  405. // Done.
  406. mDirty = false;
  407. }
  408. //-----------------------------------------------------------------------------
  409. void Frustum::invert()
  410. {
  411. mIsInverted = !mIsInverted;
  412. _update();
  413. }
  414. //-----------------------------------------------------------------------------
  415. void Frustum::setTransform( const MatrixF &mat )
  416. {
  417. mTransform = mat;
  418. mPosition = mTransform.getPosition();
  419. mDirty = true;
  420. }
  421. //-----------------------------------------------------------------------------
  422. void Frustum::scaleFromCenter( F32 scale )
  423. {
  424. // Extract the fov and aspect ratio.
  425. F32 fovInRadians = mAtan2( (mNearTop - mNearBottom)*mNumTiles/2.0f, mNearDist ) * 2.0f;
  426. F32 aspectRatio = (mNearRight - mNearLeft)/(mNearTop - mNearBottom);
  427. // Now move the near and far planes out.
  428. F32 halfDist = ( mFarDist - mNearDist ) / 2.0f;
  429. mNearDist -= halfDist * ( scale - 1.0f );
  430. mFarDist += halfDist * ( scale - 1.0f );
  431. // Setup the new scaled frustum.
  432. set( mIsOrtho, fovInRadians, aspectRatio, mNearDist, mFarDist, mTransform );
  433. }
  434. //-----------------------------------------------------------------------------
  435. void Frustum::mul( const MatrixF& mat )
  436. {
  437. mTransform.mul( mat );
  438. mDirty = true;
  439. }
  440. //-----------------------------------------------------------------------------
  441. void Frustum::mulL( const MatrixF& mat )
  442. {
  443. MatrixF last( mTransform );
  444. mTransform.mul( mat, last );
  445. mDirty = true;
  446. }
  447. //-----------------------------------------------------------------------------
  448. void Frustum::setProjectionOffset(const Point2F& offsetMat)
  449. {
  450. mProjectionOffset = offsetMat;
  451. mProjectionOffsetMatrix.identity();
  452. mProjectionOffsetMatrix.setPosition(Point3F(mProjectionOffset.x, mProjectionOffset.y, 0.0f));
  453. }
  454. //-----------------------------------------------------------------------------
  455. void Frustum::getProjectionMatrix( MatrixF *proj, bool gfxRotate ) const
  456. {
  457. if (mIsOrtho)
  458. {
  459. MathUtils::makeOrthoProjection(proj, mNearLeft, mNearRight, mNearTop, mNearBottom, mNearDist, mFarDist, gfxRotate);
  460. proj->mulL(mProjectionOffsetMatrix);
  461. }
  462. else
  463. {
  464. MathUtils::makeProjection(proj, mNearLeft, mNearRight, mNearTop, mNearBottom, mNearDist, mFarDist, gfxRotate);
  465. proj->mulL(mProjectionOffsetMatrix);
  466. }
  467. }
  468. //-----------------------------------------------------------------------------
  469. void Frustum::tileFrustum(U32 numTiles, const Point2I& curTile, Point2F overlap)
  470. {
  471. //These will be stored to re-tile the frustum if needed
  472. mNumTiles = numTiles;
  473. mCurrTile = curTile;
  474. mTileOverlap = overlap;
  475. tile(&mNearLeft, &mNearRight, &mNearTop, &mNearBottom, mNumTiles, mCurrTile, mTileOverlap);
  476. }
  477. //-----------------------------------------------------------------------------
  478. void Frustum::tile( F32 *left, F32 *right, F32 *top, F32 *bottom, U32 numTiles, const Point2I& curTile, Point2F overlap )
  479. {
  480. if (numTiles == 1)
  481. return;
  482. Point2F tileSize( ( *right - *left ) / (F32)numTiles,
  483. ( *top - *bottom ) / (F32)numTiles );
  484. F32 leftOffset = tileSize.x*overlap.x;
  485. F32 rightOffset = tileSize.x*overlap.x*2;
  486. F32 bottomOffset = tileSize.y*overlap.y;
  487. F32 topOffset = tileSize.y*overlap.y*2;
  488. *left += tileSize.x * curTile.x - leftOffset;
  489. *right = *left + tileSize.x + rightOffset;
  490. *bottom += tileSize.y * curTile.y - bottomOffset;
  491. *top = *bottom + tileSize.y + topOffset;
  492. }