frustum.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493
  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. void FrustumData::_update() const
  190. {
  191. if( !mDirty )
  192. return;
  193. PROFILE_SCOPE( Frustum_update );
  194. const Point3F& cameraPos = mPosition;
  195. // Build the frustum points in camera space first.
  196. if( mIsOrtho )
  197. {
  198. mPoints[ NearTopLeft ].set( mNearLeft, mNearDist, mNearTop );
  199. mPoints[ NearTopRight ].set( mNearRight, mNearDist, mNearTop );
  200. mPoints[ NearBottomLeft ].set( mNearLeft, mNearDist, mNearBottom );
  201. mPoints[ NearBottomRight ].set( mNearRight, mNearDist, mNearBottom );
  202. mPoints[ FarTopLeft ].set( mNearLeft, mFarDist, mNearTop );
  203. mPoints[ FarTopRight ].set( mNearRight, mFarDist, mNearTop );
  204. mPoints[ FarBottomLeft ].set( mNearLeft, mFarDist, mNearBottom );
  205. mPoints[ FarBottomRight ].set( mNearRight, mFarDist, mNearBottom );
  206. }
  207. else
  208. {
  209. const F32 farOverNear = mFarDist / mNearDist;
  210. mPoints[ NearTopLeft ].set( mNearLeft, mNearDist, mNearTop );
  211. mPoints[ NearTopRight ].set( mNearRight, mNearDist, mNearTop );
  212. mPoints[ NearBottomLeft ].set( mNearLeft, mNearDist, mNearBottom );
  213. mPoints[ NearBottomRight ].set( mNearRight, mNearDist, mNearBottom );
  214. mPoints[ FarTopLeft ].set( mNearLeft * farOverNear, mFarDist, mNearTop * farOverNear );
  215. mPoints[ FarTopRight ].set( mNearRight * farOverNear, mFarDist, mNearTop * farOverNear );
  216. mPoints[ FarBottomLeft ].set( mNearLeft * farOverNear, mFarDist, mNearBottom * farOverNear );
  217. mPoints[ FarBottomRight ].set( mNearRight * farOverNear, mFarDist, mNearBottom * farOverNear );
  218. }
  219. // Transform the points into the desired culling space.
  220. for( U32 i = 0; i < mPoints.size(); ++ i )
  221. mTransform.mulP( mPoints[ i ] );
  222. // Update the axis aligned bounding box from
  223. // the newly transformed points.
  224. mBounds = Box3F::aroundPoints( mPoints.address(), mPoints.size() );
  225. // Finally build the planes.
  226. if( mIsOrtho )
  227. {
  228. mPlanes[ PlaneLeft ].set( mPoints[ NearBottomLeft ],
  229. mPoints[ FarTopLeft ],
  230. mPoints[ FarBottomLeft ] );
  231. mPlanes[ PlaneRight ].set( mPoints[ NearTopRight ],
  232. mPoints[ FarBottomRight ],
  233. mPoints[ FarTopRight ] );
  234. mPlanes[ PlaneTop ].set( mPoints[ FarTopRight ],
  235. mPoints[ NearTopLeft ],
  236. mPoints[ NearTopRight ] );
  237. mPlanes[ PlaneBottom ].set( mPoints[ NearBottomRight ],
  238. mPoints[ FarBottomLeft ],
  239. mPoints[ FarBottomRight ] );
  240. mPlanes[ PlaneNear ].set( mPoints[ NearTopLeft ],
  241. mPoints[ NearBottomLeft ],
  242. mPoints[ NearTopRight ] );
  243. mPlanes[ PlaneFar ].set( mPoints[ FarTopLeft ],
  244. mPoints[ FarTopRight ],
  245. mPoints[ FarBottomLeft ] );
  246. }
  247. else
  248. {
  249. mPlanes[ PlaneLeft ].set( cameraPos,
  250. mPoints[ NearTopLeft ],
  251. mPoints[ NearBottomLeft ] );
  252. mPlanes[ PlaneRight ].set( cameraPos,
  253. mPoints[ NearBottomRight ],
  254. mPoints[ NearTopRight ] );
  255. mPlanes[ PlaneTop ].set( cameraPos,
  256. mPoints[ NearTopRight ],
  257. mPoints[ NearTopLeft ] );
  258. mPlanes[ PlaneBottom ].set( cameraPos,
  259. mPoints[ NearBottomLeft ],
  260. mPoints[ NearBottomRight ] );
  261. mPlanes[ PlaneNear ].set( mPoints[ NearTopLeft ],
  262. mPoints[ NearBottomLeft ],
  263. mPoints[ NearTopRight ] );
  264. mPlanes[ PlaneFar ].set( mPoints[ FarTopLeft ],
  265. mPoints[ FarTopRight ],
  266. mPoints[ FarBottomLeft ] );
  267. }
  268. // If the frustum plane orientation doesn't match mIsInverted
  269. // now, invert all the plane normals.
  270. //
  271. // Note that if we have a transform matrix with a negative scale,
  272. // then the initial planes we have computed will always be inverted.
  273. const bool inverted = mPlanes[ PlaneNear ].whichSide( cameraPos ) == PlaneF::Front;
  274. if( inverted != mIsInverted )
  275. {
  276. for( U32 i = 0; i < mPlanes.size(); ++ i )
  277. mPlanes[ i ].invert();
  278. }
  279. AssertFatal( mPlanes[ PlaneNear ].whichSide( cameraPos ) != PlaneF::Front,
  280. "Frustum::_update - Viewpoint lies on front side of near plane!" );
  281. // And now the center points which are mostly just used in debug rendering.
  282. mPlaneCenters[ PlaneLeftCenter ] = ( mPoints[ NearTopLeft ] +
  283. mPoints[ NearBottomLeft ] +
  284. mPoints[ FarTopLeft ] +
  285. mPoints[ FarBottomLeft ] ) / 4.0f;
  286. mPlaneCenters[ PlaneRightCenter ] = ( mPoints[ NearTopRight ] +
  287. mPoints[ NearBottomRight ] +
  288. mPoints[ FarTopRight ] +
  289. mPoints[ FarBottomRight ] ) / 4.0f;
  290. mPlaneCenters[ PlaneTopCenter ] = ( mPoints[ NearTopLeft ] +
  291. mPoints[ NearTopRight ] +
  292. mPoints[ FarTopLeft ] +
  293. mPoints[ FarTopRight ] ) / 4.0f;
  294. mPlaneCenters[ PlaneBottomCenter ] = ( mPoints[ NearBottomLeft ] +
  295. mPoints[ NearBottomRight ] +
  296. mPoints[ FarBottomLeft ] +
  297. mPoints[ FarBottomRight ] ) / 4.0f;
  298. mPlaneCenters[ PlaneNearCenter ] = ( mPoints[ NearTopLeft ] +
  299. mPoints[ NearTopRight ] +
  300. mPoints[ NearBottomLeft ] +
  301. mPoints[ NearBottomRight ] ) / 4.0f;
  302. mPlaneCenters[ PlaneFarCenter ] = ( mPoints[ FarTopLeft ] +
  303. mPoints[ FarTopRight ] +
  304. mPoints[ FarBottomLeft ] +
  305. mPoints[ FarBottomRight ] ) / 4.0f;
  306. // Done.
  307. mDirty = false;
  308. }
  309. //-----------------------------------------------------------------------------
  310. void Frustum::invert()
  311. {
  312. mIsInverted = !mIsInverted;
  313. _update();
  314. }
  315. //-----------------------------------------------------------------------------
  316. void Frustum::setTransform( const MatrixF &mat )
  317. {
  318. mTransform = mat;
  319. mPosition = mTransform.getPosition();
  320. mDirty = true;
  321. }
  322. //-----------------------------------------------------------------------------
  323. void Frustum::scaleFromCenter( F32 scale )
  324. {
  325. // Extract the fov and aspect ratio.
  326. F32 fovInRadians = mAtan2( (mNearTop - mNearBottom)*mNumTiles/2.0f, mNearDist ) * 2.0f;
  327. F32 aspectRatio = (mNearRight - mNearLeft)/(mNearTop - mNearBottom);
  328. // Now move the near and far planes out.
  329. F32 halfDist = ( mFarDist - mNearDist ) / 2.0f;
  330. mNearDist -= halfDist * ( scale - 1.0f );
  331. mFarDist += halfDist * ( scale - 1.0f );
  332. // Setup the new scaled frustum.
  333. set( mIsOrtho, fovInRadians, aspectRatio, mNearDist, mFarDist, mTransform );
  334. }
  335. //-----------------------------------------------------------------------------
  336. void Frustum::mul( const MatrixF& mat )
  337. {
  338. mTransform.mul( mat );
  339. mDirty = true;
  340. }
  341. //-----------------------------------------------------------------------------
  342. void Frustum::mulL( const MatrixF& mat )
  343. {
  344. MatrixF last( mTransform );
  345. mTransform.mul( mat, last );
  346. mDirty = true;
  347. }
  348. //-----------------------------------------------------------------------------
  349. void Frustum::setProjectionOffset(const Point2F& offsetMat)
  350. {
  351. mProjectionOffset = offsetMat;
  352. mProjectionOffsetMatrix.identity();
  353. mProjectionOffsetMatrix.setPosition(Point3F(mProjectionOffset.x, mProjectionOffset.y, 0.0f));
  354. }
  355. //-----------------------------------------------------------------------------
  356. void Frustum::getProjectionMatrix( MatrixF *proj, bool gfxRotate ) const
  357. {
  358. if (mIsOrtho)
  359. {
  360. MathUtils::makeOrthoProjection(proj, mNearLeft, mNearRight, mNearTop, mNearBottom, mNearDist, mFarDist, gfxRotate);
  361. proj->mulL(mProjectionOffsetMatrix);
  362. }
  363. else
  364. {
  365. MathUtils::makeProjection(proj, mNearLeft, mNearRight, mNearTop, mNearBottom, mNearDist, mFarDist, gfxRotate);
  366. proj->mulL(mProjectionOffsetMatrix);
  367. }
  368. }
  369. //-----------------------------------------------------------------------------
  370. void Frustum::tileFrustum(U32 numTiles, const Point2I& curTile, Point2F overlap)
  371. {
  372. //These will be stored to re-tile the frustum if needed
  373. mNumTiles = numTiles;
  374. mCurrTile = curTile;
  375. mTileOverlap = overlap;
  376. tile(&mNearLeft, &mNearRight, &mNearTop, &mNearBottom, mNumTiles, mCurrTile, mTileOverlap);
  377. }
  378. //-----------------------------------------------------------------------------
  379. void Frustum::tile( F32 *left, F32 *right, F32 *top, F32 *bottom, U32 numTiles, const Point2I& curTile, Point2F overlap )
  380. {
  381. if (numTiles == 1)
  382. return;
  383. Point2F tileSize( ( *right - *left ) / (F32)numTiles,
  384. ( *top - *bottom ) / (F32)numTiles );
  385. F32 leftOffset = tileSize.x*overlap.x;
  386. F32 rightOffset = tileSize.x*overlap.x*2;
  387. F32 bottomOffset = tileSize.y*overlap.y;
  388. F32 topOffset = tileSize.y*overlap.y*2;
  389. *left += tileSize.x * curTile.x - leftOffset;
  390. *right = *left + tileSize.x + rightOffset;
  391. *bottom += tileSize.y * curTile.y - bottomOffset;
  392. *top = *bottom + tileSize.y + topOffset;
  393. }