writebsp.cpp 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. #include "bsp5.h"
  2. // WriteClipNodes_r
  3. // WriteClipNodes
  4. // WriteDrawLeaf
  5. // WriteFace
  6. // WriteDrawNodes_r
  7. // FreeDrawNodes_r
  8. // WriteDrawNodes
  9. // BeginBSPFile
  10. // FinishBSPFile
  11. #include <map>
  12. typedef std::map<int,int> PlaneMap;
  13. static PlaneMap gPlaneMap;
  14. static int gNumMappedPlanes;
  15. static dplane_t gMappedPlanes[MAX_MAP_PLANES];
  16. extern bool g_noopt;
  17. // =====================================================================================
  18. // WritePlane
  19. // hook for plane optimization
  20. // =====================================================================================
  21. static int WritePlane(int planenum)
  22. {
  23. planenum = planenum & (~1);
  24. if(g_noopt)
  25. {
  26. return planenum;
  27. }
  28. PlaneMap::iterator item = gPlaneMap.find(planenum);
  29. if(item != gPlaneMap.end())
  30. {
  31. return item->second;
  32. }
  33. //add plane to BSP
  34. hlassume(gNumMappedPlanes < MAX_MAP_PLANES, assume_MAX_MAP_PLANES);
  35. gMappedPlanes[gNumMappedPlanes] = g_dplanes[planenum];
  36. gPlaneMap.insert(PlaneMap::value_type(planenum,gNumMappedPlanes));
  37. return gNumMappedPlanes++;
  38. }
  39. // =====================================================================================
  40. // WriteClipNodes_r
  41. // =====================================================================================
  42. static int WriteClipNodes_r(node_t* node)
  43. {
  44. int i, c;
  45. dclipnode_t* cn;
  46. int num;
  47. if (node->planenum == -1)
  48. {
  49. num = node->contents;
  50. free(node->markfaces);
  51. free(node);
  52. return num;
  53. }
  54. hlassume(g_numclipnodes < MAX_MAP_CLIPNODES, assume_MAX_MAP_CLIPNODES);
  55. // emit a clipnode
  56. c = g_numclipnodes;
  57. cn = &g_dclipnodes[g_numclipnodes];
  58. g_numclipnodes++;
  59. if (node->planenum & 1)
  60. {
  61. Error("WriteClipNodes_r: odd planenum");
  62. }
  63. #ifdef ZHLT_PLANETYPE_FIX //debug
  64. {
  65. dplane_t *plane = &g_dplanes[node->planenum];
  66. if (plane->normal[(plane->type)%3] < 0)
  67. if (plane->type < 3)
  68. Warning ("WriteClipNodes_r: negative plane");
  69. else
  70. Developer (DEVELOPER_LEVEL_MESSAGE, "Warning: WriteClipNodes_r: negative plane\n");
  71. }
  72. #endif
  73. cn->planenum = WritePlane(node->planenum);
  74. for (i = 0; i < 2; i++)
  75. {
  76. cn->children[i] = WriteClipNodes_r(node->children[i]);
  77. }
  78. free(node);
  79. return c;
  80. }
  81. // =====================================================================================
  82. // WriteClipNodes
  83. // Called after the clipping hull is completed. Generates a disk format
  84. // representation and frees the original memory.
  85. // =====================================================================================
  86. void WriteClipNodes(node_t* nodes)
  87. {
  88. WriteClipNodes_r(nodes);
  89. }
  90. // =====================================================================================
  91. // WriteDrawLeaf
  92. // =====================================================================================
  93. static void WriteDrawLeaf(const node_t* const node)
  94. {
  95. face_t** fp;
  96. face_t* f;
  97. dleaf_t* leaf_p;
  98. // emit a leaf
  99. leaf_p = &g_dleafs[g_numleafs];
  100. g_numleafs++;
  101. leaf_p->contents = node->contents;
  102. //
  103. // write bounding box info
  104. //
  105. VectorCopy(node->mins, leaf_p->mins);
  106. VectorCopy(node->maxs, leaf_p->maxs);
  107. leaf_p->visofs = -1; // no vis info yet
  108. //
  109. // write the marksurfaces
  110. //
  111. leaf_p->firstmarksurface = g_nummarksurfaces;
  112. hlassume(node->markfaces != NULL, assume_EmptySolid);
  113. for (fp = node->markfaces; *fp; fp++)
  114. {
  115. // emit a marksurface
  116. f = *fp;
  117. do
  118. {
  119. g_dmarksurfaces[g_nummarksurfaces] = f->outputnumber;
  120. hlassume(g_nummarksurfaces < MAX_MAP_MARKSURFACES, assume_MAX_MAP_MARKSURFACES);
  121. g_nummarksurfaces++;
  122. f = f->original; // grab tjunction split faces
  123. }
  124. while (f);
  125. }
  126. free(node->markfaces);
  127. leaf_p->nummarksurfaces = g_nummarksurfaces - leaf_p->firstmarksurface;
  128. }
  129. // =====================================================================================
  130. // WriteFace
  131. // =====================================================================================
  132. static void WriteFace(face_t* f)
  133. {
  134. dface_t* df;
  135. int i;
  136. int e;
  137. if ( CheckFaceForHint(f)
  138. || CheckFaceForSkip(f)
  139. #ifdef ZHLT_NULLTEX
  140. || CheckFaceForNull(f) // AJM
  141. #endif
  142. // =====================================================================================
  143. //Cpt_Andrew - Env_Sky Check
  144. // =====================================================================================
  145. || CheckFaceForEnv_Sky(f)
  146. // =====================================================================================
  147. )
  148. {
  149. return;
  150. }
  151. f->outputnumber = g_numfaces;
  152. df = &g_dfaces[g_numfaces];
  153. hlassume(g_numfaces < MAX_MAP_FACES, assume_MAX_MAP_FACES);
  154. g_numfaces++;
  155. df->planenum = WritePlane(f->planenum);
  156. df->side = f->planenum & 1;
  157. df->firstedge = g_numsurfedges;
  158. df->numedges = f->numpoints;
  159. df->texinfo = f->texturenum;
  160. for (i = 0; i < f->numpoints; i++)
  161. {
  162. e = GetEdge(f->pts[i], f->pts[(i + 1) % f->numpoints], f);
  163. hlassume(g_numsurfedges < MAX_MAP_SURFEDGES, assume_MAX_MAP_SURFEDGES);
  164. g_dsurfedges[g_numsurfedges] = e;
  165. g_numsurfedges++;
  166. }
  167. }
  168. // =====================================================================================
  169. // WriteDrawNodes_r
  170. // =====================================================================================
  171. static void WriteDrawNodes_r(const node_t* const node)
  172. {
  173. dnode_t* n;
  174. int i;
  175. face_t* f;
  176. // emit a node
  177. hlassume(g_numnodes < MAX_MAP_NODES, assume_MAX_MAP_NODES);
  178. n = &g_dnodes[g_numnodes];
  179. g_numnodes++;
  180. VectorCopy(node->mins, n->mins);
  181. VectorCopy(node->maxs, n->maxs);
  182. if (node->planenum & 1)
  183. {
  184. Error("WriteDrawNodes_r: odd planenum");
  185. }
  186. n->planenum = WritePlane(node->planenum);
  187. n->firstface = g_numfaces;
  188. for (f = node->faces; f; f = f->next)
  189. {
  190. WriteFace(f);
  191. }
  192. n->numfaces = g_numfaces - n->firstface;
  193. //
  194. // recursively output the other nodes
  195. //
  196. for (i = 0; i < 2; i++)
  197. {
  198. if (node->children[i]->planenum == -1)
  199. {
  200. if (node->children[i]->contents == CONTENTS_SOLID)
  201. {
  202. n->children[i] = -1;
  203. }
  204. else
  205. {
  206. n->children[i] = -(g_numleafs + 1);
  207. WriteDrawLeaf(node->children[i]);
  208. }
  209. }
  210. else
  211. {
  212. n->children[i] = g_numnodes;
  213. WriteDrawNodes_r(node->children[i]);
  214. }
  215. }
  216. }
  217. // =====================================================================================
  218. // FreeDrawNodes_r
  219. // =====================================================================================
  220. static void FreeDrawNodes_r(node_t* node)
  221. {
  222. int i;
  223. face_t* f;
  224. face_t* next;
  225. for (i = 0; i < 2; i++)
  226. {
  227. if (node->children[i]->planenum != -1)
  228. {
  229. FreeDrawNodes_r(node->children[i]);
  230. }
  231. }
  232. //
  233. // free the faces on the node
  234. //
  235. for (f = node->faces; f; f = next)
  236. {
  237. next = f->next;
  238. FreeFace(f);
  239. }
  240. free(node);
  241. }
  242. // =====================================================================================
  243. // WriteDrawNodes
  244. // Called after a drawing hull is completed
  245. // Frees all nodes and faces
  246. // =====================================================================================
  247. void WriteDrawNodes(node_t* headnode)
  248. {
  249. if (headnode->contents < 0)
  250. {
  251. WriteDrawLeaf(headnode);
  252. }
  253. else
  254. {
  255. WriteDrawNodes_r(headnode);
  256. FreeDrawNodes_r(headnode);
  257. }
  258. }
  259. // =====================================================================================
  260. // BeginBSPFile
  261. // =====================================================================================
  262. void BeginBSPFile()
  263. {
  264. // these values may actually be initialized
  265. // if the file existed when loaded, so clear them explicitly
  266. gNumMappedPlanes = 0;
  267. gPlaneMap.clear();
  268. g_nummodels = 0;
  269. g_numfaces = 0;
  270. g_numnodes = 0;
  271. g_numclipnodes = 0;
  272. g_numvertexes = 0;
  273. g_nummarksurfaces = 0;
  274. g_numsurfedges = 0;
  275. // edge 0 is not used, because 0 can't be negated
  276. g_numedges = 1;
  277. // leaf 0 is common solid with no faces
  278. g_numleafs = 1;
  279. g_dleafs[0].contents = CONTENTS_SOLID;
  280. }
  281. // =====================================================================================
  282. // FinishBSPFile
  283. // =====================================================================================
  284. void FinishBSPFile()
  285. {
  286. Verbose("--- FinishBSPFile ---\n");
  287. if(!g_noopt)
  288. {
  289. for(int counter = 0; counter < gNumMappedPlanes; counter++)
  290. {
  291. g_dplanes[counter] = gMappedPlanes[counter];
  292. }
  293. g_numplanes = gNumMappedPlanes;
  294. }
  295. if (g_chart)
  296. {
  297. PrintBSPFileSizes();
  298. }
  299. WriteBSPFile(g_bspfilename);
  300. }