frustum.cpp 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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/frustum.cpp $*
  25. * *
  26. * Author:: Greg Hjelstrom *
  27. * *
  28. * $Modtime:: 4/01/01 12:47p $*
  29. * *
  30. * $Revision:: 5 $*
  31. * *
  32. *---------------------------------------------------------------------------------------------*
  33. * Functions: *
  34. * FrustumClass::Init -- Initialize a frustum object *
  35. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  36. #include "frustum.h"
  37. /***********************************************************************************************
  38. * FrustumClass::Init -- Initialize a frustum object *
  39. * *
  40. * This function initializes a frustum from the description of a camera *
  41. * *
  42. * INPUT: *
  43. * camera - camera transform, note that the camera looks down the -Z axis *
  44. * vpmin - min corner of the z=-1.0 view plane (not necessarily the near clip plane) *
  45. * vpmax - max corner of the z=-1.0 view plane (not necessarily the near clip plane) *
  46. * znear - near clip plane (should be negative, negated if it is not) *
  47. * zfar - far clip plane (should be negative, negated if it is not) *
  48. * *
  49. * OUTPUT: *
  50. * *
  51. * WARNINGS: *
  52. * The vpmin and vpmax variables are the min and max of a view-plane at z=-1.0 *
  53. * *
  54. * *
  55. * HISTORY: *
  56. * 2/17/2000 gth : Created. *
  57. *=============================================================================================*/
  58. void FrustumClass::Init
  59. (
  60. const Matrix3D & camera,
  61. const Vector2 & vpmin,
  62. const Vector2 & vpmax,
  63. float znear,
  64. float zfar
  65. )
  66. {
  67. int i;
  68. // Store the camera transform
  69. CameraTransform = camera;
  70. // Forward is negative Z in our viewspace coordinate system.
  71. // Just flip the sign if the user passed in positive values.
  72. if ((znear > 0.0f) && (zfar > 0.0f)) {
  73. znear = -znear;
  74. zfar = -zfar;
  75. }
  76. // Calculate the corners of the camera frustum.
  77. // Generate the camera-space frustum corners by linearly
  78. // extrapolating the viewplane to the near and far z clipping planes.
  79. // The camera frustum corners are defined in the following order:
  80. // When looking at the frustum from the position of the camera, the near four corners are
  81. // numbered: upper left 0, upper right 1, lower left 2, lower right 3. The far plane's
  82. // Frustum corners are numbered from 4 to 7 in an analogous fashion.
  83. // (remember: the camera space has x going to the right, y up and z backwards).
  84. //calculate a proper z-vector assuming our right-handed coordinate system
  85. Vector3 zv;
  86. Vector3::Cross_Product(CameraTransform.Get_X_Vector(),CameraTransform.Get_Y_Vector(),&zv);
  87. //compare correct z-vector with the one in the camera matrix. If they point in
  88. //opposite directions, we have a reflected camera matrix.
  89. if (Vector3::Dot_Product(CameraTransform.Get_Z_Vector(),zv) < 0)
  90. { //flip the frustum corners horizontally for a reflected matrix
  91. Corners[1].Set(vpmin.X, vpmax.Y, 1.0);
  92. Corners[5] = Corners[1];
  93. Corners[1] *= znear;
  94. Corners[5] *= zfar;
  95. Corners[0].Set(vpmax.X, vpmax.Y, 1.0);
  96. Corners[4] = Corners[0];
  97. Corners[0] *= znear;
  98. Corners[4] *= zfar;
  99. Corners[3].Set(vpmin.X, vpmin.Y, 1.0);
  100. Corners[7] = Corners[3];
  101. Corners[3] *= znear;
  102. Corners[7] *= zfar;
  103. Corners[2].Set(vpmax.X, vpmin.Y, 1.0);
  104. Corners[6] = Corners[2];
  105. Corners[2] *= znear;
  106. Corners[6] *= zfar;
  107. }
  108. else
  109. { //normal camera
  110. Corners[0].Set(vpmin.X, vpmax.Y, 1.0);
  111. Corners[4] = Corners[0];
  112. Corners[0] *= znear;
  113. Corners[4] *= zfar;
  114. Corners[1].Set(vpmax.X, vpmax.Y, 1.0);
  115. Corners[5] = Corners[1];
  116. Corners[1] *= znear;
  117. Corners[5] *= zfar;
  118. Corners[2].Set(vpmin.X, vpmin.Y, 1.0);
  119. Corners[6] = Corners[2];
  120. Corners[2] *= znear;
  121. Corners[6] *= zfar;
  122. Corners[3].Set(vpmax.X, vpmin.Y, 1.0);
  123. Corners[7] = Corners[3];
  124. Corners[3] *= znear;
  125. Corners[7] *= zfar;
  126. }
  127. // Transform the eight corners of the view frustum from camera space to world space.
  128. for (i = 0; i < 8; i++) {
  129. Matrix3D::Transform_Vector(CameraTransform, Corners[i], &(Corners[i]));
  130. }
  131. // Create the six frustum bounding planes from the eight corner Corners.
  132. // The bounding planes are oriented so that their normals point outward
  133. PlaneClass frustum_planes[6];
  134. Planes[0].Set(Corners[0], Corners[3], Corners[1]); // near
  135. Planes[1].Set(Corners[0], Corners[5], Corners[4]); // bottom
  136. Planes[2].Set(Corners[0], Corners[6], Corners[2]); // right
  137. Planes[3].Set(Corners[2], Corners[7], Corners[3]); // top
  138. Planes[4].Set(Corners[1], Corners[7], Corners[5]); // left
  139. Planes[5].Set(Corners[4], Corners[7], Corners[6]); // far
  140. // find the bounding box of the entire frustum (may be used for sloppy quick rejection)
  141. BoundMin = BoundMax = Corners[0];
  142. for (i=1; i<8;i++) {
  143. if (Corners[i].X < BoundMin.X) BoundMin.X = Corners[i].X;
  144. if (Corners[i].X > BoundMax.X) BoundMax.X = Corners[i].X;
  145. if (Corners[i].Y < BoundMin.Y) BoundMin.Y = Corners[i].Y;
  146. if (Corners[i].Y > BoundMax.Y) BoundMax.Y = Corners[i].Y;
  147. if (Corners[i].Z < BoundMin.Z) BoundMin.Z = Corners[i].Z;
  148. if (Corners[i].Z > BoundMax.Z) BoundMax.Z = Corners[i].Z;
  149. }
  150. }