portals.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. #ifdef SYSTEM_WIN32
  2. #pragma warning(disable:4996)
  3. #endif
  4. #pragma warning(disable:4018) // '<' : signed/unsigned mismatch
  5. #include "bsp5.h"
  6. node_t g_outside_node; // portals outside the world face this
  7. //=============================================================================
  8. /*
  9. * =============
  10. * AddPortalToNodes
  11. * =============
  12. */
  13. void AddPortalToNodes(portal_t* p, node_t* front, node_t* back)
  14. {
  15. if (p->nodes[0] || p->nodes[1])
  16. {
  17. Error("AddPortalToNode: allready included");
  18. }
  19. p->nodes[0] = front;
  20. p->next[0] = front->portals;
  21. front->portals = p;
  22. p->nodes[1] = back;
  23. p->next[1] = back->portals;
  24. back->portals = p;
  25. }
  26. /*
  27. * =============
  28. * RemovePortalFromNode
  29. * =============
  30. */
  31. void RemovePortalFromNode(portal_t* portal, node_t* l)
  32. {
  33. portal_t** pp;
  34. portal_t* t;
  35. // remove reference to the current portal
  36. pp = &l->portals;
  37. while (1)
  38. {
  39. t = *pp;
  40. if (!t)
  41. {
  42. Error("RemovePortalFromNode: portal not in leaf");
  43. }
  44. if (t == portal)
  45. {
  46. break;
  47. }
  48. if (t->nodes[0] == l)
  49. {
  50. pp = &t->next[0];
  51. }
  52. else if (t->nodes[1] == l)
  53. {
  54. pp = &t->next[1];
  55. }
  56. else
  57. {
  58. Error("RemovePortalFromNode: portal not bounding leaf");
  59. }
  60. }
  61. if (portal->nodes[0] == l)
  62. {
  63. *pp = portal->next[0];
  64. portal->nodes[0] = NULL;
  65. }
  66. else if (portal->nodes[1] == l)
  67. {
  68. *pp = portal->next[1];
  69. portal->nodes[1] = NULL;
  70. }
  71. }
  72. //============================================================================
  73. /*
  74. * ================
  75. * MakeHeadnodePortals
  76. *
  77. * The created portals will face the global g_outside_node
  78. * ================
  79. */
  80. void MakeHeadnodePortals(node_t* node, const vec3_t mins, const vec3_t maxs)
  81. {
  82. vec3_t bounds[2];
  83. int i, j, n;
  84. portal_t* p;
  85. portal_t* portals[6];
  86. dplane_t bplanes[6];
  87. dplane_t* pl;
  88. // pad with some space so there will never be null volume leafs
  89. for (i = 0; i < 3; i++)
  90. {
  91. bounds[0][i] = mins[i] - SIDESPACE;
  92. bounds[1][i] = maxs[i] + SIDESPACE;
  93. }
  94. g_outside_node.contents = CONTENTS_SOLID;
  95. g_outside_node.portals = NULL;
  96. for (i = 0; i < 3; i++)
  97. {
  98. for (j = 0; j < 2; j++)
  99. {
  100. n = j * 3 + i;
  101. p = AllocPortal();
  102. portals[n] = p;
  103. pl = &bplanes[n];
  104. memset(pl, 0, sizeof(*pl));
  105. if (j)
  106. {
  107. pl->normal[i] = -1;
  108. pl->dist = -bounds[j][i];
  109. }
  110. else
  111. {
  112. pl->normal[i] = 1;
  113. pl->dist = bounds[j][i];
  114. }
  115. p->plane = *pl;
  116. p->winding = new Winding(*pl);
  117. AddPortalToNodes(p, node, &g_outside_node);
  118. }
  119. }
  120. // clip the basewindings by all the other planes
  121. for (i = 0; i < 6; i++)
  122. {
  123. for (j = 0; j < 6; j++)
  124. {
  125. if (j == i)
  126. {
  127. continue;
  128. }
  129. portals[i]->winding->Clip(bplanes[j], true);
  130. }
  131. }
  132. }
  133. /*
  134. * ==============================================================================
  135. *
  136. * PORTAL FILE GENERATION
  137. *
  138. * ==============================================================================
  139. */
  140. static FILE* pf;
  141. static int num_visleafs; // leafs the player can be in
  142. static int num_visportals;
  143. static void WritePortalFile_r(const node_t* const node)
  144. {
  145. int i;
  146. portal_t* p;
  147. Winding* w;
  148. dplane_t plane2;
  149. if (!node->contents)
  150. {
  151. WritePortalFile_r(node->children[0]);
  152. WritePortalFile_r(node->children[1]);
  153. return;
  154. }
  155. if (node->contents == CONTENTS_SOLID)
  156. {
  157. return;
  158. }
  159. for (p = node->portals; p;)
  160. {
  161. w = p->winding;
  162. if (w && p->nodes[0] == node)
  163. {
  164. if (p->nodes[0]->contents == p->nodes[1]->contents)
  165. {
  166. // write out to the file
  167. // sometimes planes get turned around when they are very near
  168. // the changeover point between different axis. interpret the
  169. // plane the same way vis will, and flip the side orders if needed
  170. w->getPlane(plane2);
  171. if (DotProduct(p->plane.normal, plane2.normal) < 1.0 - ON_EPSILON)
  172. { // backwards...
  173. fprintf(pf, "%u %i %i ", w->m_NumPoints, p->nodes[1]->visleafnum, p->nodes[0]->visleafnum);
  174. }
  175. else
  176. {
  177. fprintf(pf, "%u %i %i ", w->m_NumPoints, p->nodes[0]->visleafnum, p->nodes[1]->visleafnum);
  178. }
  179. for (i = 0; i < w->m_NumPoints; i++)
  180. {
  181. fprintf(pf, "(%f %f %f) ", w->m_Points[i][0], w->m_Points[i][1], w->m_Points[i][2]);
  182. }
  183. fprintf(pf, "\n");
  184. }
  185. }
  186. if (p->nodes[0] == node)
  187. {
  188. p = p->next[0];
  189. }
  190. else
  191. {
  192. p = p->next[1];
  193. }
  194. }
  195. }
  196. /*
  197. * ================
  198. * NumberLeafs_r
  199. * ================
  200. */
  201. static void NumberLeafs_r(node_t* node)
  202. {
  203. portal_t* p;
  204. if (!node->contents)
  205. { // decision node
  206. node->visleafnum = -99;
  207. NumberLeafs_r(node->children[0]);
  208. NumberLeafs_r(node->children[1]);
  209. return;
  210. }
  211. if (node->contents == CONTENTS_SOLID)
  212. { // solid block, viewpoint never inside
  213. node->visleafnum = -1;
  214. return;
  215. }
  216. node->visleafnum = num_visleafs++;
  217. for (p = node->portals; p;)
  218. {
  219. if (p->nodes[0] == node) // only write out from first leaf
  220. {
  221. if (p->nodes[0]->contents == p->nodes[1]->contents)
  222. {
  223. num_visportals++;
  224. }
  225. p = p->next[0];
  226. }
  227. else
  228. {
  229. p = p->next[1];
  230. }
  231. }
  232. }
  233. /*
  234. * ================
  235. * WritePortalfile
  236. * ================
  237. */
  238. void WritePortalfile(node_t* headnode)
  239. {
  240. // set the visleafnum field in every leaf and count the total number of portals
  241. num_visleafs = 0;
  242. num_visportals = 0;
  243. NumberLeafs_r(headnode);
  244. // write the file
  245. pf = fopen(g_portfilename, "w");
  246. if (!pf)
  247. {
  248. Error("Error writing portal file %s", g_portfilename);
  249. }
  250. fprintf(pf, "%i\n", num_visleafs);
  251. fprintf(pf, "%i\n", num_visportals);
  252. WritePortalFile_r(headnode);
  253. fclose(pf);
  254. Log("BSP generation successful, writing portal file '%s'\n", g_portfilename);
  255. }
  256. //===================================================
  257. void FreePortals(node_t* node)
  258. {
  259. portal_t* p;
  260. portal_t* nextp;
  261. if (node->planenum != -1)
  262. {
  263. FreePortals(node->children[0]);
  264. FreePortals(node->children[1]);
  265. return;
  266. }
  267. for (p = node->portals; p; p = nextp)
  268. {
  269. if (p->nodes[0] == node)
  270. {
  271. nextp = p->next[0];
  272. }
  273. else
  274. {
  275. nextp = p->next[1];
  276. }
  277. RemovePortalFromNode(p, p->nodes[0]);
  278. RemovePortalFromNode(p, p->nodes[1]);
  279. delete p->winding;
  280. FreePortal(p);
  281. }
  282. }