solidbsp.cpp 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164
  1. #pragma warning(disable:4018) // '<' : signed/unsigned mismatch
  2. #include "bsp5.h"
  3. // FaceSide
  4. // ChooseMidPlaneFromList
  5. // ChoosePlaneFromList
  6. // SelectPartition
  7. // CalcSurfaceInfo
  8. // DivideSurface
  9. // SplitNodeSurfaces
  10. // RankForContents
  11. // ContentsForRank
  12. // FreeLeafSurfs
  13. // LinkLeafFaces
  14. // MakeNodePortal
  15. // SplitNodePortals
  16. // CalcNodeBounds
  17. // CopyFacesToNode
  18. // BuildBspTree_r
  19. // SolidBSP
  20. // Each node or leaf will have a set of portals that completely enclose
  21. // the volume of the node and pass into an adjacent node.
  22. int g_maxnode_size = DEFAULT_MAXNODE_SIZE;
  23. static bool g_reportProgress = false;
  24. static int g_numProcessed = 0;
  25. static int g_numReported = 0;
  26. static void ResetStatus(bool report_progress)
  27. {
  28. g_reportProgress = report_progress;
  29. g_numProcessed = g_numReported = 0;
  30. }
  31. static void UpdateStatus(void)
  32. {
  33. if(g_reportProgress)
  34. {
  35. ++g_numProcessed;
  36. if((g_numProcessed / 500) > g_numReported)
  37. {
  38. g_numReported = (g_numProcessed / 500);
  39. Log("%d...",g_numProcessed);
  40. }
  41. }
  42. }
  43. // =====================================================================================
  44. // FaceSide
  45. // For BSP hueristic
  46. // =====================================================================================
  47. static int FaceSide(face_t* in, const dplane_t* const split)
  48. {
  49. int frontcount, backcount;
  50. vec_t dot;
  51. int i;
  52. vec_t* p;
  53. frontcount = backcount = 0;
  54. // axial planes are fast
  55. if (split->type <= last_axial)
  56. {
  57. vec_t splitGtEp = split->dist + ON_EPSILON; // Invariant moved out of loop
  58. vec_t splitLtEp = split->dist - ON_EPSILON; // Invariant moved out of loop
  59. for (i = 0, p = in->pts[0] + split->type; i < in->numpoints; i++, p += 3)
  60. {
  61. if (*p > splitGtEp)
  62. {
  63. if (backcount)
  64. {
  65. return SIDE_ON;
  66. }
  67. frontcount = 1;
  68. }
  69. else if (*p < splitLtEp)
  70. {
  71. if (frontcount)
  72. {
  73. return SIDE_ON;
  74. }
  75. backcount = 1;
  76. }
  77. }
  78. }
  79. else
  80. {
  81. // sloping planes take longer
  82. for (i = 0, p = in->pts[0]; i < in->numpoints; i++, p += 3)
  83. {
  84. dot = DotProduct(p, split->normal);
  85. dot -= split->dist;
  86. if (dot > ON_EPSILON)
  87. {
  88. if (backcount)
  89. {
  90. return SIDE_ON;
  91. }
  92. frontcount = 1;
  93. }
  94. else if (dot < -ON_EPSILON)
  95. {
  96. if (frontcount)
  97. {
  98. return SIDE_ON;
  99. }
  100. backcount = 1;
  101. }
  102. }
  103. }
  104. if (!frontcount)
  105. {
  106. return SIDE_BACK;
  107. }
  108. if (!backcount)
  109. {
  110. return SIDE_FRONT;
  111. }
  112. return SIDE_ON;
  113. }
  114. // =====================================================================================
  115. // ChooseMidPlaneFromList
  116. // When there are a huge number of planes, just choose one closest
  117. // to the middle.
  118. // =====================================================================================
  119. static surface_t* ChooseMidPlaneFromList(surface_t* surfaces, const vec3_t mins, const vec3_t maxs)
  120. {
  121. int j, l;
  122. surface_t* p;
  123. surface_t* bestsurface;
  124. vec_t bestvalue;
  125. vec_t value;
  126. vec_t dist;
  127. dplane_t* plane;
  128. //
  129. // pick the plane that splits the least
  130. //
  131. bestvalue = 6 * 8192 * 8192;
  132. bestsurface = NULL;
  133. for (p = surfaces; p; p = p->next)
  134. {
  135. if (p->onnode)
  136. {
  137. continue;
  138. }
  139. plane = &g_dplanes[p->planenum];
  140. // check for axis aligned surfaces
  141. l = plane->type;
  142. if (l > last_axial)
  143. {
  144. continue;
  145. }
  146. //
  147. // calculate the split metric along axis l, smaller values are better
  148. //
  149. value = 0;
  150. dist = plane->dist * plane->normal[l];
  151. for (j = 0; j < 3; j++)
  152. {
  153. if (j == l)
  154. {
  155. value += (maxs[l] - dist) * (maxs[l] - dist);
  156. value += (dist - mins[l]) * (dist - mins[l]);
  157. }
  158. else
  159. {
  160. value += 2 * (maxs[j] - mins[j]) * (maxs[j] - mins[j]);
  161. }
  162. }
  163. if (value > bestvalue)
  164. {
  165. continue;
  166. }
  167. //
  168. // currently the best!
  169. //
  170. bestvalue = value;
  171. bestsurface = p;
  172. }
  173. if (!bestsurface)
  174. {
  175. for (p = surfaces; p; p = p->next)
  176. {
  177. if (!p->onnode)
  178. {
  179. return p; // first valid surface
  180. }
  181. }
  182. Error("ChooseMidPlaneFromList: no valid planes");
  183. }
  184. return bestsurface;
  185. }
  186. // =====================================================================================
  187. // ChoosePlaneFromList
  188. // Choose the plane that splits the least faces
  189. // =====================================================================================
  190. static surface_t* ChoosePlaneFromList(surface_t* surfaces, const vec3_t mins, const vec3_t maxs)
  191. {
  192. int j;
  193. int k;
  194. int l;
  195. surface_t* p;
  196. surface_t* p2;
  197. surface_t* bestsurface;
  198. vec_t bestvalue;
  199. vec_t bestdistribution;
  200. vec_t value;
  201. vec_t dist;
  202. dplane_t* plane;
  203. face_t* f;
  204. //
  205. // pick the plane that splits the least
  206. //
  207. #define UNDESIREABLE_HINT_FACTOR 10000
  208. #define WORST_VALUE 100000000
  209. bestvalue = WORST_VALUE;
  210. bestsurface = NULL;
  211. bestdistribution = 9e30;
  212. for (p = surfaces; p; p = p->next)
  213. {
  214. if (p->onnode)
  215. {
  216. continue;
  217. }
  218. #ifdef ZHLT_DETAIL
  219. if (g_bDetailBrushes)
  220. {
  221. // AJM: cycle though all faces, and make sure none of them are detail
  222. // if any of them are, this surface isnt to cause a bsp split
  223. for (face_t* f = p->faces; f; f = f->next)
  224. {
  225. if (f->contents == CONTENTS_DETAIL)
  226. {
  227. //Log("ChoosePlaneFromList::got a detial surface, skipping...\n");
  228. continue;
  229. }
  230. }
  231. }
  232. #endif
  233. plane = &g_dplanes[p->planenum];
  234. k = 0;
  235. for (p2 = surfaces; p2; p2 = p2->next)
  236. {
  237. if (p2 == p)
  238. {
  239. continue;
  240. }
  241. if (p2->onnode)
  242. {
  243. continue;
  244. }
  245. for (f = p2->faces; f; f = f->next)
  246. {
  247. // Give this face (a hint brush fragment) a large 'undesireable' value, only split when we have to)
  248. if (f->facestyle == face_hint)
  249. {
  250. k += UNDESIREABLE_HINT_FACTOR;
  251. hlassert(k < WORST_VALUE);
  252. if (k >= WORST_VALUE)
  253. {
  254. Warning("::ChoosePlaneFromList() surface fragmentation undesireability exceeded WORST_VALUE");
  255. k = WORST_VALUE - 1;
  256. }
  257. }
  258. if (FaceSide(f, plane) == SIDE_ON)
  259. {
  260. k++;
  261. if (k >= bestvalue)
  262. {
  263. break;
  264. }
  265. }
  266. }
  267. if (k > bestvalue)
  268. {
  269. break;
  270. }
  271. }
  272. if (k > bestvalue)
  273. {
  274. continue;
  275. }
  276. // if equal numbers, axial planes win, then decide on spatial subdivision
  277. if (k < bestvalue || (k == bestvalue && (plane->type <= last_axial)))
  278. {
  279. // check for axis aligned surfaces
  280. l = plane->type;
  281. if (l <= last_axial)
  282. { // axial aligned
  283. //
  284. // calculate the split metric along axis l
  285. //
  286. value = 0;
  287. for (j = 0; j < 3; j++)
  288. {
  289. if (j == l)
  290. {
  291. dist = plane->dist * plane->normal[l];
  292. value += (maxs[l] - dist) * (maxs[l] - dist);
  293. value += (dist - mins[l]) * (dist - mins[l]);
  294. }
  295. else
  296. {
  297. value += 2 * (maxs[j] - mins[j]) * (maxs[j] - mins[j]);
  298. }
  299. }
  300. if (value > bestdistribution && k == bestvalue)
  301. {
  302. continue;
  303. }
  304. bestdistribution = value;
  305. }
  306. //
  307. // currently the best!
  308. //
  309. bestvalue = k;
  310. bestsurface = p;
  311. }
  312. }
  313. return bestsurface;
  314. }
  315. // =====================================================================================
  316. // SelectPartition
  317. // Selects a surface from a linked list of surfaces to split the group on
  318. // returns NULL if the surface list can not be divided any more (a leaf)
  319. // =====================================================================================
  320. static surface_t* SelectPartition(surface_t* surfaces, const node_t* const node, const bool usemidsplit)
  321. {
  322. int i;
  323. surface_t* p;
  324. surface_t* bestsurface;
  325. //
  326. // count surface choices
  327. //
  328. i = 0;
  329. bestsurface = NULL;
  330. for (p = surfaces; p; p = p->next)
  331. {
  332. if (!p->onnode)
  333. {
  334. #ifdef ZHLT_DETAIL
  335. if (g_bDetailBrushes)
  336. {
  337. // AJM: cycle though all faces, and make sure none of them are detail
  338. // if any of them are, this surface isnt to cause a bsp split
  339. for (face_t* f = p->faces; f; f = f->next)
  340. {
  341. if (f->contents == CONTENTS_DETAIL)
  342. {
  343. //Log("SelectPartition::got a detial surface, skipping...\n");
  344. continue;
  345. }
  346. }
  347. }
  348. #endif
  349. i++;
  350. bestsurface = p;
  351. }
  352. }
  353. if (i == 0)
  354. {
  355. return NULL; // this is a leafnode
  356. }
  357. if (i == 1)
  358. {
  359. return bestsurface; // this is a final split
  360. }
  361. if (usemidsplit)
  362. {
  363. // do fast way for clipping hull
  364. return ChooseMidPlaneFromList(surfaces, node->mins, node->maxs);
  365. }
  366. else
  367. {
  368. // do slow way to save poly splits for drawing hull
  369. return ChoosePlaneFromList(surfaces, node->mins, node->maxs);
  370. }
  371. }
  372. // =====================================================================================
  373. // CalcSurfaceInfo
  374. // Calculates the bounding box
  375. // =====================================================================================
  376. static void CalcSurfaceInfo(surface_t* surf)
  377. {
  378. int i;
  379. int j;
  380. face_t* f;
  381. hlassume(surf->faces != NULL, assume_ValidPointer); // "CalcSurfaceInfo() surface without a face"
  382. //
  383. // calculate a bounding box
  384. //
  385. for (i = 0; i < 3; i++)
  386. {
  387. surf->mins[i] = 99999;
  388. surf->maxs[i] = -99999;
  389. }
  390. for (f = surf->faces; f; f = f->next)
  391. {
  392. if (f->contents >= 0)
  393. {
  394. Error("Bad contents");
  395. }
  396. for (i = 0; i < f->numpoints; i++)
  397. {
  398. for (j = 0; j < 3; j++)
  399. {
  400. if (f->pts[i][j] < surf->mins[j])
  401. {
  402. surf->mins[j] = f->pts[i][j];
  403. }
  404. if (f->pts[i][j] > surf->maxs[j])
  405. {
  406. surf->maxs[j] = f->pts[i][j];
  407. }
  408. }
  409. }
  410. }
  411. }
  412. // =====================================================================================
  413. // DivideSurface
  414. // =====================================================================================
  415. static void DivideSurface(surface_t* in, const dplane_t* const split, surface_t** front, surface_t** back)
  416. {
  417. face_t* facet;
  418. face_t* next;
  419. face_t* frontlist;
  420. face_t* backlist;
  421. face_t* frontfrag;
  422. face_t* backfrag;
  423. surface_t* news;
  424. dplane_t* inplane;
  425. inplane = &g_dplanes[in->planenum];
  426. // parallel case is easy
  427. if (inplane->normal[0] == split->normal[0]
  428. && inplane->normal[1] == split->normal[1]
  429. && inplane->normal[2] == split->normal[2])
  430. {
  431. if (inplane->dist > split->dist)
  432. {
  433. *front = in;
  434. *back = NULL;
  435. }
  436. else if (inplane->dist < split->dist)
  437. {
  438. *front = NULL;
  439. *back = in;
  440. }
  441. else
  442. { // split the surface into front and back
  443. frontlist = NULL;
  444. backlist = NULL;
  445. for (facet = in->faces; facet; facet = next)
  446. {
  447. next = facet->next;
  448. if (facet->planenum & 1)
  449. {
  450. facet->next = backlist;
  451. backlist = facet;
  452. }
  453. else
  454. {
  455. facet->next = frontlist;
  456. frontlist = facet;
  457. }
  458. }
  459. goto makesurfs;
  460. }
  461. return;
  462. }
  463. // do a real split. may still end up entirely on one side
  464. // OPTIMIZE: use bounding box for fast test
  465. frontlist = NULL;
  466. backlist = NULL;
  467. for (facet = in->faces; facet; facet = next)
  468. {
  469. next = facet->next;
  470. SplitFace(facet, split, &frontfrag, &backfrag);
  471. if (frontfrag)
  472. {
  473. frontfrag->next = frontlist;
  474. frontlist = frontfrag;
  475. }
  476. if (backfrag)
  477. {
  478. backfrag->next = backlist;
  479. backlist = backfrag;
  480. }
  481. }
  482. // if nothing actually got split, just move the in plane
  483. makesurfs:
  484. if (frontlist == NULL)
  485. {
  486. *front = NULL;
  487. *back = in;
  488. in->faces = backlist;
  489. return;
  490. }
  491. if (backlist == NULL)
  492. {
  493. *front = in;
  494. *back = NULL;
  495. in->faces = frontlist;
  496. return;
  497. }
  498. // stuff got split, so allocate one new surface and reuse in
  499. news = AllocSurface();
  500. *news = *in;
  501. news->faces = backlist;
  502. *back = news;
  503. in->faces = frontlist;
  504. *front = in;
  505. // recalc bboxes and flags
  506. CalcSurfaceInfo(news);
  507. CalcSurfaceInfo(in);
  508. }
  509. // =====================================================================================
  510. // SplitNodeSurfaces
  511. // =====================================================================================
  512. static void SplitNodeSurfaces(surface_t* surfaces, const node_t* const node)
  513. {
  514. surface_t* p;
  515. surface_t* next;
  516. surface_t* frontlist;
  517. surface_t* backlist;
  518. surface_t* frontfrag;
  519. surface_t* backfrag;
  520. dplane_t* splitplane;
  521. splitplane = &g_dplanes[node->planenum];
  522. frontlist = NULL;
  523. backlist = NULL;
  524. for (p = surfaces; p; p = next)
  525. {
  526. next = p->next;
  527. DivideSurface(p, splitplane, &frontfrag, &backfrag);
  528. if (frontfrag)
  529. {
  530. if (!frontfrag->faces)
  531. {
  532. Error("surface with no faces");
  533. }
  534. frontfrag->next = frontlist;
  535. frontlist = frontfrag;
  536. }
  537. if (backfrag)
  538. {
  539. if (!backfrag->faces)
  540. {
  541. Error("surface with no faces");
  542. }
  543. backfrag->next = backlist;
  544. backlist = backfrag;
  545. }
  546. }
  547. node->children[0]->surfaces = frontlist;
  548. node->children[1]->surfaces = backlist;
  549. }
  550. // =====================================================================================
  551. // RankForContents
  552. // =====================================================================================
  553. static int RankForContents(const int contents)
  554. {
  555. //Log("SolidBSP::RankForContents - contents type is %i ",contents);
  556. switch (contents)
  557. {
  558. #ifdef ZHLT_NULLTEX // AJM
  559. case CONTENTS_NULL:
  560. //Log("(null)\n");
  561. //return 13;
  562. return -2;
  563. #endif
  564. case CONTENTS_EMPTY:
  565. //Log("(empty)\n");
  566. return 0;
  567. case CONTENTS_WATER:
  568. //Log("(water)\n");
  569. return 1;
  570. case CONTENTS_TRANSLUCENT:
  571. //Log("(traslucent)\n");
  572. return 2;
  573. case CONTENTS_CURRENT_0:
  574. //Log("(current_0)\n");
  575. return 3;
  576. case CONTENTS_CURRENT_90:
  577. //Log("(current_90)\n");
  578. return 4;
  579. case CONTENTS_CURRENT_180:
  580. //Log("(current_180)\n");
  581. return 5;
  582. case CONTENTS_CURRENT_270:
  583. //Log("(current_270)\n");
  584. return 6;
  585. case CONTENTS_CURRENT_UP:
  586. //Log("(current_up)\n");
  587. return 7;
  588. case CONTENTS_CURRENT_DOWN:
  589. //Log("(current_down)\n");
  590. return 8;
  591. case CONTENTS_SLIME:
  592. //Log("(slime)\n");
  593. return 9;
  594. case CONTENTS_LAVA:
  595. //Log("(lava)\n");
  596. return 10;
  597. case CONTENTS_SKY:
  598. //Log("(sky)\n");
  599. return 11;
  600. case CONTENTS_SOLID:
  601. //Log("(solid)\n");
  602. return 12;
  603. #ifdef ZHLT_DETAIL
  604. case CONTENTS_DETAIL:
  605. return 13;
  606. //Log("(detail)\n");
  607. #endif
  608. default:
  609. hlassert(false);
  610. Error("RankForContents: bad contents %i (Possibly non-solid entity with clip-brush)", contents);
  611. }
  612. return -1;
  613. }
  614. // =====================================================================================
  615. // ContentsForRank
  616. // =====================================================================================
  617. static int ContentsForRank(const int rank)
  618. {
  619. switch (rank)
  620. {
  621. #ifdef ZHLT_NULLTEX // AJM
  622. case -2:
  623. return CONTENTS_NULL; // has at leat one face with null
  624. #endif
  625. case -1:
  626. return CONTENTS_SOLID; // no faces at all
  627. case 0:
  628. return CONTENTS_EMPTY;
  629. case 1:
  630. return CONTENTS_WATER;
  631. case 2:
  632. return CONTENTS_TRANSLUCENT;
  633. case 3:
  634. return CONTENTS_CURRENT_0;
  635. case 4:
  636. return CONTENTS_CURRENT_90;
  637. case 5:
  638. return CONTENTS_CURRENT_180;
  639. case 6:
  640. return CONTENTS_CURRENT_270;
  641. case 7:
  642. return CONTENTS_CURRENT_UP;
  643. case 8:
  644. return CONTENTS_CURRENT_DOWN;
  645. case 9:
  646. return CONTENTS_SLIME;
  647. case 10:
  648. return CONTENTS_LAVA;
  649. case 11:
  650. return CONTENTS_SKY;
  651. case 12:
  652. return CONTENTS_SOLID;
  653. #ifdef ZHLT_DETAIL // AJM
  654. case 13:
  655. return CONTENTS_DETAIL;
  656. #endif
  657. default:
  658. hlassert(false);
  659. Error("ContentsForRank: bad rank %i", rank);
  660. }
  661. return -1;
  662. }
  663. // =====================================================================================
  664. // FreeLeafSurfs
  665. // =====================================================================================
  666. static void FreeLeafSurfs(node_t* leaf)
  667. {
  668. surface_t* surf;
  669. surface_t* snext;
  670. face_t* f;
  671. face_t* fnext;
  672. for (surf = leaf->surfaces; surf; surf = snext)
  673. {
  674. snext = surf->next;
  675. for (f = surf->faces; f; f = fnext)
  676. {
  677. fnext = f->next;
  678. FreeFace(f);
  679. }
  680. FreeSurface(surf);
  681. }
  682. leaf->surfaces = NULL;
  683. }
  684. // =====================================================================================
  685. // LinkLeafFaces
  686. // Determines the contents of the leaf and creates the final list of original faces
  687. // that have some fragment inside this leaf
  688. // =====================================================================================
  689. #define MAX_LEAF_FACES 1024
  690. static void LinkLeafFaces(surface_t* planelist, node_t* leafnode)
  691. {
  692. face_t* f;
  693. surface_t* surf;
  694. int rank, r;
  695. int nummarkfaces;
  696. face_t* markfaces[MAX_LEAF_FACES];
  697. leafnode->faces = NULL;
  698. leafnode->planenum = -1;
  699. rank = -1;
  700. for (surf = planelist; surf; surf = surf->next)
  701. {
  702. for (f = surf->faces; f; f = f->next)
  703. {
  704. if ((f->contents == CONTENTS_HINT))
  705. {
  706. f->contents = CONTENTS_EMPTY;
  707. }
  708. r = RankForContents(f->contents);
  709. if (r > rank)
  710. {
  711. rank = r;
  712. }
  713. }
  714. }
  715. leafnode->contents = ContentsForRank(rank);
  716. if (leafnode->contents != CONTENTS_SOLID)
  717. {
  718. nummarkfaces = 0;
  719. for (surf = leafnode->surfaces; surf; surf = surf->next)
  720. {
  721. for (f = surf->faces; f; f = f->next)
  722. {
  723. hlassume(nummarkfaces < MAX_LEAF_FACES, assume_MAX_LEAF_FACES);
  724. markfaces[nummarkfaces++] = f->original;
  725. }
  726. }
  727. markfaces[nummarkfaces] = NULL; // end marker
  728. nummarkfaces++;
  729. leafnode->markfaces = (face_t**)malloc(nummarkfaces * sizeof(*leafnode->markfaces));
  730. memcpy(leafnode->markfaces, markfaces, nummarkfaces * sizeof(*leafnode->markfaces));
  731. }
  732. FreeLeafSurfs(leafnode);
  733. leafnode->surfaces = NULL;
  734. }
  735. // =====================================================================================
  736. // MakeNodePortal
  737. // Create the new portal by taking the full plane winding for the cutting plane and
  738. // clipping it by all of the planes from the other portals.
  739. // Each portal tracks the node that created it, so unused nodes can be removed later.
  740. // =====================================================================================
  741. static void MakeNodePortal(node_t* node)
  742. {
  743. portal_t* new_portal;
  744. portal_t* p;
  745. dplane_t* plane;
  746. dplane_t clipplane;
  747. Winding * w;
  748. int side = 0;
  749. plane = &g_dplanes[node->planenum];
  750. w = new Winding(*plane);
  751. new_portal = AllocPortal();
  752. new_portal->plane = *plane;
  753. new_portal->onnode = node;
  754. for (p = node->portals; p; p = p->next[side])
  755. {
  756. clipplane = p->plane;
  757. if (p->nodes[0] == node)
  758. {
  759. side = 0;
  760. }
  761. else if (p->nodes[1] == node)
  762. {
  763. clipplane.dist = -clipplane.dist;
  764. VectorSubtract(vec3_origin, clipplane.normal, clipplane.normal);
  765. side = 1;
  766. }
  767. else
  768. {
  769. Error("MakeNodePortal: mislinked portal");
  770. }
  771. w->Clip(clipplane, true);
  772. if (!w)
  773. {
  774. Warning("MakeNodePortal:new portal was clipped away from node@(%.0f,%.0f,%.0f)-(%.0f,%.0f,%.0f)",
  775. node->mins[0], node->mins[1], node->mins[2], node->maxs[0], node->maxs[1], node->maxs[2]);
  776. FreePortal(new_portal);
  777. return;
  778. }
  779. }
  780. new_portal->winding = w;
  781. AddPortalToNodes(new_portal, node->children[0], node->children[1]);
  782. }
  783. // =====================================================================================
  784. // SplitNodePortals
  785. // Move or split the portals that bound node so that the node's children have portals instead of node.
  786. // =====================================================================================
  787. static void SplitNodePortals(node_t *node)
  788. {
  789. portal_t* p;
  790. portal_t* next_portal;
  791. portal_t* new_portal;
  792. node_t* f;
  793. node_t* b;
  794. node_t* other_node;
  795. int side = 0;
  796. dplane_t* plane;
  797. Winding* frontwinding;
  798. Winding* backwinding;
  799. plane = &g_dplanes[node->planenum];
  800. f = node->children[0];
  801. b = node->children[1];
  802. for (p = node->portals; p; p = next_portal)
  803. {
  804. if (p->nodes[0] == node)
  805. {
  806. side = 0;
  807. }
  808. else if (p->nodes[1] == node)
  809. {
  810. side = 1;
  811. }
  812. else
  813. {
  814. Error("SplitNodePortals: mislinked portal");
  815. }
  816. next_portal = p->next[side];
  817. other_node = p->nodes[!side];
  818. RemovePortalFromNode(p, p->nodes[0]);
  819. RemovePortalFromNode(p, p->nodes[1]);
  820. // cut the portal into two portals, one on each side of the cut plane
  821. p->winding->Divide(*plane, &frontwinding, &backwinding);
  822. if (!frontwinding)
  823. {
  824. if (side == 0)
  825. {
  826. AddPortalToNodes(p, b, other_node);
  827. }
  828. else
  829. {
  830. AddPortalToNodes(p, other_node, b);
  831. }
  832. continue;
  833. }
  834. if (!backwinding)
  835. {
  836. if (side == 0)
  837. {
  838. AddPortalToNodes(p, f, other_node);
  839. }
  840. else
  841. {
  842. AddPortalToNodes(p, other_node, f);
  843. }
  844. continue;
  845. }
  846. // the winding is split
  847. new_portal = AllocPortal();
  848. *new_portal = *p;
  849. new_portal->winding = backwinding;
  850. delete p->winding;
  851. p->winding = frontwinding;
  852. if (side == 0)
  853. {
  854. AddPortalToNodes(p, f, other_node);
  855. AddPortalToNodes(new_portal, b, other_node);
  856. }
  857. else
  858. {
  859. AddPortalToNodes(p, other_node, f);
  860. AddPortalToNodes(new_portal, other_node, b);
  861. }
  862. }
  863. node->portals = NULL;
  864. }
  865. // =====================================================================================
  866. // CalcNodeBounds
  867. // Determines the boundaries of a node by minmaxing all the portal points, whcih
  868. // completely enclose the node.
  869. // Returns true if the node should be midsplit.(very large)
  870. // =====================================================================================
  871. static bool CalcNodeBounds(node_t* node)
  872. {
  873. int i;
  874. int j;
  875. vec_t v;
  876. portal_t* p;
  877. portal_t* next_portal;
  878. int side = 0;
  879. node->mins[0] = node->mins[1] = node->mins[2] = 9999;
  880. node->maxs[0] = node->maxs[1] = node->maxs[2] = -9999;
  881. for (p = node->portals; p; p = next_portal)
  882. {
  883. if (p->nodes[0] == node)
  884. {
  885. side = 0;
  886. }
  887. else if (p->nodes[1] == node)
  888. {
  889. side = 1;
  890. }
  891. else
  892. {
  893. Error("CalcNodeBounds: mislinked portal");
  894. }
  895. next_portal = p->next[side];
  896. for (i = 0; i < p->winding->m_NumPoints; i++)
  897. {
  898. for (j = 0; j < 3; j++)
  899. {
  900. v = p->winding->m_Points[i][j];
  901. if (v < node->mins[j])
  902. {
  903. node->mins[j] = v;
  904. }
  905. if (v > node->maxs[j])
  906. {
  907. node->maxs[j] = v;
  908. }
  909. }
  910. }
  911. }
  912. for (i = 0; i < 3; i++)
  913. {
  914. if (node->maxs[i] - node->mins[i] > g_maxnode_size)
  915. {
  916. return true;
  917. }
  918. }
  919. return false;
  920. }
  921. // =====================================================================================
  922. // CopyFacesToNode
  923. // Do a final merge attempt, then subdivide the faces to surface cache size if needed.
  924. // These are final faces that will be drawable in the game.
  925. // Copies of these faces are further chopped up into the leafs, but they will reference these originals.
  926. // =====================================================================================
  927. static void CopyFacesToNode(node_t* node, surface_t* surf)
  928. {
  929. face_t** prevptr;
  930. face_t* f;
  931. face_t* newf;
  932. // merge as much as possible
  933. MergePlaneFaces(surf);
  934. // subdivide large faces
  935. prevptr = &surf->faces;
  936. while (1)
  937. {
  938. f = *prevptr;
  939. if (!f)
  940. {
  941. break;
  942. }
  943. SubdivideFace(f, prevptr);
  944. f = *prevptr;
  945. prevptr = &f->next;
  946. }
  947. // copy the faces to the node, and consider them the originals
  948. node->surfaces = NULL;
  949. node->faces = NULL;
  950. for (f = surf->faces; f; f = f->next)
  951. {
  952. if (f->contents != CONTENTS_SOLID)
  953. {
  954. newf = AllocFace();
  955. *newf = *f;
  956. f->original = newf;
  957. newf->next = node->faces;
  958. node->faces = newf;
  959. }
  960. }
  961. }
  962. // =====================================================================================
  963. // BuildBspTree_r
  964. // =====================================================================================
  965. static void BuildBspTree_r(node_t* node)
  966. {
  967. surface_t* split;
  968. bool midsplit;
  969. surface_t* allsurfs;
  970. midsplit = CalcNodeBounds(node);
  971. split = SelectPartition(node->surfaces, node, midsplit);
  972. if (!split)
  973. { // this is a leaf node
  974. node->planenum = PLANENUM_LEAF;
  975. LinkLeafFaces(node->surfaces, node);
  976. return;
  977. }
  978. // these are final polygons
  979. split->onnode = node; // can't use again
  980. allsurfs = node->surfaces;
  981. node->planenum = split->planenum;
  982. node->faces = NULL;
  983. CopyFacesToNode(node, split);
  984. node->children[0] = AllocNode();
  985. node->children[1] = AllocNode();
  986. // split all the polysurfaces into front and back lists
  987. SplitNodeSurfaces(allsurfs, node);
  988. // create the portal that seperates the two children
  989. MakeNodePortal(node);
  990. // carve the portals on the boundaries of the node
  991. SplitNodePortals(node);
  992. // recursively do the children
  993. BuildBspTree_r(node->children[0]);
  994. BuildBspTree_r(node->children[1]);
  995. UpdateStatus();
  996. }
  997. // =====================================================================================
  998. // SolidBSP
  999. // Takes a chain of surfaces plus a split type, and returns a bsp tree with faces
  1000. // off the nodes.
  1001. // The original surface chain will be completely freed.
  1002. // =====================================================================================
  1003. node_t* SolidBSP(const surfchain_t* const surfhead, bool report_progress)
  1004. {
  1005. node_t* headnode;
  1006. ResetStatus(report_progress);
  1007. double start_time = I_FloatTime();
  1008. if(report_progress)
  1009. {
  1010. Log("SolidBSP [hull %d] ",g_hullnum);
  1011. }
  1012. else
  1013. {
  1014. Verbose("----- SolidBSP -----\n");
  1015. }
  1016. headnode = AllocNode();
  1017. headnode->surfaces = surfhead->surfaces;
  1018. if (!surfhead->surfaces)
  1019. {
  1020. // nothing at all to build
  1021. headnode->planenum = -1;
  1022. headnode->contents = CONTENTS_EMPTY;
  1023. return headnode;
  1024. }
  1025. // generate six portals that enclose the entire world
  1026. MakeHeadnodePortals(headnode, surfhead->mins, surfhead->maxs);
  1027. // recursively partition everything
  1028. BuildBspTree_r(headnode);
  1029. double end_time = I_FloatTime();
  1030. if(report_progress)
  1031. {
  1032. Log("%d (%.2f seconds)\n",++g_numProcessed,(end_time - start_time));
  1033. }
  1034. return headnode;
  1035. }