csg.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /*
  2. ===========================================================================
  3. Copyright (C) 1997-2006 Id Software, Inc.
  4. This file is part of Quake 2 Tools source code.
  5. Quake 2 Tools source code is free software; you can redistribute it
  6. and/or modify it under the terms of the GNU General Public License as
  7. published by the Free Software Foundation; either version 2 of the License,
  8. or (at your option) any later version.
  9. Quake 2 Tools source code is distributed in the hope that it will be
  10. useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with Quake 2 Tools source code; if not, write to the Free Software
  15. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  16. ===========================================================================
  17. */
  18. #include "qe3.h"
  19. /*
  20. ==============
  21. CSG_SplitBrushByFace
  22. The incoming brush is NOT freed.
  23. The incoming face is NOT left referenced.
  24. ==============
  25. */
  26. void CSG_SplitBrushByFace (brush_t *in, face_t *f, brush_t **front, brush_t **back)
  27. {
  28. brush_t *b;
  29. face_t *nf;
  30. vec3_t temp;
  31. b = Brush_Clone (in);
  32. nf = Face_Clone (f);
  33. nf->texdef = b->brush_faces->texdef;
  34. nf->next = b->brush_faces;
  35. b->brush_faces = nf;
  36. Brush_Build( b );
  37. Brush_RemoveEmptyFaces ( b );
  38. if ( !b->brush_faces )
  39. { // completely clipped away
  40. Brush_Free (b);
  41. *back = NULL;
  42. }
  43. else
  44. {
  45. Entity_LinkBrush (in->owner, b);
  46. *back = b;
  47. }
  48. b = Brush_Clone (in);
  49. nf = Face_Clone (f);
  50. // swap the plane winding
  51. VectorCopy (nf->planepts[0], temp);
  52. VectorCopy (nf->planepts[1], nf->planepts[0]);
  53. VectorCopy (temp, nf->planepts[1]);
  54. nf->texdef = b->brush_faces->texdef;
  55. nf->next = b->brush_faces;
  56. b->brush_faces = nf;
  57. Brush_Build( b );
  58. Brush_RemoveEmptyFaces ( b );
  59. if ( !b->brush_faces )
  60. { // completely clipped away
  61. Brush_Free (b);
  62. *front = NULL;
  63. }
  64. else
  65. {
  66. Entity_LinkBrush (in->owner, b);
  67. *front = b;
  68. }
  69. }
  70. /*
  71. =============
  72. CSG_MakeHollow
  73. =============
  74. */
  75. void CSG_MakeHollow (void)
  76. {
  77. brush_t *b, *front, *back, *next;
  78. face_t *f;
  79. face_t split;
  80. vec3_t move;
  81. int i;
  82. for (b = selected_brushes.next ; b != &selected_brushes ; b=next)
  83. {
  84. next = b->next;
  85. for (f = b->brush_faces ; f ; f=f->next)
  86. {
  87. split = *f;
  88. VectorScale (f->plane.normal, g_qeglobals.d_gridsize, move);
  89. for (i=0 ; i<3 ; i++)
  90. VectorSubtract (split.planepts[i], move, split.planepts[i]);
  91. CSG_SplitBrushByFace (b, &split, &front, &back);
  92. if (back)
  93. Brush_Free (back);
  94. if (front)
  95. Brush_AddToList (front, &selected_brushes);
  96. }
  97. Brush_Free (b);
  98. }
  99. Sys_UpdateWindows (W_ALL);
  100. }
  101. /*
  102. =============
  103. CSG_Subtract
  104. =============
  105. */
  106. void CSG_Subtract (void)
  107. {
  108. brush_t *b, *s, *frag, *front, *back, *next, *snext;
  109. face_t *f;
  110. int i;
  111. Sys_Printf ("Subtracting...\n");
  112. for (b = selected_brushes.next ; b != &selected_brushes ; b=next)
  113. {
  114. next = b->next;
  115. if (b->owner->eclass->fixedsize)
  116. continue; // can't use texture from a fixed entity, so don't subtract
  117. for (s=active_brushes.next ; s != &active_brushes ; s=snext)
  118. {
  119. snext = s->next;
  120. if (s->owner->eclass->fixedsize)
  121. continue;
  122. for (i=0 ; i<3 ; i++)
  123. if (b->mins[i] >= s->maxs[i] - ON_EPSILON
  124. || b->maxs[i] <= s->mins[i] + ON_EPSILON)
  125. break;
  126. if (i != 3)
  127. continue; // definately don't touch
  128. frag = s;
  129. for (f = b->brush_faces ; f && frag ; f=f->next)
  130. {
  131. CSG_SplitBrushByFace (frag, f, &front, &back);
  132. Brush_Free (frag);
  133. frag = back;
  134. if (front)
  135. Brush_AddToList (front, &active_brushes);
  136. }
  137. if (frag)
  138. Brush_Free (frag);
  139. }
  140. }
  141. Sys_Printf ("done.\n");
  142. Sys_UpdateWindows (W_ALL);
  143. }