frustum.cpp 22 KB

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