colmathfrustum.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. /*
  2. ** Command & Conquer Generals(tm)
  3. ** Copyright 2025 Electronic Arts Inc.
  4. **
  5. ** This program is free software: you can redistribute it and/or modify
  6. ** it under the terms of the GNU General Public License as published by
  7. ** the Free Software Foundation, either version 3 of the License, or
  8. ** (at your option) any later version.
  9. **
  10. ** This program 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. **
  15. ** You should have received a copy of the GNU General Public License
  16. ** along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. /***********************************************************************************************
  19. *** C O N F I D E N T I A L --- W E S T W O O D S T U D I O S ***
  20. ***********************************************************************************************
  21. * *
  22. * Project Name : WWMath *
  23. * *
  24. * $Archive:: /Commando/Code/wwmath/colmathfrustum.cpp $*
  25. * *
  26. * Author:: Greg Hjelstrom *
  27. * *
  28. * $Modtime:: 3/29/00 5:40p $*
  29. * *
  30. * $Revision:: 7 $*
  31. * *
  32. *---------------------------------------------------------------------------------------------*
  33. * Functions: *
  34. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  35. #include "colmath.h"
  36. #include "colmathinlines.h"
  37. #include "aaplane.h"
  38. #include "plane.h"
  39. #include "lineseg.h"
  40. #include "tri.h"
  41. #include "sphere.h"
  42. #include "aabox.h"
  43. #include "obbox.h"
  44. #include "frustum.h"
  45. #include "wwdebug.h"
  46. // TODO: Most of these overlap functions actually do not catch all cases of when
  47. // the primitive is outside of the frustum...
  48. // Frustum functions
  49. CollisionMath::OverlapType
  50. CollisionMath::Overlap_Test(const FrustumClass & frustum,const Vector3 & point)
  51. {
  52. int mask = 0;
  53. for (int i = 0; i < 6; i++) {
  54. int result = CollisionMath::Overlap_Test(frustum.Planes[i],point);
  55. if (result == OUTSIDE) {
  56. return OUTSIDE;
  57. }
  58. mask |= result;
  59. }
  60. if (mask == INSIDE) {
  61. return INSIDE;
  62. }
  63. return OVERLAPPED;
  64. }
  65. CollisionMath::OverlapType
  66. CollisionMath::Overlap_Test(const FrustumClass & frustum,const TriClass & tri)
  67. {
  68. int mask = 0;
  69. // TODO: doesn't catch all cases...
  70. for (int i = 0; i < 6; i++) {
  71. int result = CollisionMath::Overlap_Test(frustum.Planes[i],tri);
  72. if (result == OUTSIDE) {
  73. return OUTSIDE;
  74. }
  75. mask |= result;
  76. }
  77. if (mask == INSIDE) {
  78. return INSIDE;
  79. }
  80. return OVERLAPPED;
  81. }
  82. CollisionMath::OverlapType
  83. CollisionMath::Overlap_Test(const FrustumClass & frustum,const SphereClass & sphere)
  84. {
  85. int mask = 0;
  86. // TODO: doesn't catch all cases...
  87. for (int i = 0; i < 6; i++) {
  88. int result = CollisionMath::Overlap_Test(frustum.Planes[i],sphere);
  89. if (result == OUTSIDE) {
  90. return OUTSIDE;
  91. }
  92. mask |= result;
  93. }
  94. if (mask == INSIDE) {
  95. return INSIDE;
  96. }
  97. return OVERLAPPED;
  98. }
  99. CollisionMath::OverlapType
  100. CollisionMath::Overlap_Test(const FrustumClass & frustum,const AABoxClass & box)
  101. {
  102. int mask = 0;
  103. // TODO: doesn't catch all cases...
  104. for (int i = 0; i < 6; i++) {
  105. int result = CollisionMath::Overlap_Test(frustum.Planes[i],box);
  106. if (result == OUTSIDE) {
  107. return OUTSIDE;
  108. }
  109. mask |= result;
  110. }
  111. if (mask == INSIDE) {
  112. return INSIDE;
  113. }
  114. return OVERLAPPED;
  115. }
  116. CollisionMath::OverlapType
  117. CollisionMath::Overlap_Test(const FrustumClass & frustum,const OBBoxClass & box)
  118. {
  119. int mask = 0;
  120. // TODO: doesn't catch all cases...
  121. for (int i = 0; i < 6; i++) {
  122. int result = CollisionMath::Overlap_Test(frustum.Planes[i],box);
  123. if (result == OUTSIDE) {
  124. return OUTSIDE;
  125. }
  126. mask |= result;
  127. }
  128. if (mask == INSIDE) {
  129. return INSIDE;
  130. }
  131. return OVERLAPPED;
  132. }
  133. CollisionMath::OverlapType
  134. CollisionMath::Overlap_Test(const FrustumClass & frustum,const OBBoxClass & box,int & planes_passed)
  135. {
  136. int mask = 0;
  137. // TODO: doesn't catch all cases...
  138. for (int i = 0; i < 6; i++) {
  139. int plane_bit = (1<<i);
  140. // only check this plane if we have to
  141. if ((planes_passed & plane_bit) == 0) {
  142. int result = CollisionMath::Overlap_Test(frustum.Planes[i],box);
  143. if (result == OUTSIDE) {
  144. return OUTSIDE;
  145. } else if (result == INSIDE) {
  146. planes_passed |= plane_bit;
  147. }
  148. mask |= result;
  149. } else {
  150. mask |= INSIDE;
  151. }
  152. }
  153. if (mask == INSIDE) {
  154. return INSIDE;
  155. }
  156. return OVERLAPPED;
  157. }