prtfile.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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. /*
  20. ==============================================================================
  21. PORTAL FILE GENERATION
  22. Save out name.prt for qvis to read
  23. ==============================================================================
  24. */
  25. #define PORTALFILE "PRT1"
  26. FILE *pf;
  27. int num_visclusters; // clusters the player can be in
  28. int num_visportals;
  29. void WriteFloat (FILE *f, vec_t v)
  30. {
  31. if ( fabs(v - Q_rint(v)) < 0.001 )
  32. fprintf (f,"%i ",(int)Q_rint(v));
  33. else
  34. fprintf (f,"%f ",v);
  35. }
  36. /*
  37. =================
  38. WritePortalFile_r
  39. =================
  40. */
  41. void WritePortalFile_r (node_t *node)
  42. {
  43. int i, s;
  44. portal_t *p;
  45. winding_t *w;
  46. vec3_t normal;
  47. vec_t dist;
  48. // decision node
  49. if (node->planenum != PLANENUM_LEAF && !node->detail_seperator)
  50. {
  51. WritePortalFile_r (node->children[0]);
  52. WritePortalFile_r (node->children[1]);
  53. return;
  54. }
  55. if (node->contents & CONTENTS_SOLID)
  56. return;
  57. for (p = node->portals ; p ; p=p->next[s])
  58. {
  59. w = p->winding;
  60. s = (p->nodes[1] == node);
  61. if (w && p->nodes[0] == node)
  62. {
  63. if (!Portal_VisFlood (p))
  64. continue;
  65. // write out to the file
  66. // sometimes planes get turned around when they are very near
  67. // the changeover point between different axis. interpret the
  68. // plane the same way vis will, and flip the side orders if needed
  69. // FIXME: is this still relevent?
  70. WindingPlane (w, normal, &dist);
  71. if ( DotProduct (p->plane.normal, normal) < 0.99 )
  72. { // backwards...
  73. fprintf (pf,"%i %i %i ",w->numpoints, p->nodes[1]->cluster, p->nodes[0]->cluster);
  74. }
  75. else
  76. fprintf (pf,"%i %i %i ",w->numpoints, p->nodes[0]->cluster, p->nodes[1]->cluster);
  77. for (i=0 ; i<w->numpoints ; i++)
  78. {
  79. fprintf (pf,"(");
  80. WriteFloat (pf, w->p[i][0]);
  81. WriteFloat (pf, w->p[i][1]);
  82. WriteFloat (pf, w->p[i][2]);
  83. fprintf (pf,") ");
  84. }
  85. fprintf (pf,"\n");
  86. }
  87. }
  88. }
  89. /*
  90. ================
  91. FillLeafNumbers_r
  92. All of the leafs under node will have the same cluster
  93. ================
  94. */
  95. void FillLeafNumbers_r (node_t *node, int num)
  96. {
  97. if (node->planenum == PLANENUM_LEAF)
  98. {
  99. if (node->contents & CONTENTS_SOLID)
  100. node->cluster = -1;
  101. else
  102. node->cluster = num;
  103. return;
  104. }
  105. node->cluster = num;
  106. FillLeafNumbers_r (node->children[0], num);
  107. FillLeafNumbers_r (node->children[1], num);
  108. }
  109. /*
  110. ================
  111. NumberLeafs_r
  112. ================
  113. */
  114. void NumberLeafs_r (node_t *node)
  115. {
  116. portal_t *p;
  117. if (node->planenum != PLANENUM_LEAF && !node->detail_seperator)
  118. { // decision node
  119. node->cluster = -99;
  120. NumberLeafs_r (node->children[0]);
  121. NumberLeafs_r (node->children[1]);
  122. return;
  123. }
  124. // either a leaf or a detail cluster
  125. if ( node->contents & CONTENTS_SOLID )
  126. { // solid block, viewpoint never inside
  127. node->cluster = -1;
  128. return;
  129. }
  130. FillLeafNumbers_r (node, num_visclusters);
  131. num_visclusters++;
  132. // count the portals
  133. for (p = node->portals ; p ; )
  134. {
  135. if (p->nodes[0] == node) // only write out from first leaf
  136. {
  137. if (Portal_VisFlood (p))
  138. num_visportals++;
  139. p = p->next[0];
  140. }
  141. else
  142. p = p->next[1];
  143. }
  144. }
  145. /*
  146. ================
  147. CreateVisPortals_r
  148. ================
  149. */
  150. void CreateVisPortals_r (node_t *node)
  151. {
  152. // stop as soon as we get to a detail_seperator, which
  153. // means that everything below is in a single cluster
  154. if (node->planenum == PLANENUM_LEAF || node->detail_seperator )
  155. return;
  156. MakeNodePortal (node);
  157. SplitNodePortals (node);
  158. CreateVisPortals_r (node->children[0]);
  159. CreateVisPortals_r (node->children[1]);
  160. }
  161. /*
  162. ================
  163. FinishVisPortals_r
  164. ================
  165. */
  166. void FinishVisPortals2_r (node_t *node)
  167. {
  168. if (node->planenum == PLANENUM_LEAF)
  169. return;
  170. MakeNodePortal (node);
  171. SplitNodePortals (node);
  172. FinishVisPortals2_r (node->children[0]);
  173. FinishVisPortals2_r (node->children[1]);
  174. }
  175. void FinishVisPortals_r (node_t *node)
  176. {
  177. if (node->planenum == PLANENUM_LEAF)
  178. return;
  179. if (node->detail_seperator)
  180. {
  181. FinishVisPortals2_r (node);
  182. return;
  183. }
  184. FinishVisPortals_r (node->children[0]);
  185. FinishVisPortals_r (node->children[1]);
  186. }
  187. int clusterleaf;
  188. void SaveClusters_r (node_t *node)
  189. {
  190. if (node->planenum == PLANENUM_LEAF)
  191. {
  192. dleafs[clusterleaf++].cluster = node->cluster;
  193. return;
  194. }
  195. SaveClusters_r (node->children[0]);
  196. SaveClusters_r (node->children[1]);
  197. }
  198. /*
  199. ================
  200. WritePortalFile
  201. ================
  202. */
  203. void WritePortalFile (tree_t *tree)
  204. {
  205. char filename[1024];
  206. node_t *headnode;
  207. qprintf ("--- WritePortalFile ---\n");
  208. headnode = tree->headnode;
  209. num_visclusters = 0;
  210. num_visportals = 0;
  211. FreeTreePortals_r (headnode);
  212. MakeHeadnodePortals (tree);
  213. CreateVisPortals_r (headnode);
  214. // set the cluster field in every leaf and count the total number of portals
  215. NumberLeafs_r (headnode);
  216. // write the file
  217. sprintf (filename, "%s.prt", source);
  218. printf ("writing %s\n", filename);
  219. pf = fopen (filename, "w");
  220. if (!pf)
  221. Error ("Error opening %s", filename);
  222. fprintf (pf, "%s\n", PORTALFILE);
  223. fprintf (pf, "%i\n", num_visclusters);
  224. fprintf (pf, "%i\n", num_visportals);
  225. qprintf ("%5i visclusters\n", num_visclusters);
  226. qprintf ("%5i visportals\n", num_visportals);
  227. WritePortalFile_r (headnode);
  228. fclose (pf);
  229. // we need to store the clusters out now because ordering
  230. // issues made us do this after writebsp...
  231. clusterleaf = 1;
  232. SaveClusters_r (headnode);
  233. }