W3DPoly.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /*
  2. ** Command & Conquer Generals Zero Hour(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. // //
  20. // (c) 2001-2003 Electronic Arts Inc. //
  21. // //
  22. ////////////////////////////////////////////////////////////////////////////////
  23. #include "W3DDevice/GameClient/W3DPoly.h"
  24. #include "Lib/BaseType.h"
  25. //-------------------------------------------------------------------------------------------------
  26. /** Delete all vertices in polygon */
  27. //-------------------------------------------------------------------------------------------------
  28. void ClipPolyClass::Reset(void)
  29. {
  30. Verts.Delete_All(false);
  31. }
  32. //-------------------------------------------------------------------------------------------------
  33. /** Add a new vertex to polygon */
  34. //-------------------------------------------------------------------------------------------------
  35. void ClipPolyClass::Add_Vertex(const Vector3 & point)
  36. {
  37. Verts.Add(point);
  38. }
  39. //-------------------------------------------------------------------------------------------------
  40. /** Clip polygon to given plane, returning new polygon in dest. */
  41. //-------------------------------------------------------------------------------------------------
  42. void ClipPolyClass::Clip(const PlaneClass & plane,ClipPolyClass & dest) const
  43. {
  44. dest.Reset();
  45. // temporary variables used in clipping
  46. Int i = 0;
  47. Int vcount = Verts.Count();
  48. Int iprev = vcount - 1;
  49. Bool cur_point_in_front;
  50. Bool prev_point_in_front;
  51. Real alpha;
  52. Vector3 int_point;
  53. if (vcount <= 2) return;
  54. // perform clipping
  55. prev_point_in_front = !plane.In_Front(Verts[iprev]); // Note, plane normal is outward so we invert this test
  56. for (Int j=0; j<vcount; j++) {
  57. cur_point_in_front = !plane.In_Front(Verts[i]); // Note, plane nomral is out so we invert this test
  58. if (prev_point_in_front) {
  59. if (cur_point_in_front) {
  60. // Previous vertex was in front of plane and this vertex is in
  61. // front of the plane so we emit this vertex.
  62. dest.Add_Vertex(Verts[i]);
  63. } else {
  64. // Previous vert was in front, this vert is behind, compute
  65. // the intersection and emit the point.
  66. plane.Compute_Intersection(Verts[iprev],Verts[i],&alpha);
  67. Vector3::Lerp(Verts[iprev],Verts[i],alpha,&int_point);
  68. dest.Add_Vertex(int_point);
  69. }
  70. } else {
  71. if (cur_point_in_front) {
  72. // segment is going from the back halfspace to the front halfspace
  73. // compute the intersection and emit it, then continue
  74. // the edge into the front halfspace and emit the end point.
  75. plane.Compute_Intersection(Verts[iprev],Verts[i],&alpha);
  76. Vector3::Lerp(Verts[iprev],Verts[i],alpha,&int_point);
  77. dest.Add_Vertex(int_point);
  78. dest.Add_Vertex(Verts[i]);
  79. }
  80. }
  81. prev_point_in_front = cur_point_in_front;
  82. iprev = i;
  83. //i = (i+1)%(Verts.Count());
  84. i++;
  85. if (i>=vcount) {
  86. i = 0;
  87. }
  88. }
  89. }