frustum.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475
  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. }
  69. //-----------------------------------------------------------------------------
  70. void Frustum::set( bool isOrtho,
  71. F32 fovYInRadians,
  72. F32 aspectRatio,
  73. F32 nearDist,
  74. F32 farDist,
  75. const MatrixF &transform )
  76. {
  77. F32 left, right, top, bottom;
  78. MathUtils::makeFrustum( &left, &right, &top, &bottom, fovYInRadians, aspectRatio, nearDist );
  79. tile( &left, &right, &top, &bottom, mNumTiles, mCurrTile, mTileOverlap );
  80. set( isOrtho, left, right, top, bottom, nearDist, farDist, transform );
  81. }
  82. //-----------------------------------------------------------------------------
  83. void Frustum::set( bool isOrtho,
  84. F32 nearLeft,
  85. F32 nearRight,
  86. F32 nearTop,
  87. F32 nearBottom,
  88. F32 nearDist,
  89. F32 farDist,
  90. const MatrixF &transform )
  91. {
  92. mTransform = transform;
  93. mPosition = mTransform.getPosition();
  94. mNearLeft = nearLeft;
  95. mNearRight = nearRight;
  96. mNearTop = nearTop;
  97. mNearBottom = nearBottom;
  98. mNearDist = nearDist;
  99. mFarDist = farDist;
  100. mIsOrtho = isOrtho;
  101. mDirty = true;
  102. }
  103. //-----------------------------------------------------------------------------
  104. #if 0
  105. void Frustum::set( const MatrixF &projMat, bool normalize )
  106. {
  107. // From "Fast Extraction of Viewing Frustum Planes from the World-View-Projection Matrix"
  108. // by Gil Gribb and Klaus Hartmann.
  109. //
  110. // http://www2.ravensoft.com/users/ggribb/plane%20extraction.pdf
  111. // Right clipping plane.
  112. mPlanes[ PlaneRight ].set( projMat[3] - projMat[0],
  113. projMat[7] - projMat[4],
  114. projMat[11] - projMat[8],
  115. projMat[15] - projMat[12] );
  116. // Left clipping plane.
  117. mPlanes[ PlaneLeft ].set( projMat[3] + projMat[0],
  118. projMat[7] + projMat[4],
  119. projMat[11] + projMat[8],
  120. projMat[15] + projMat[12] );
  121. // Bottom clipping plane.
  122. mPlanes[ PlaneBottom ].set( projMat[3] + projMat[1],
  123. projMat[7] + projMat[5],
  124. projMat[11] + projMat[9],
  125. projMat[15] + projMat[13] );
  126. // Top clipping plane.
  127. mPlanes[ PlaneTop ].set( projMat[3] - projMat[1],
  128. projMat[7] - projMat[5],
  129. projMat[11] - projMat[9],
  130. projMat[15] - projMat[13] );
  131. // Near clipping plane
  132. mPlanes[ PlaneNear ].set( projMat[3] + projMat[2],
  133. projMat[7] + projMat[6],
  134. projMat[11] + projMat[10],
  135. projMat[15] + projMat[14] );
  136. // Far clipping plane.
  137. mPlanes[ PlaneFar ].set( projMat[3] - projMat[2],
  138. projMat[7] - projMat[6],
  139. projMat[11] - projMat[10],
  140. projMat[15] - projMat[14] );
  141. if( normalize )
  142. {
  143. for( S32 i = 0; i < PlaneCount; ++ i )
  144. mPlanes[ i ].normalize();
  145. }
  146. /* // Create the corner points via plane intersections.
  147. mPlanes[ PlaneNear ].intersect( mPlanes[ PlaneTop ], mPlanes[ PlaneLeft ], &mPoints[ NearTopLeft ] );
  148. mPlanes[ PlaneNear ].intersect( mPlanes[ PlaneTop ], mPlanes[ PlaneRight ], &mPoints[ NearTopRight ] );
  149. mPlanes[ PlaneNear ].intersect( mPlanes[ PlaneBottom ], mPlanes[ PlaneLeft ], &mPoints[ NearBottomLeft ] );
  150. mPlanes[ PlaneNear ].intersect( mPlanes[ PlaneBottom ], mPlanes[ PlaneRight ], &mPoints[ NearBottomRight ] );
  151. mPlanes[ PlaneFar ].intersect( mPlanes[ PlaneTop ], mPlanes[ PlaneLeft ], &mPoints[ FarTopLeft ] );
  152. mPlanes[ PlaneFar ].intersect( mPlanes[ PlaneTop ], mPlanes[ PlaneRight ], &mPoints[ FarTopRight ] );
  153. mPlanes[ PlaneFar ].intersect( mPlanes[ PlaneBottom ], mPlanes[ PlaneLeft ], &mPoints[ FarBottomLeft ] );
  154. mPlanes[ PlaneFar ].intersect( mPlanes[ PlaneBottom ], mPlanes[ PlaneRight ], &mPoints[ FarBottomRight ] );
  155. */
  156. // Update the axis aligned bounding box.
  157. _updateBounds();
  158. }
  159. #endif
  160. //-----------------------------------------------------------------------------
  161. void Frustum::setNearDist( F32 nearDist )
  162. {
  163. setNearFarDist( nearDist, mFarDist );
  164. }
  165. //-----------------------------------------------------------------------------
  166. void Frustum::setFarDist( F32 farDist )
  167. {
  168. setNearFarDist( mNearDist, farDist );
  169. }
  170. //-----------------------------------------------------------------------------
  171. void Frustum::setNearFarDist( F32 nearDist, F32 farDist )
  172. {
  173. if( mNearDist == nearDist && mFarDist == farDist )
  174. return;
  175. // Recalculate the frustum.
  176. MatrixF xfm( mTransform );
  177. set( mIsOrtho, getFov(), getAspectRatio(), nearDist, farDist, xfm );
  178. }
  179. //-----------------------------------------------------------------------------
  180. void Frustum::cropNearFar(F32 newNearDist, F32 newFarDist)
  181. {
  182. const F32 newOverOld = newNearDist / mNearDist;
  183. set( mIsOrtho, mNearLeft * newOverOld, mNearRight * newOverOld, mNearTop * newOverOld, mNearBottom * newOverOld,
  184. newNearDist, newFarDist, mTransform);
  185. }
  186. //-----------------------------------------------------------------------------
  187. void FrustumData::_update() const
  188. {
  189. if( !mDirty )
  190. return;
  191. PROFILE_SCOPE( Frustum_update );
  192. const Point3F& cameraPos = mPosition;
  193. // Build the frustum points in camera space first.
  194. if( mIsOrtho )
  195. {
  196. mPoints[ NearTopLeft ].set( mNearLeft, mNearDist, mNearTop );
  197. mPoints[ NearTopRight ].set( mNearRight, mNearDist, mNearTop );
  198. mPoints[ NearBottomLeft ].set( mNearLeft, mNearDist, mNearBottom );
  199. mPoints[ NearBottomRight ].set( mNearRight, mNearDist, mNearBottom );
  200. mPoints[ FarTopLeft ].set( mNearLeft, mFarDist, mNearTop );
  201. mPoints[ FarTopRight ].set( mNearRight, mFarDist, mNearTop );
  202. mPoints[ FarBottomLeft ].set( mNearLeft, mFarDist, mNearBottom );
  203. mPoints[ FarBottomRight ].set( mNearRight, mFarDist, mNearBottom );
  204. }
  205. else
  206. {
  207. const F32 farOverNear = mFarDist / mNearDist;
  208. mPoints[ NearTopLeft ].set( mNearLeft, mNearDist, mNearTop );
  209. mPoints[ NearTopRight ].set( mNearRight, mNearDist, mNearTop );
  210. mPoints[ NearBottomLeft ].set( mNearLeft, mNearDist, mNearBottom );
  211. mPoints[ NearBottomRight ].set( mNearRight, mNearDist, mNearBottom );
  212. mPoints[ FarTopLeft ].set( mNearLeft * farOverNear, mFarDist, mNearTop * farOverNear );
  213. mPoints[ FarTopRight ].set( mNearRight * farOverNear, mFarDist, mNearTop * farOverNear );
  214. mPoints[ FarBottomLeft ].set( mNearLeft * farOverNear, mFarDist, mNearBottom * farOverNear );
  215. mPoints[ FarBottomRight ].set( mNearRight * farOverNear, mFarDist, mNearBottom * farOverNear );
  216. }
  217. // Transform the points into the desired culling space.
  218. for( U32 i = 0; i < mPoints.size(); ++ i )
  219. mTransform.mulP( mPoints[ i ] );
  220. // Update the axis aligned bounding box from
  221. // the newly transformed points.
  222. mBounds = Box3F::aroundPoints( mPoints.address(), mPoints.size() );
  223. // Finally build the planes.
  224. if( mIsOrtho )
  225. {
  226. mPlanes[ PlaneLeft ].set( mPoints[ NearBottomLeft ],
  227. mPoints[ FarTopLeft ],
  228. mPoints[ FarBottomLeft ] );
  229. mPlanes[ PlaneRight ].set( mPoints[ NearTopRight ],
  230. mPoints[ FarBottomRight ],
  231. mPoints[ FarTopRight ] );
  232. mPlanes[ PlaneTop ].set( mPoints[ FarTopRight ],
  233. mPoints[ NearTopLeft ],
  234. mPoints[ NearTopRight ] );
  235. mPlanes[ PlaneBottom ].set( mPoints[ NearBottomRight ],
  236. mPoints[ FarBottomLeft ],
  237. mPoints[ FarBottomRight ] );
  238. mPlanes[ PlaneNear ].set( mPoints[ NearTopLeft ],
  239. mPoints[ NearBottomLeft ],
  240. mPoints[ NearTopRight ] );
  241. mPlanes[ PlaneFar ].set( mPoints[ FarTopLeft ],
  242. mPoints[ FarTopRight ],
  243. mPoints[ FarBottomLeft ] );
  244. }
  245. else
  246. {
  247. mPlanes[ PlaneLeft ].set( cameraPos,
  248. mPoints[ NearTopLeft ],
  249. mPoints[ NearBottomLeft ] );
  250. mPlanes[ PlaneRight ].set( cameraPos,
  251. mPoints[ NearBottomRight ],
  252. mPoints[ NearTopRight ] );
  253. mPlanes[ PlaneTop ].set( cameraPos,
  254. mPoints[ NearTopRight ],
  255. mPoints[ NearTopLeft ] );
  256. mPlanes[ PlaneBottom ].set( cameraPos,
  257. mPoints[ NearBottomLeft ],
  258. mPoints[ NearBottomRight ] );
  259. mPlanes[ PlaneNear ].set( mPoints[ NearTopLeft ],
  260. mPoints[ NearBottomLeft ],
  261. mPoints[ NearTopRight ] );
  262. mPlanes[ PlaneFar ].set( mPoints[ FarTopLeft ],
  263. mPoints[ FarTopRight ],
  264. mPoints[ FarBottomLeft ] );
  265. }
  266. // If the frustum plane orientation doesn't match mIsInverted
  267. // now, invert all the plane normals.
  268. //
  269. // Note that if we have a transform matrix with a negative scale,
  270. // then the initial planes we have computed will always be inverted.
  271. const bool inverted = mPlanes[ PlaneNear ].whichSide( cameraPos ) == PlaneF::Front;
  272. if( inverted != mIsInverted )
  273. {
  274. for( U32 i = 0; i < mPlanes.size(); ++ i )
  275. mPlanes[ i ].invert();
  276. }
  277. AssertFatal( mPlanes[ PlaneNear ].whichSide( cameraPos ) != PlaneF::Front,
  278. "Frustum::_update - Viewpoint lies on front side of near plane!" );
  279. // And now the center points which are mostly just used in debug rendering.
  280. mPlaneCenters[ PlaneLeftCenter ] = ( mPoints[ NearTopLeft ] +
  281. mPoints[ NearBottomLeft ] +
  282. mPoints[ FarTopLeft ] +
  283. mPoints[ FarBottomLeft ] ) / 4.0f;
  284. mPlaneCenters[ PlaneRightCenter ] = ( mPoints[ NearTopRight ] +
  285. mPoints[ NearBottomRight ] +
  286. mPoints[ FarTopRight ] +
  287. mPoints[ FarBottomRight ] ) / 4.0f;
  288. mPlaneCenters[ PlaneTopCenter ] = ( mPoints[ NearTopLeft ] +
  289. mPoints[ NearTopRight ] +
  290. mPoints[ FarTopLeft ] +
  291. mPoints[ FarTopRight ] ) / 4.0f;
  292. mPlaneCenters[ PlaneBottomCenter ] = ( mPoints[ NearBottomLeft ] +
  293. mPoints[ NearBottomRight ] +
  294. mPoints[ FarBottomLeft ] +
  295. mPoints[ FarBottomRight ] ) / 4.0f;
  296. mPlaneCenters[ PlaneNearCenter ] = ( mPoints[ NearTopLeft ] +
  297. mPoints[ NearTopRight ] +
  298. mPoints[ NearBottomLeft ] +
  299. mPoints[ NearBottomRight ] ) / 4.0f;
  300. mPlaneCenters[ PlaneFarCenter ] = ( mPoints[ FarTopLeft ] +
  301. mPoints[ FarTopRight ] +
  302. mPoints[ FarBottomLeft ] +
  303. mPoints[ FarBottomRight ] ) / 4.0f;
  304. // Done.
  305. mDirty = false;
  306. }
  307. //-----------------------------------------------------------------------------
  308. void Frustum::invert()
  309. {
  310. mIsInverted = !mIsInverted;
  311. _update();
  312. }
  313. //-----------------------------------------------------------------------------
  314. void Frustum::setTransform( const MatrixF &mat )
  315. {
  316. mTransform = mat;
  317. mPosition = mTransform.getPosition();
  318. mDirty = true;
  319. }
  320. //-----------------------------------------------------------------------------
  321. void Frustum::scaleFromCenter( F32 scale )
  322. {
  323. // Extract the fov and aspect ratio.
  324. F32 fovInRadians = mAtan2( (mNearTop - mNearBottom)*mNumTiles/2.0f, mNearDist ) * 2.0f;
  325. F32 aspectRatio = (mNearRight - mNearLeft)/(mNearTop - mNearBottom);
  326. // Now move the near and far planes out.
  327. F32 halfDist = ( mFarDist - mNearDist ) / 2.0f;
  328. mNearDist -= halfDist * ( scale - 1.0f );
  329. mFarDist += halfDist * ( scale - 1.0f );
  330. // Setup the new scaled frustum.
  331. set( mIsOrtho, fovInRadians, aspectRatio, mNearDist, mFarDist, mTransform );
  332. }
  333. //-----------------------------------------------------------------------------
  334. void Frustum::mul( const MatrixF& mat )
  335. {
  336. mTransform.mul( mat );
  337. mDirty = true;
  338. }
  339. //-----------------------------------------------------------------------------
  340. void Frustum::mulL( const MatrixF& mat )
  341. {
  342. MatrixF last( mTransform );
  343. mTransform.mul( mat, last );
  344. mDirty = true;
  345. }
  346. //-----------------------------------------------------------------------------
  347. void Frustum::getProjectionMatrix( MatrixF *proj, bool gfxRotate ) const
  348. {
  349. if (mIsOrtho)
  350. MathUtils::makeOrthoProjection(proj, mNearLeft, mNearRight, mNearTop, mNearBottom, mNearDist, mFarDist, gfxRotate);
  351. else
  352. MathUtils::makeProjection(proj, mNearLeft, mNearRight, mNearTop, mNearBottom, mNearDist, mFarDist, gfxRotate);
  353. }
  354. //-----------------------------------------------------------------------------
  355. void Frustum::tileFrustum(U32 numTiles, const Point2I& curTile, Point2F overlap)
  356. {
  357. //These will be stored to re-tile the frustum if needed
  358. mNumTiles = numTiles;
  359. mCurrTile = curTile;
  360. mTileOverlap = overlap;
  361. tile(&mNearLeft, &mNearRight, &mNearTop, &mNearBottom, mNumTiles, mCurrTile, mTileOverlap);
  362. }
  363. //-----------------------------------------------------------------------------
  364. void Frustum::tile( F32 *left, F32 *right, F32 *top, F32 *bottom, U32 numTiles, const Point2I& curTile, Point2F overlap )
  365. {
  366. if (numTiles == 1)
  367. return;
  368. Point2F tileSize( ( *right - *left ) / (F32)numTiles,
  369. ( *top - *bottom ) / (F32)numTiles );
  370. F32 leftOffset = tileSize.x*overlap.x;
  371. F32 rightOffset = tileSize.x*overlap.x*2;
  372. F32 bottomOffset = tileSize.y*overlap.y;
  373. F32 topOffset = tileSize.y*overlap.y*2;
  374. *left += tileSize.x * curTile.x - leftOffset;
  375. *right = *left + tileSize.x + rightOffset;
  376. *bottom += tileSize.y * curTile.y - bottomOffset;
  377. *top = *bottom + tileSize.y + topOffset;
  378. }