frustum.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589
  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. set( mIsOrtho, getFov(), getAspectRatio(), nearDist, farDist, xfm );
  181. }
  182. //-----------------------------------------------------------------------------
  183. void Frustum::cropNearFar(F32 newNearDist, F32 newFarDist)
  184. {
  185. const F32 newOverOld = newNearDist / mNearDist;
  186. set( mIsOrtho, mNearLeft * newOverOld, mNearRight * newOverOld, mNearTop * newOverOld, mNearBottom * newOverOld,
  187. newNearDist, newFarDist, mTransform);
  188. }
  189. //-----------------------------------------------------------------------------
  190. bool Frustum::bakeProjectionOffset()
  191. {
  192. // Nothing to bake if ortho
  193. if( mIsOrtho )
  194. return false;
  195. // Nothing to bake if no offset
  196. if( mProjectionOffset.isZero() )
  197. return false;
  198. // Near plane points in camera space
  199. Point3F np[4];
  200. np[0].set( mNearLeft, mNearDist, mNearTop ); // NearTopLeft
  201. np[1].set( mNearRight, mNearDist, mNearTop ); // NearTopRight
  202. np[2].set( mNearLeft, mNearDist, mNearBottom ); // NearBottomLeft
  203. np[3].set( mNearRight, mNearDist, mNearBottom ); // NearBottomRight
  204. // Generate the near plane
  205. PlaneF nearPlane( np[0], np[1], np[3] );
  206. // Far plane points in camera space
  207. const F32 farOverNear = mFarDist / mNearDist;
  208. Point3F fp0( mNearLeft * farOverNear, mFarDist, mNearTop * farOverNear ); // FarTopLeft
  209. Point3F fp1( mNearRight * farOverNear, mFarDist, mNearTop * farOverNear ); // FarTopRight
  210. Point3F fp2( mNearLeft * farOverNear, mFarDist, mNearBottom * farOverNear ); // FarBottomLeft
  211. Point3F fp3( mNearRight * farOverNear, mFarDist, mNearBottom * farOverNear ); // FarBottomRight
  212. // Generate the far plane
  213. PlaneF farPlane( fp0, fp1, fp3 );
  214. // The offset camera point
  215. Point3F offsetCamera( mProjectionOffset.x, 0.0f, mProjectionOffset.y );
  216. // The near plane point we'll be using for our calculations below
  217. U32 nIndex = 0;
  218. if( mProjectionOffset.x < 0.0 )
  219. {
  220. // Offset to the left so we'll need to use the near plane point on the right
  221. nIndex = 1;
  222. }
  223. if( mProjectionOffset.y > 0.0 )
  224. {
  225. // Offset to the top so we'll need to use the near plane point at the bottom
  226. nIndex += 2;
  227. }
  228. // Begin by calculating the offset point on the far plane as it goes
  229. // from the offset camera to the edge of the near plane.
  230. Point3F farPoint;
  231. Point3F fdir = np[nIndex] - offsetCamera;
  232. fdir.normalize();
  233. if( farPlane.intersect(offsetCamera, fdir, &farPoint) )
  234. {
  235. // Calculate the new near plane edge from the non-offset camera position
  236. // to the far plane point from above.
  237. Point3F nearPoint;
  238. Point3F ndir = farPoint;
  239. ndir.normalize();
  240. if( nearPlane.intersect( Point3F::Zero, ndir, &nearPoint) )
  241. {
  242. // Handle a x offset
  243. if( mProjectionOffset.x < 0.0 )
  244. {
  245. // The new near plane right side
  246. mNearRight = nearPoint.x;
  247. }
  248. else if( mProjectionOffset.x > 0.0 )
  249. {
  250. // The new near plane left side
  251. mNearLeft = nearPoint.x;
  252. }
  253. // Handle a y offset
  254. if( mProjectionOffset.y < 0.0 )
  255. {
  256. // The new near plane top side
  257. mNearTop = nearPoint.y;
  258. }
  259. else if( mProjectionOffset.y > 0.0 )
  260. {
  261. // The new near plane bottom side
  262. mNearBottom = nearPoint.y;
  263. }
  264. }
  265. }
  266. mDirty = true;
  267. // Indicate that we've modified the frustum
  268. return true;
  269. }
  270. //-----------------------------------------------------------------------------
  271. void FrustumData::_update() const
  272. {
  273. if( !mDirty )
  274. return;
  275. PROFILE_SCOPE( Frustum_update );
  276. const Point3F& cameraPos = mPosition;
  277. // Build the frustum points in camera space first.
  278. if( mIsOrtho )
  279. {
  280. mPoints[ NearTopLeft ].set( mNearLeft, mNearDist, mNearTop );
  281. mPoints[ NearTopRight ].set( mNearRight, mNearDist, mNearTop );
  282. mPoints[ NearBottomLeft ].set( mNearLeft, mNearDist, mNearBottom );
  283. mPoints[ NearBottomRight ].set( mNearRight, mNearDist, mNearBottom );
  284. mPoints[ FarTopLeft ].set( mNearLeft, mFarDist, mNearTop );
  285. mPoints[ FarTopRight ].set( mNearRight, mFarDist, mNearTop );
  286. mPoints[ FarBottomLeft ].set( mNearLeft, mFarDist, mNearBottom );
  287. mPoints[ FarBottomRight ].set( mNearRight, mFarDist, mNearBottom );
  288. }
  289. else
  290. {
  291. const F32 farOverNear = mFarDist / mNearDist;
  292. mPoints[ NearTopLeft ].set( mNearLeft, mNearDist, mNearTop );
  293. mPoints[ NearTopRight ].set( mNearRight, mNearDist, mNearTop );
  294. mPoints[ NearBottomLeft ].set( mNearLeft, mNearDist, mNearBottom );
  295. mPoints[ NearBottomRight ].set( mNearRight, mNearDist, mNearBottom );
  296. mPoints[ FarTopLeft ].set( mNearLeft * farOverNear, mFarDist, mNearTop * farOverNear );
  297. mPoints[ FarTopRight ].set( mNearRight * farOverNear, mFarDist, mNearTop * farOverNear );
  298. mPoints[ FarBottomLeft ].set( mNearLeft * farOverNear, mFarDist, mNearBottom * farOverNear );
  299. mPoints[ FarBottomRight ].set( mNearRight * farOverNear, mFarDist, mNearBottom * farOverNear );
  300. }
  301. // Transform the points into the desired culling space.
  302. for( U32 i = 0; i < mPoints.size(); ++ i )
  303. mTransform.mulP( mPoints[ i ] );
  304. // Update the axis aligned bounding box from
  305. // the newly transformed points.
  306. mBounds = Box3F::aroundPoints( mPoints.address(), mPoints.size() );
  307. // Finally build the planes.
  308. if( mIsOrtho )
  309. {
  310. mPlanes[ PlaneLeft ].set( mPoints[ NearBottomLeft ],
  311. mPoints[ FarTopLeft ],
  312. mPoints[ FarBottomLeft ] );
  313. mPlanes[ PlaneRight ].set( mPoints[ NearTopRight ],
  314. mPoints[ FarBottomRight ],
  315. mPoints[ FarTopRight ] );
  316. mPlanes[ PlaneTop ].set( mPoints[ FarTopRight ],
  317. mPoints[ NearTopLeft ],
  318. mPoints[ NearTopRight ] );
  319. mPlanes[ PlaneBottom ].set( mPoints[ NearBottomRight ],
  320. mPoints[ FarBottomLeft ],
  321. mPoints[ FarBottomRight ] );
  322. mPlanes[ PlaneNear ].set( mPoints[ NearTopLeft ],
  323. mPoints[ NearBottomLeft ],
  324. mPoints[ NearTopRight ] );
  325. mPlanes[ PlaneFar ].set( mPoints[ FarTopLeft ],
  326. mPoints[ FarTopRight ],
  327. mPoints[ FarBottomLeft ] );
  328. }
  329. else
  330. {
  331. mPlanes[ PlaneLeft ].set( cameraPos,
  332. mPoints[ NearTopLeft ],
  333. mPoints[ NearBottomLeft ] );
  334. mPlanes[ PlaneRight ].set( cameraPos,
  335. mPoints[ NearBottomRight ],
  336. mPoints[ NearTopRight ] );
  337. mPlanes[ PlaneTop ].set( cameraPos,
  338. mPoints[ NearTopRight ],
  339. mPoints[ NearTopLeft ] );
  340. mPlanes[ PlaneBottom ].set( cameraPos,
  341. mPoints[ NearBottomLeft ],
  342. mPoints[ NearBottomRight ] );
  343. mPlanes[ PlaneNear ].set( mPoints[ NearTopLeft ],
  344. mPoints[ NearBottomLeft ],
  345. mPoints[ NearTopRight ] );
  346. mPlanes[ PlaneFar ].set( mPoints[ FarTopLeft ],
  347. mPoints[ FarTopRight ],
  348. mPoints[ FarBottomLeft ] );
  349. }
  350. // If the frustum plane orientation doesn't match mIsInverted
  351. // now, invert all the plane normals.
  352. //
  353. // Note that if we have a transform matrix with a negative scale,
  354. // then the initial planes we have computed will always be inverted.
  355. const bool inverted = mPlanes[ PlaneNear ].whichSide( cameraPos ) == PlaneF::Front;
  356. if( inverted != mIsInverted )
  357. {
  358. for( U32 i = 0; i < mPlanes.size(); ++ i )
  359. mPlanes[ i ].invert();
  360. }
  361. AssertFatal( mPlanes[ PlaneNear ].whichSide( cameraPos ) != PlaneF::Front,
  362. "Frustum::_update - Viewpoint lies on front side of near plane!" );
  363. // And now the center points which are mostly just used in debug rendering.
  364. mPlaneCenters[ PlaneLeftCenter ] = ( mPoints[ NearTopLeft ] +
  365. mPoints[ NearBottomLeft ] +
  366. mPoints[ FarTopLeft ] +
  367. mPoints[ FarBottomLeft ] ) / 4.0f;
  368. mPlaneCenters[ PlaneRightCenter ] = ( mPoints[ NearTopRight ] +
  369. mPoints[ NearBottomRight ] +
  370. mPoints[ FarTopRight ] +
  371. mPoints[ FarBottomRight ] ) / 4.0f;
  372. mPlaneCenters[ PlaneTopCenter ] = ( mPoints[ NearTopLeft ] +
  373. mPoints[ NearTopRight ] +
  374. mPoints[ FarTopLeft ] +
  375. mPoints[ FarTopRight ] ) / 4.0f;
  376. mPlaneCenters[ PlaneBottomCenter ] = ( mPoints[ NearBottomLeft ] +
  377. mPoints[ NearBottomRight ] +
  378. mPoints[ FarBottomLeft ] +
  379. mPoints[ FarBottomRight ] ) / 4.0f;
  380. mPlaneCenters[ PlaneNearCenter ] = ( mPoints[ NearTopLeft ] +
  381. mPoints[ NearTopRight ] +
  382. mPoints[ NearBottomLeft ] +
  383. mPoints[ NearBottomRight ] ) / 4.0f;
  384. mPlaneCenters[ PlaneFarCenter ] = ( mPoints[ FarTopLeft ] +
  385. mPoints[ FarTopRight ] +
  386. mPoints[ FarBottomLeft ] +
  387. mPoints[ FarBottomRight ] ) / 4.0f;
  388. // Done.
  389. mDirty = false;
  390. }
  391. //-----------------------------------------------------------------------------
  392. void Frustum::invert()
  393. {
  394. mIsInverted = !mIsInverted;
  395. _update();
  396. }
  397. //-----------------------------------------------------------------------------
  398. void Frustum::setTransform( const MatrixF &mat )
  399. {
  400. mTransform = mat;
  401. mPosition = mTransform.getPosition();
  402. mDirty = true;
  403. }
  404. //-----------------------------------------------------------------------------
  405. void Frustum::scaleFromCenter( F32 scale )
  406. {
  407. // Extract the fov and aspect ratio.
  408. F32 fovInRadians = mAtan2( (mNearTop - mNearBottom)*mNumTiles/2.0f, mNearDist ) * 2.0f;
  409. F32 aspectRatio = (mNearRight - mNearLeft)/(mNearTop - mNearBottom);
  410. // Now move the near and far planes out.
  411. F32 halfDist = ( mFarDist - mNearDist ) / 2.0f;
  412. mNearDist -= halfDist * ( scale - 1.0f );
  413. mFarDist += halfDist * ( scale - 1.0f );
  414. // Setup the new scaled frustum.
  415. set( mIsOrtho, fovInRadians, aspectRatio, mNearDist, mFarDist, mTransform );
  416. }
  417. //-----------------------------------------------------------------------------
  418. void Frustum::mul( const MatrixF& mat )
  419. {
  420. mTransform.mul( mat );
  421. mDirty = true;
  422. }
  423. //-----------------------------------------------------------------------------
  424. void Frustum::mulL( const MatrixF& mat )
  425. {
  426. MatrixF last( mTransform );
  427. mTransform.mul( mat, last );
  428. mDirty = true;
  429. }
  430. //-----------------------------------------------------------------------------
  431. void Frustum::setProjectionOffset(const Point2F& offsetMat)
  432. {
  433. mProjectionOffset = offsetMat;
  434. mProjectionOffsetMatrix.identity();
  435. mProjectionOffsetMatrix.setPosition(Point3F(mProjectionOffset.x, mProjectionOffset.y, 0.0f));
  436. }
  437. //-----------------------------------------------------------------------------
  438. void Frustum::getProjectionMatrix( MatrixF *proj, bool gfxRotate ) const
  439. {
  440. if (mIsOrtho)
  441. {
  442. MathUtils::makeOrthoProjection(proj, mNearLeft, mNearRight, mNearTop, mNearBottom, mNearDist, mFarDist, gfxRotate);
  443. proj->mulL(mProjectionOffsetMatrix);
  444. }
  445. else
  446. {
  447. MathUtils::makeProjection(proj, mNearLeft, mNearRight, mNearTop, mNearBottom, mNearDist, mFarDist, gfxRotate);
  448. proj->mulL(mProjectionOffsetMatrix);
  449. }
  450. }
  451. //-----------------------------------------------------------------------------
  452. void Frustum::tileFrustum(U32 numTiles, const Point2I& curTile, Point2F overlap)
  453. {
  454. //These will be stored to re-tile the frustum if needed
  455. mNumTiles = numTiles;
  456. mCurrTile = curTile;
  457. mTileOverlap = overlap;
  458. tile(&mNearLeft, &mNearRight, &mNearTop, &mNearBottom, mNumTiles, mCurrTile, mTileOverlap);
  459. }
  460. //-----------------------------------------------------------------------------
  461. void Frustum::tile( F32 *left, F32 *right, F32 *top, F32 *bottom, U32 numTiles, const Point2I& curTile, Point2F overlap )
  462. {
  463. if (numTiles == 1)
  464. return;
  465. Point2F tileSize( ( *right - *left ) / (F32)numTiles,
  466. ( *top - *bottom ) / (F32)numTiles );
  467. F32 leftOffset = tileSize.x*overlap.x;
  468. F32 rightOffset = tileSize.x*overlap.x*2;
  469. F32 bottomOffset = tileSize.y*overlap.y;
  470. F32 topOffset = tileSize.y*overlap.y*2;
  471. *left += tileSize.x * curTile.x - leftOffset;
  472. *right = *left + tileSize.x + rightOffset;
  473. *bottom += tileSize.y * curTile.y - bottomOffset;
  474. *top = *bottom + tileSize.y + topOffset;
  475. }