prtfile.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. /*
  2. ===========================================================================
  3. Copyright (C) 1999-2005 Id Software, Inc.
  4. This file is part of Quake III Arena source code.
  5. Quake III Arena 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 III Arena 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 Foobar; 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. int num_solidfaces;
  30. void WriteFloat (FILE *f, vec_t v)
  31. {
  32. if ( fabs(v - Q_rint(v)) < 0.001 )
  33. fprintf (f,"%i ",(int)Q_rint(v));
  34. else
  35. fprintf (f,"%f ",v);
  36. }
  37. /*
  38. =================
  39. WritePortalFile_r
  40. =================
  41. */
  42. void WritePortalFile_r (node_t *node)
  43. {
  44. int i, s;
  45. portal_t *p;
  46. winding_t *w;
  47. vec3_t normal;
  48. vec_t dist;
  49. // decision node
  50. if (node->planenum != PLANENUM_LEAF) {
  51. WritePortalFile_r (node->children[0]);
  52. WritePortalFile_r (node->children[1]);
  53. return;
  54. }
  55. if (node->opaque) {
  56. return;
  57. }
  58. for (p = node->portals ; p ; p=p->next[s])
  59. {
  60. w = p->winding;
  61. s = (p->nodes[1] == node);
  62. if (w && p->nodes[0] == node)
  63. {
  64. if (!Portal_Passable(p))
  65. continue;
  66. // write out to the file
  67. // sometimes planes get turned around when they are very near
  68. // the changeover point between different axis. interpret the
  69. // plane the same way vis will, and flip the side orders if needed
  70. // FIXME: is this still relevent?
  71. WindingPlane (w, normal, &dist);
  72. if ( DotProduct (p->plane.normal, normal) < 0.99 )
  73. { // backwards...
  74. fprintf (pf,"%i %i %i ",w->numpoints, p->nodes[1]->cluster, p->nodes[0]->cluster);
  75. }
  76. else
  77. fprintf (pf,"%i %i %i ",w->numpoints, p->nodes[0]->cluster, p->nodes[1]->cluster);
  78. if (p->hint)
  79. fprintf (pf, "1 ");
  80. else
  81. fprintf (pf, "0 ");
  82. for (i=0 ; i<w->numpoints ; i++)
  83. {
  84. fprintf (pf,"(");
  85. WriteFloat (pf, w->p[i][0]);
  86. WriteFloat (pf, w->p[i][1]);
  87. WriteFloat (pf, w->p[i][2]);
  88. fprintf (pf,") ");
  89. }
  90. fprintf (pf,"\n");
  91. }
  92. }
  93. }
  94. /*
  95. =================
  96. WriteFaceFile_r
  97. =================
  98. */
  99. void WriteFaceFile_r (node_t *node)
  100. {
  101. int i, s;
  102. portal_t *p;
  103. winding_t *w;
  104. // decision node
  105. if (node->planenum != PLANENUM_LEAF) {
  106. WriteFaceFile_r (node->children[0]);
  107. WriteFaceFile_r (node->children[1]);
  108. return;
  109. }
  110. if (node->opaque) {
  111. return;
  112. }
  113. for (p = node->portals ; p ; p=p->next[s])
  114. {
  115. w = p->winding;
  116. s = (p->nodes[1] == node);
  117. if (w)
  118. {
  119. if (Portal_Passable(p))
  120. continue;
  121. // write out to the file
  122. if (p->nodes[0] == node)
  123. {
  124. fprintf (pf,"%i %i ",w->numpoints, p->nodes[0]->cluster);
  125. for (i=0 ; i<w->numpoints ; i++)
  126. {
  127. fprintf (pf,"(");
  128. WriteFloat (pf, w->p[i][0]);
  129. WriteFloat (pf, w->p[i][1]);
  130. WriteFloat (pf, w->p[i][2]);
  131. fprintf (pf,") ");
  132. }
  133. fprintf (pf,"\n");
  134. }
  135. else
  136. {
  137. fprintf (pf,"%i %i ",w->numpoints, p->nodes[1]->cluster);
  138. for (i = w->numpoints-1; i >= 0; i--)
  139. {
  140. fprintf (pf,"(");
  141. WriteFloat (pf, w->p[i][0]);
  142. WriteFloat (pf, w->p[i][1]);
  143. WriteFloat (pf, w->p[i][2]);
  144. fprintf (pf,") ");
  145. }
  146. fprintf (pf,"\n");
  147. }
  148. }
  149. }
  150. }
  151. /*
  152. ================
  153. NumberLeafs_r
  154. ================
  155. */
  156. void NumberLeafs_r (node_t *node)
  157. {
  158. portal_t *p;
  159. if ( node->planenum != PLANENUM_LEAF ) {
  160. // decision node
  161. node->cluster = -99;
  162. NumberLeafs_r (node->children[0]);
  163. NumberLeafs_r (node->children[1]);
  164. return;
  165. }
  166. node->area = -1;
  167. if ( node->opaque ) {
  168. // solid block, viewpoint never inside
  169. node->cluster = -1;
  170. return;
  171. }
  172. node->cluster = num_visclusters;
  173. num_visclusters++;
  174. // count the portals
  175. for (p = node->portals ; p ; )
  176. {
  177. if (p->nodes[0] == node) // only write out from first leaf
  178. {
  179. if (Portal_Passable(p))
  180. num_visportals++;
  181. else
  182. num_solidfaces++;
  183. p = p->next[0];
  184. }
  185. else
  186. {
  187. if (!Portal_Passable(p))
  188. num_solidfaces++;
  189. p = p->next[1];
  190. }
  191. }
  192. }
  193. /*
  194. ================
  195. NumberClusters
  196. ================
  197. */
  198. void NumberClusters(tree_t *tree) {
  199. num_visclusters = 0;
  200. num_visportals = 0;
  201. num_solidfaces = 0;
  202. qprintf ("--- NumberClusters ---\n");
  203. // set the cluster field in every leaf and count the total number of portals
  204. NumberLeafs_r (tree->headnode);
  205. qprintf ("%5i visclusters\n", num_visclusters);
  206. qprintf ("%5i visportals\n", num_visportals);
  207. qprintf ("%5i solidfaces\n", num_solidfaces);
  208. }
  209. /*
  210. ================
  211. WritePortalFile
  212. ================
  213. */
  214. void WritePortalFile (tree_t *tree)
  215. {
  216. char filename[1024];
  217. qprintf ("--- WritePortalFile ---\n");
  218. // write the file
  219. sprintf (filename, "%s.prt", source);
  220. _printf ("writing %s\n", filename);
  221. pf = fopen (filename, "w");
  222. if (!pf)
  223. Error ("Error opening %s", filename);
  224. fprintf (pf, "%s\n", PORTALFILE);
  225. fprintf (pf, "%i\n", num_visclusters);
  226. fprintf (pf, "%i\n", num_visportals);
  227. fprintf (pf, "%i\n", num_solidfaces);
  228. WritePortalFile_r(tree->headnode);
  229. WriteFaceFile_r(tree->headnode);
  230. fclose (pf);
  231. }