Frustum_gcc.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /*
  2. ===========================================================================
  3. Doom 3 GPL Source Code
  4. Copyright (C) 1999-2011 id Software LLC, a ZeniMax Media company.
  5. This file is part of the Doom 3 GPL Source Code (?Doom 3 Source Code?).
  6. Doom 3 Source Code is free software: you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation, either version 3 of the License, or
  9. (at your option) any later version.
  10. Doom 3 Source Code is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with Doom 3 Source Code. If not, see <http://www.gnu.org/licenses/>.
  16. In addition, the Doom 3 Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the Doom 3 Source Code. If not, please request a copy in writing from id Software at the address below.
  17. If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA.
  18. ===========================================================================
  19. */
  20. #include "../precompiled.h"
  21. void BoxToPoints( const idVec3 &center, const idVec3 &extents, const idMat3 &axis, idVec3 points[8] );
  22. /*
  23. ============
  24. idFrustum::ProjectionBounds
  25. ============
  26. */
  27. bool idFrustum::ProjectionBounds( const idBox &box, idBounds &projectionBounds ) const {
  28. int i, p1, p2, pointCull[8], culled, outside;
  29. float scale1, scale2;
  30. idFrustum localFrustum;
  31. idVec3 points[8], localOrigin;
  32. idMat3 localAxis, localScaled;
  33. idBounds bounds( -box.GetExtents(), box.GetExtents() );
  34. // if the frustum origin is inside the bounds
  35. if ( bounds.ContainsPoint( ( origin - box.GetCenter() ) * box.GetAxis().Transpose() ) ) {
  36. // bounds that cover the whole frustum
  37. float boxMin, boxMax, base;
  38. base = origin * axis[0];
  39. box.AxisProjection( axis[0], boxMin, boxMax );
  40. projectionBounds[0].x = boxMin - base;
  41. projectionBounds[1].x = boxMax - base;
  42. projectionBounds[0].y = projectionBounds[0].z = -1.0f;
  43. projectionBounds[1].y = projectionBounds[1].z = 1.0f;
  44. return true;
  45. }
  46. projectionBounds.Clear();
  47. // transform the bounds into the space of this frustum
  48. localOrigin = ( box.GetCenter() - origin ) * axis.Transpose();
  49. localAxis = box.GetAxis() * axis.Transpose();
  50. BoxToPoints( localOrigin, box.GetExtents(), localAxis, points );
  51. // test outer four edges of the bounds
  52. culled = -1;
  53. outside = 0;
  54. for ( i = 0; i < 4; i++ ) {
  55. p1 = i;
  56. p2 = 4 + i;
  57. AddLocalLineToProjectionBoundsSetCull( points[p1], points[p2], pointCull[p1], pointCull[p2], projectionBounds );
  58. culled &= pointCull[p1] & pointCull[p2];
  59. outside |= pointCull[p1] | pointCull[p2];
  60. }
  61. // if the bounds are completely outside this frustum
  62. if ( culled ) {
  63. return false;
  64. }
  65. // if the bounds are completely inside this frustum
  66. if ( !outside ) {
  67. return true;
  68. }
  69. // test the remaining edges of the bounds
  70. for ( i = 0; i < 4; i++ ) {
  71. p1 = i;
  72. p2 = (i+1)&3;
  73. AddLocalLineToProjectionBoundsUseCull( points[p1], points[p2], pointCull[p1], pointCull[p2], projectionBounds );
  74. }
  75. for ( i = 0; i < 4; i++ ) {
  76. p1 = 4 + i;
  77. p2 = 4 + ((i+1)&3);
  78. AddLocalLineToProjectionBoundsUseCull( points[p1], points[p2], pointCull[p1], pointCull[p2], projectionBounds );
  79. }
  80. // if the bounds extend beyond two or more boundaries of this frustum
  81. if ( outside != 1 && outside != 2 && outside != 4 && outside != 8 ) {
  82. localOrigin = ( origin - box.GetCenter() ) * box.GetAxis().Transpose();
  83. localScaled = axis * box.GetAxis().Transpose();
  84. localScaled[0] *= dFar;
  85. localScaled[1] *= dLeft;
  86. localScaled[2] *= dUp;
  87. // test the outer edges of this frustum for intersection with the bounds
  88. if ( (outside & 2) && (outside & 8) ) {
  89. BoundsRayIntersection( bounds, localOrigin, localScaled[0] - localScaled[1] - localScaled[2], scale1, scale2 );
  90. if ( scale1 <= scale2 && scale1 >= 0.0f ) {
  91. projectionBounds.AddPoint( idVec3( scale1 * dFar, -1.0f, -1.0f ) );
  92. projectionBounds.AddPoint( idVec3( scale2 * dFar, -1.0f, -1.0f ) );
  93. }
  94. }
  95. if ( (outside & 2) && (outside & 4) ) {
  96. BoundsRayIntersection( bounds, localOrigin, localScaled[0] - localScaled[1] + localScaled[2], scale1, scale2 );
  97. if ( scale1 <= scale2 && scale1 >= 0.0f ) {
  98. projectionBounds.AddPoint( idVec3( scale1 * dFar, -1.0f, 1.0f ) );
  99. projectionBounds.AddPoint( idVec3( scale2 * dFar, -1.0f, 1.0f ) );
  100. }
  101. }
  102. if ( (outside & 1) && (outside & 8) ) {
  103. BoundsRayIntersection( bounds, localOrigin, localScaled[0] + localScaled[1] - localScaled[2], scale1, scale2 );
  104. if ( scale1 <= scale2 && scale1 >= 0.0f ) {
  105. projectionBounds.AddPoint( idVec3( scale1 * dFar, 1.0f, -1.0f ) );
  106. projectionBounds.AddPoint( idVec3( scale2 * dFar, 1.0f, -1.0f ) );
  107. }
  108. }
  109. if ( (outside & 1) && (outside & 2) ) {
  110. BoundsRayIntersection( bounds, localOrigin, localScaled[0] + localScaled[1] + localScaled[2], scale1, scale2 );
  111. if ( scale1 <= scale2 && scale1 >= 0.0f ) {
  112. projectionBounds.AddPoint( idVec3( scale1 * dFar, 1.0f, 1.0f ) );
  113. projectionBounds.AddPoint( idVec3( scale2 * dFar, 1.0f, 1.0f ) );
  114. }
  115. }
  116. }
  117. return true;
  118. }