tree.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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 "qbsp.h"
  19. extern int c_nodes;
  20. void RemovePortalFromNode (portal_t *portal, node_t *l);
  21. node_t *NodeForPoint (node_t *node, vec3_t origin)
  22. {
  23. plane_t *plane;
  24. vec_t d;
  25. while (node->planenum != PLANENUM_LEAF)
  26. {
  27. plane = &mapplanes[node->planenum];
  28. d = DotProduct (origin, plane->normal) - plane->dist;
  29. if (d >= 0)
  30. node = node->children[0];
  31. else
  32. node = node->children[1];
  33. }
  34. return node;
  35. }
  36. /*
  37. =============
  38. FreeTreePortals_r
  39. =============
  40. */
  41. void FreeTreePortals_r (node_t *node)
  42. {
  43. portal_t *p, *nextp;
  44. int s;
  45. // free children
  46. if (node->planenum != PLANENUM_LEAF)
  47. {
  48. FreeTreePortals_r (node->children[0]);
  49. FreeTreePortals_r (node->children[1]);
  50. }
  51. // free portals
  52. for (p=node->portals ; p ; p=nextp)
  53. {
  54. s = (p->nodes[1] == node);
  55. nextp = p->next[s];
  56. RemovePortalFromNode (p, p->nodes[!s]);
  57. FreePortal (p);
  58. }
  59. node->portals = NULL;
  60. }
  61. /*
  62. =============
  63. FreeTree_r
  64. =============
  65. */
  66. void FreeTree_r (node_t *node)
  67. {
  68. face_t *f, *nextf;
  69. // free children
  70. if (node->planenum != PLANENUM_LEAF)
  71. {
  72. FreeTree_r (node->children[0]);
  73. FreeTree_r (node->children[1]);
  74. }
  75. // free bspbrushes
  76. FreeBrushList (node->brushlist);
  77. // free faces
  78. for (f=node->faces ; f ; f=nextf)
  79. {
  80. nextf = f->next;
  81. FreeFace (f);
  82. }
  83. // free the node
  84. if (node->volume)
  85. FreeBrush (node->volume);
  86. if (numthreads == 1)
  87. c_nodes--;
  88. free (node);
  89. }
  90. /*
  91. =============
  92. FreeTree
  93. =============
  94. */
  95. void FreeTree (tree_t *tree)
  96. {
  97. FreeTreePortals_r (tree->headnode);
  98. FreeTree_r (tree->headnode);
  99. free (tree);
  100. }
  101. //===============================================================
  102. void PrintTree_r (node_t *node, int depth)
  103. {
  104. int i;
  105. plane_t *plane;
  106. bspbrush_t *bb;
  107. for (i=0 ; i<depth ; i++)
  108. printf (" ");
  109. if (node->planenum == PLANENUM_LEAF)
  110. {
  111. if (!node->brushlist)
  112. printf ("NULL\n");
  113. else
  114. {
  115. for (bb=node->brushlist ; bb ; bb=bb->next)
  116. printf ("%i ", bb->original->brushnum);
  117. printf ("\n");
  118. }
  119. return;
  120. }
  121. plane = &mapplanes[node->planenum];
  122. printf ("#%i (%5.2f %5.2f %5.2f):%5.2f\n", node->planenum,
  123. plane->normal[0], plane->normal[1], plane->normal[2],
  124. plane->dist);
  125. PrintTree_r (node->children[0], depth+1);
  126. PrintTree_r (node->children[1], depth+1);
  127. }
  128. /*
  129. =========================================================
  130. NODES THAT DON'T SEPERATE DIFFERENT CONTENTS CAN BE PRUNED
  131. =========================================================
  132. */
  133. int c_pruned;
  134. /*
  135. ============
  136. PruneNodes_r
  137. ============
  138. */
  139. void PruneNodes_r (node_t *node)
  140. {
  141. bspbrush_t *b, *next;
  142. if (node->planenum == PLANENUM_LEAF)
  143. return;
  144. PruneNodes_r (node->children[0]);
  145. PruneNodes_r (node->children[1]);
  146. if ( (node->children[0]->contents & CONTENTS_SOLID)
  147. && (node->children[1]->contents & CONTENTS_SOLID) )
  148. {
  149. if (node->faces)
  150. Error ("node->faces seperating CONTENTS_SOLID");
  151. if (node->children[0]->faces || node->children[1]->faces)
  152. Error ("!node->faces with children");
  153. // FIXME: free stuff
  154. node->planenum = PLANENUM_LEAF;
  155. node->contents = CONTENTS_SOLID;
  156. node->detail_seperator = false;
  157. if (node->brushlist)
  158. Error ("PruneNodes: node->brushlist");
  159. // combine brush lists
  160. node->brushlist = node->children[1]->brushlist;
  161. for (b=node->children[0]->brushlist ; b ; b=next)
  162. {
  163. next = b->next;
  164. b->next = node->brushlist;
  165. node->brushlist = b;
  166. }
  167. c_pruned++;
  168. }
  169. }
  170. void PruneNodes (node_t *node)
  171. {
  172. qprintf ("--- PruneNodes ---\n");
  173. c_pruned = 0;
  174. PruneNodes_r (node);
  175. qprintf ("%5i pruned nodes\n", c_pruned);
  176. }
  177. //===========================================================