brushbsp.c 24 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331
  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. int c_nodes;
  20. int c_nonvis;
  21. int c_active_brushes;
  22. // if a brush just barely pokes onto the other side,
  23. // let it slide by without chopping
  24. #define PLANESIDE_EPSILON 0.001
  25. //0.1
  26. #define PSIDE_FRONT 1
  27. #define PSIDE_BACK 2
  28. #define PSIDE_BOTH (PSIDE_FRONT|PSIDE_BACK)
  29. #define PSIDE_FACING 4
  30. void FindBrushInTree (node_t *node, int brushnum)
  31. {
  32. bspbrush_t *b;
  33. if (node->planenum == PLANENUM_LEAF)
  34. {
  35. for (b=node->brushlist ; b ; b=b->next)
  36. if (b->original->brushnum == brushnum)
  37. printf ("here\n");
  38. return;
  39. }
  40. FindBrushInTree (node->children[0], brushnum);
  41. FindBrushInTree (node->children[1], brushnum);
  42. }
  43. //==================================================
  44. /*
  45. ================
  46. DrawBrushList
  47. ================
  48. */
  49. void DrawBrushList (bspbrush_t *brush, node_t *node)
  50. {
  51. int i;
  52. side_t *s;
  53. GLS_BeginScene ();
  54. for ( ; brush ; brush=brush->next)
  55. {
  56. for (i=0 ; i<brush->numsides ; i++)
  57. {
  58. s = &brush->sides[i];
  59. if (!s->winding)
  60. continue;
  61. if (s->texinfo == TEXINFO_NODE)
  62. GLS_Winding (s->winding, 1);
  63. else if (!s->visible)
  64. GLS_Winding (s->winding, 2);
  65. else
  66. GLS_Winding (s->winding, 0);
  67. }
  68. }
  69. GLS_EndScene ();
  70. }
  71. /*
  72. ================
  73. WriteBrushList
  74. ================
  75. */
  76. void WriteBrushList (char *name, bspbrush_t *brush, qboolean onlyvis)
  77. {
  78. int i;
  79. side_t *s;
  80. FILE *f;
  81. qprintf ("writing %s\n", name);
  82. f = SafeOpenWrite (name);
  83. for ( ; brush ; brush=brush->next)
  84. {
  85. for (i=0 ; i<brush->numsides ; i++)
  86. {
  87. s = &brush->sides[i];
  88. if (!s->winding)
  89. continue;
  90. if (onlyvis && !s->visible)
  91. continue;
  92. OutputWinding (brush->sides[i].winding, f);
  93. }
  94. }
  95. fclose (f);
  96. }
  97. void PrintBrush (bspbrush_t *brush)
  98. {
  99. int i;
  100. printf ("brush: %p\n", brush);
  101. for (i=0;i<brush->numsides ; i++)
  102. {
  103. pw(brush->sides[i].winding);
  104. printf ("\n");
  105. }
  106. }
  107. /*
  108. ==================
  109. BoundBrush
  110. Sets the mins/maxs based on the windings
  111. ==================
  112. */
  113. void BoundBrush (bspbrush_t *brush)
  114. {
  115. int i, j;
  116. winding_t *w;
  117. ClearBounds (brush->mins, brush->maxs);
  118. for (i=0 ; i<brush->numsides ; i++)
  119. {
  120. w = brush->sides[i].winding;
  121. if (!w)
  122. continue;
  123. for (j=0 ; j<w->numpoints ; j++)
  124. AddPointToBounds (w->p[j], brush->mins, brush->maxs);
  125. }
  126. }
  127. /*
  128. ==================
  129. CreateBrushWindings
  130. ==================
  131. */
  132. void CreateBrushWindings (bspbrush_t *brush)
  133. {
  134. int i, j;
  135. winding_t *w;
  136. side_t *side;
  137. plane_t *plane;
  138. for (i=0 ; i<brush->numsides ; i++)
  139. {
  140. side = &brush->sides[i];
  141. plane = &mapplanes[side->planenum];
  142. w = BaseWindingForPlane (plane->normal, plane->dist);
  143. for (j=0 ; j<brush->numsides && w; j++)
  144. {
  145. if (i == j)
  146. continue;
  147. if (brush->sides[j].bevel)
  148. continue;
  149. plane = &mapplanes[brush->sides[j].planenum^1];
  150. ChopWindingInPlace (&w, plane->normal, plane->dist, 0); //CLIP_EPSILON);
  151. }
  152. side->winding = w;
  153. }
  154. BoundBrush (brush);
  155. }
  156. /*
  157. ==================
  158. BrushFromBounds
  159. Creates a new axial brush
  160. ==================
  161. */
  162. bspbrush_t *BrushFromBounds (vec3_t mins, vec3_t maxs)
  163. {
  164. bspbrush_t *b;
  165. int i;
  166. vec3_t normal;
  167. vec_t dist;
  168. b = AllocBrush (6);
  169. b->numsides = 6;
  170. for (i=0 ; i<3 ; i++)
  171. {
  172. VectorClear (normal);
  173. normal[i] = 1;
  174. dist = maxs[i];
  175. b->sides[i].planenum = FindFloatPlane (normal, dist);
  176. normal[i] = -1;
  177. dist = -mins[i];
  178. b->sides[3+i].planenum = FindFloatPlane (normal, dist);
  179. }
  180. CreateBrushWindings (b);
  181. return b;
  182. }
  183. /*
  184. ==================
  185. BrushVolume
  186. ==================
  187. */
  188. vec_t BrushVolume (bspbrush_t *brush)
  189. {
  190. int i;
  191. winding_t *w;
  192. vec3_t corner;
  193. vec_t d, area, volume;
  194. plane_t *plane;
  195. if (!brush)
  196. return 0;
  197. // grab the first valid point as the corner
  198. w = NULL;
  199. for (i=0 ; i<brush->numsides ; i++)
  200. {
  201. w = brush->sides[i].winding;
  202. if (w)
  203. break;
  204. }
  205. if (!w)
  206. return 0;
  207. VectorCopy (w->p[0], corner);
  208. // make tetrahedrons to all other faces
  209. volume = 0;
  210. for ( ; i<brush->numsides ; i++)
  211. {
  212. w = brush->sides[i].winding;
  213. if (!w)
  214. continue;
  215. plane = &mapplanes[brush->sides[i].planenum];
  216. d = -(DotProduct (corner, plane->normal) - plane->dist);
  217. area = WindingArea (w);
  218. volume += d*area;
  219. }
  220. volume /= 3;
  221. return volume;
  222. }
  223. /*
  224. ================
  225. CountBrushList
  226. ================
  227. */
  228. int CountBrushList (bspbrush_t *brushes)
  229. {
  230. int c;
  231. c = 0;
  232. for ( ; brushes ; brushes = brushes->next)
  233. c++;
  234. return c;
  235. }
  236. /*
  237. ================
  238. AllocTree
  239. ================
  240. */
  241. tree_t *AllocTree (void)
  242. {
  243. tree_t *tree;
  244. tree = malloc(sizeof(*tree));
  245. memset (tree, 0, sizeof(*tree));
  246. ClearBounds (tree->mins, tree->maxs);
  247. return tree;
  248. }
  249. /*
  250. ================
  251. AllocNode
  252. ================
  253. */
  254. node_t *AllocNode (void)
  255. {
  256. node_t *node;
  257. node = malloc(sizeof(*node));
  258. memset (node, 0, sizeof(*node));
  259. return node;
  260. }
  261. /*
  262. ================
  263. AllocBrush
  264. ================
  265. */
  266. bspbrush_t *AllocBrush (int numsides)
  267. {
  268. bspbrush_t *bb;
  269. int c;
  270. c = (int)&(((bspbrush_t *)0)->sides[numsides]);
  271. bb = malloc(c);
  272. memset (bb, 0, c);
  273. if (numthreads == 1)
  274. c_active_brushes++;
  275. return bb;
  276. }
  277. /*
  278. ================
  279. FreeBrush
  280. ================
  281. */
  282. void FreeBrush (bspbrush_t *brushes)
  283. {
  284. int i;
  285. for (i=0 ; i<brushes->numsides ; i++)
  286. if (brushes->sides[i].winding)
  287. FreeWinding(brushes->sides[i].winding);
  288. free (brushes);
  289. if (numthreads == 1)
  290. c_active_brushes--;
  291. }
  292. /*
  293. ================
  294. FreeBrushList
  295. ================
  296. */
  297. void FreeBrushList (bspbrush_t *brushes)
  298. {
  299. bspbrush_t *next;
  300. for ( ; brushes ; brushes = next)
  301. {
  302. next = brushes->next;
  303. FreeBrush (brushes);
  304. }
  305. }
  306. /*
  307. ==================
  308. CopyBrush
  309. Duplicates the brush, the sides, and the windings
  310. ==================
  311. */
  312. bspbrush_t *CopyBrush (bspbrush_t *brush)
  313. {
  314. bspbrush_t *newbrush;
  315. int size;
  316. int i;
  317. size = (int)&(((bspbrush_t *)0)->sides[brush->numsides]);
  318. newbrush = AllocBrush (brush->numsides);
  319. memcpy (newbrush, brush, size);
  320. for (i=0 ; i<brush->numsides ; i++)
  321. {
  322. if (brush->sides[i].winding)
  323. newbrush->sides[i].winding = CopyWinding (brush->sides[i].winding);
  324. }
  325. return newbrush;
  326. }
  327. /*
  328. ==================
  329. PointInLeaf
  330. ==================
  331. */
  332. node_t *PointInLeaf (node_t *node, vec3_t point)
  333. {
  334. vec_t d;
  335. plane_t *plane;
  336. while (node->planenum != PLANENUM_LEAF)
  337. {
  338. plane = &mapplanes[node->planenum];
  339. d = DotProduct (point, plane->normal) - plane->dist;
  340. if (d > 0)
  341. node = node->children[0];
  342. else
  343. node = node->children[1];
  344. }
  345. return node;
  346. }
  347. //========================================================
  348. /*
  349. ==============
  350. BoxOnPlaneSide
  351. Returns PSIDE_FRONT, PSIDE_BACK, or PSIDE_BOTH
  352. ==============
  353. */
  354. int BoxOnPlaneSide (vec3_t mins, vec3_t maxs, plane_t *plane)
  355. {
  356. int side;
  357. int i;
  358. vec3_t corners[2];
  359. vec_t dist1, dist2;
  360. // axial planes are easy
  361. if (plane->type < 3)
  362. {
  363. side = 0;
  364. if (maxs[plane->type] > plane->dist+PLANESIDE_EPSILON)
  365. side |= PSIDE_FRONT;
  366. if (mins[plane->type] < plane->dist-PLANESIDE_EPSILON)
  367. side |= PSIDE_BACK;
  368. return side;
  369. }
  370. // create the proper leading and trailing verts for the box
  371. for (i=0 ; i<3 ; i++)
  372. {
  373. if (plane->normal[i] < 0)
  374. {
  375. corners[0][i] = mins[i];
  376. corners[1][i] = maxs[i];
  377. }
  378. else
  379. {
  380. corners[1][i] = mins[i];
  381. corners[0][i] = maxs[i];
  382. }
  383. }
  384. dist1 = DotProduct (plane->normal, corners[0]) - plane->dist;
  385. dist2 = DotProduct (plane->normal, corners[1]) - plane->dist;
  386. side = 0;
  387. if (dist1 >= PLANESIDE_EPSILON)
  388. side = PSIDE_FRONT;
  389. if (dist2 < PLANESIDE_EPSILON)
  390. side |= PSIDE_BACK;
  391. return side;
  392. }
  393. /*
  394. ============
  395. QuickTestBrushToPlanenum
  396. ============
  397. */
  398. int QuickTestBrushToPlanenum (bspbrush_t *brush, int planenum, int *numsplits)
  399. {
  400. int i, num;
  401. plane_t *plane;
  402. int s;
  403. *numsplits = 0;
  404. // if the brush actually uses the planenum,
  405. // we can tell the side for sure
  406. for (i=0 ; i<brush->numsides ; i++)
  407. {
  408. num = brush->sides[i].planenum;
  409. if (num >= 0x10000)
  410. Error ("bad planenum");
  411. if (num == planenum)
  412. return PSIDE_BACK|PSIDE_FACING;
  413. if (num == (planenum ^ 1) )
  414. return PSIDE_FRONT|PSIDE_FACING;
  415. }
  416. // box on plane side
  417. plane = &mapplanes[planenum];
  418. s = BoxOnPlaneSide (brush->mins, brush->maxs, plane);
  419. // if both sides, count the visible faces split
  420. if (s == PSIDE_BOTH)
  421. {
  422. *numsplits += 3;
  423. }
  424. return s;
  425. }
  426. /*
  427. ============
  428. TestBrushToPlanenum
  429. ============
  430. */
  431. int TestBrushToPlanenum (bspbrush_t *brush, int planenum,
  432. int *numsplits, qboolean *hintsplit, int *epsilonbrush)
  433. {
  434. int i, j, num;
  435. plane_t *plane;
  436. int s;
  437. winding_t *w;
  438. vec_t d, d_front, d_back;
  439. int front, back;
  440. *numsplits = 0;
  441. *hintsplit = false;
  442. // if the brush actually uses the planenum,
  443. // we can tell the side for sure
  444. for (i=0 ; i<brush->numsides ; i++)
  445. {
  446. num = brush->sides[i].planenum;
  447. if (num >= 0x10000)
  448. Error ("bad planenum");
  449. if (num == planenum)
  450. return PSIDE_BACK|PSIDE_FACING;
  451. if (num == (planenum ^ 1) )
  452. return PSIDE_FRONT|PSIDE_FACING;
  453. }
  454. // box on plane side
  455. plane = &mapplanes[planenum];
  456. s = BoxOnPlaneSide (brush->mins, brush->maxs, plane);
  457. if (s != PSIDE_BOTH)
  458. return s;
  459. // if both sides, count the visible faces split
  460. d_front = d_back = 0;
  461. for (i=0 ; i<brush->numsides ; i++)
  462. {
  463. if (brush->sides[i].texinfo == TEXINFO_NODE)
  464. continue; // on node, don't worry about splits
  465. if (!brush->sides[i].visible)
  466. continue; // we don't care about non-visible
  467. w = brush->sides[i].winding;
  468. if (!w)
  469. continue;
  470. front = back = 0;
  471. for (j=0 ; j<w->numpoints; j++)
  472. {
  473. d = DotProduct (w->p[j], plane->normal) - plane->dist;
  474. if (d > d_front)
  475. d_front = d;
  476. if (d < d_back)
  477. d_back = d;
  478. if (d > 0.1) // PLANESIDE_EPSILON)
  479. front = 1;
  480. if (d < -0.1) // PLANESIDE_EPSILON)
  481. back = 1;
  482. }
  483. if (front && back)
  484. {
  485. if ( !(brush->sides[i].surf & SURF_SKIP) )
  486. {
  487. (*numsplits)++;
  488. if (brush->sides[i].surf & SURF_HINT)
  489. *hintsplit = true;
  490. }
  491. }
  492. }
  493. if ( (d_front > 0.0 && d_front < 1.0)
  494. || (d_back < 0.0 && d_back > -1.0) )
  495. (*epsilonbrush)++;
  496. #if 0
  497. if (*numsplits == 0)
  498. { // didn't really need to be split
  499. if (front)
  500. s = PSIDE_FRONT;
  501. else if (back)
  502. s = PSIDE_BACK;
  503. else
  504. s = 0;
  505. }
  506. #endif
  507. return s;
  508. }
  509. //========================================================
  510. /*
  511. ================
  512. WindingIsTiny
  513. Returns true if the winding would be crunched out of
  514. existance by the vertex snapping.
  515. ================
  516. */
  517. #define EDGE_LENGTH 0.2
  518. qboolean WindingIsTiny (winding_t *w)
  519. {
  520. #if 0
  521. if (WindingArea (w) < 1)
  522. return true;
  523. return false;
  524. #else
  525. int i, j;
  526. vec_t len;
  527. vec3_t delta;
  528. int edges;
  529. edges = 0;
  530. for (i=0 ; i<w->numpoints ; i++)
  531. {
  532. j = i == w->numpoints - 1 ? 0 : i+1;
  533. VectorSubtract (w->p[j], w->p[i], delta);
  534. len = VectorLength (delta);
  535. if (len > EDGE_LENGTH)
  536. {
  537. if (++edges == 3)
  538. return false;
  539. }
  540. }
  541. return true;
  542. #endif
  543. }
  544. /*
  545. ================
  546. WindingIsHuge
  547. Returns true if the winding still has one of the points
  548. from basewinding for plane
  549. ================
  550. */
  551. qboolean WindingIsHuge (winding_t *w)
  552. {
  553. int i, j;
  554. for (i=0 ; i<w->numpoints ; i++)
  555. {
  556. for (j=0 ; j<3 ; j++)
  557. if (w->p[i][j] < -8000 || w->p[i][j] > 8000)
  558. return true;
  559. }
  560. return false;
  561. }
  562. //============================================================
  563. /*
  564. ================
  565. Leafnode
  566. ================
  567. */
  568. void LeafNode (node_t *node, bspbrush_t *brushes)
  569. {
  570. bspbrush_t *b;
  571. int i;
  572. node->planenum = PLANENUM_LEAF;
  573. node->contents = 0;
  574. for (b=brushes ; b ; b=b->next)
  575. {
  576. // if the brush is solid and all of its sides are on nodes,
  577. // it eats everything
  578. if (b->original->contents & CONTENTS_SOLID)
  579. {
  580. for (i=0 ; i<b->numsides ; i++)
  581. if (b->sides[i].texinfo != TEXINFO_NODE)
  582. break;
  583. if (i == b->numsides)
  584. {
  585. node->contents = CONTENTS_SOLID;
  586. break;
  587. }
  588. }
  589. node->contents |= b->original->contents;
  590. }
  591. node->brushlist = brushes;
  592. }
  593. //============================================================
  594. void CheckPlaneAgainstParents (int pnum, node_t *node)
  595. {
  596. node_t *p;
  597. for (p=node->parent ; p ; p=p->parent)
  598. {
  599. if (p->planenum == pnum)
  600. Error ("Tried parent");
  601. }
  602. }
  603. qboolean CheckPlaneAgainstVolume (int pnum, node_t *node)
  604. {
  605. bspbrush_t *front, *back;
  606. qboolean good;
  607. SplitBrush (node->volume, pnum, &front, &back);
  608. good = (front && back);
  609. if (front)
  610. FreeBrush (front);
  611. if (back)
  612. FreeBrush (back);
  613. return good;
  614. }
  615. /*
  616. ================
  617. SelectSplitSide
  618. Using a hueristic, choses one of the sides out of the brushlist
  619. to partition the brushes with.
  620. Returns NULL if there are no valid planes to split with..
  621. ================
  622. */
  623. side_t *SelectSplitSide (bspbrush_t *brushes, node_t *node)
  624. {
  625. int value, bestvalue;
  626. bspbrush_t *brush, *test;
  627. side_t *side, *bestside;
  628. int i, j, pass, numpasses;
  629. int pnum;
  630. int s;
  631. int front, back, both, facing, splits;
  632. int bsplits;
  633. int bestsplits;
  634. int epsilonbrush;
  635. qboolean hintsplit;
  636. bestside = NULL;
  637. bestvalue = -99999;
  638. bestsplits = 0;
  639. // the search order goes: visible-structural, visible-detail,
  640. // nonvisible-structural, nonvisible-detail.
  641. // If any valid plane is available in a pass, no further
  642. // passes will be tried.
  643. numpasses = 4;
  644. for (pass = 0 ; pass < numpasses ; pass++)
  645. {
  646. for (brush = brushes ; brush ; brush=brush->next)
  647. {
  648. if ( (pass & 1) && !(brush->original->contents & CONTENTS_DETAIL) )
  649. continue;
  650. if ( !(pass & 1) && (brush->original->contents & CONTENTS_DETAIL) )
  651. continue;
  652. for (i=0 ; i<brush->numsides ; i++)
  653. {
  654. side = brush->sides + i;
  655. if (side->bevel)
  656. continue; // never use a bevel as a spliter
  657. if (!side->winding)
  658. continue; // nothing visible, so it can't split
  659. if (side->texinfo == TEXINFO_NODE)
  660. continue; // allready a node splitter
  661. if (side->tested)
  662. continue; // we allready have metrics for this plane
  663. if (side->surf & SURF_SKIP)
  664. continue; // skip surfaces are never chosen
  665. if ( side->visible ^ (pass<2) )
  666. continue; // only check visible faces on first pass
  667. pnum = side->planenum;
  668. pnum &= ~1; // allways use positive facing plane
  669. CheckPlaneAgainstParents (pnum, node);
  670. if (!CheckPlaneAgainstVolume (pnum, node))
  671. continue; // would produce a tiny volume
  672. front = 0;
  673. back = 0;
  674. both = 0;
  675. facing = 0;
  676. splits = 0;
  677. epsilonbrush = 0;
  678. for (test = brushes ; test ; test=test->next)
  679. {
  680. s = TestBrushToPlanenum (test, pnum, &bsplits, &hintsplit, &epsilonbrush);
  681. splits += bsplits;
  682. if (bsplits && (s&PSIDE_FACING) )
  683. Error ("PSIDE_FACING with splits");
  684. test->testside = s;
  685. // if the brush shares this face, don't bother
  686. // testing that facenum as a splitter again
  687. if (s & PSIDE_FACING)
  688. {
  689. facing++;
  690. for (j=0 ; j<test->numsides ; j++)
  691. {
  692. if ( (test->sides[j].planenum&~1) == pnum)
  693. test->sides[j].tested = true;
  694. }
  695. }
  696. if (s & PSIDE_FRONT)
  697. front++;
  698. if (s & PSIDE_BACK)
  699. back++;
  700. if (s == PSIDE_BOTH)
  701. both++;
  702. }
  703. // give a value estimate for using this plane
  704. value = 5*facing - 5*splits - abs(front-back);
  705. // value = -5*splits;
  706. // value = 5*facing - 5*splits;
  707. if (mapplanes[pnum].type < 3)
  708. value+=5; // axial is better
  709. value -= epsilonbrush*1000; // avoid!
  710. // never split a hint side except with another hint
  711. if (hintsplit && !(side->surf & SURF_HINT) )
  712. value = -9999999;
  713. // save off the side test so we don't need
  714. // to recalculate it when we actually seperate
  715. // the brushes
  716. if (value > bestvalue)
  717. {
  718. bestvalue = value;
  719. bestside = side;
  720. bestsplits = splits;
  721. for (test = brushes ; test ; test=test->next)
  722. test->side = test->testside;
  723. }
  724. }
  725. }
  726. // if we found a good plane, don't bother trying any
  727. // other passes
  728. if (bestside)
  729. {
  730. if (pass > 1)
  731. {
  732. if (numthreads == 1)
  733. c_nonvis++;
  734. }
  735. if (pass > 0)
  736. node->detail_seperator = true; // not needed for vis
  737. break;
  738. }
  739. }
  740. //
  741. // clear all the tested flags we set
  742. //
  743. for (brush = brushes ; brush ; brush=brush->next)
  744. {
  745. for (i=0 ; i<brush->numsides ; i++)
  746. brush->sides[i].tested = false;
  747. }
  748. return bestside;
  749. }
  750. /*
  751. ==================
  752. BrushMostlyOnSide
  753. ==================
  754. */
  755. int BrushMostlyOnSide (bspbrush_t *brush, plane_t *plane)
  756. {
  757. int i, j;
  758. winding_t *w;
  759. vec_t d, max;
  760. int side;
  761. max = 0;
  762. side = PSIDE_FRONT;
  763. for (i=0 ; i<brush->numsides ; i++)
  764. {
  765. w = brush->sides[i].winding;
  766. if (!w)
  767. continue;
  768. for (j=0 ; j<w->numpoints ; j++)
  769. {
  770. d = DotProduct (w->p[j], plane->normal) - plane->dist;
  771. if (d > max)
  772. {
  773. max = d;
  774. side = PSIDE_FRONT;
  775. }
  776. if (-d > max)
  777. {
  778. max = -d;
  779. side = PSIDE_BACK;
  780. }
  781. }
  782. }
  783. return side;
  784. }
  785. /*
  786. ================
  787. SplitBrush
  788. Generates two new brushes, leaving the original
  789. unchanged
  790. ================
  791. */
  792. void SplitBrush (bspbrush_t *brush, int planenum,
  793. bspbrush_t **front, bspbrush_t **back)
  794. {
  795. bspbrush_t *b[2];
  796. int i, j;
  797. winding_t *w, *cw[2], *midwinding;
  798. plane_t *plane, *plane2;
  799. side_t *s, *cs;
  800. float d, d_front, d_back;
  801. *front = *back = NULL;
  802. plane = &mapplanes[planenum];
  803. // check all points
  804. d_front = d_back = 0;
  805. for (i=0 ; i<brush->numsides ; i++)
  806. {
  807. w = brush->sides[i].winding;
  808. if (!w)
  809. continue;
  810. for (j=0 ; j<w->numpoints ; j++)
  811. {
  812. d = DotProduct (w->p[j], plane->normal) - plane->dist;
  813. if (d > 0 && d > d_front)
  814. d_front = d;
  815. if (d < 0 && d < d_back)
  816. d_back = d;
  817. }
  818. }
  819. if (d_front < 0.1) // PLANESIDE_EPSILON)
  820. { // only on back
  821. *back = CopyBrush (brush);
  822. return;
  823. }
  824. if (d_back > -0.1) // PLANESIDE_EPSILON)
  825. { // only on front
  826. *front = CopyBrush (brush);
  827. return;
  828. }
  829. // create a new winding from the split plane
  830. w = BaseWindingForPlane (plane->normal, plane->dist);
  831. for (i=0 ; i<brush->numsides && w ; i++)
  832. {
  833. plane2 = &mapplanes[brush->sides[i].planenum ^ 1];
  834. ChopWindingInPlace (&w, plane2->normal, plane2->dist, 0); // PLANESIDE_EPSILON);
  835. }
  836. if (!w || WindingIsTiny (w) )
  837. { // the brush isn't really split
  838. int side;
  839. side = BrushMostlyOnSide (brush, plane);
  840. if (side == PSIDE_FRONT)
  841. *front = CopyBrush (brush);
  842. if (side == PSIDE_BACK)
  843. *back = CopyBrush (brush);
  844. return;
  845. }
  846. if (WindingIsHuge (w))
  847. {
  848. qprintf ("WARNING: huge winding\n");
  849. }
  850. midwinding = w;
  851. // split it for real
  852. for (i=0 ; i<2 ; i++)
  853. {
  854. b[i] = AllocBrush (brush->numsides+1);
  855. b[i]->original = brush->original;
  856. }
  857. // split all the current windings
  858. for (i=0 ; i<brush->numsides ; i++)
  859. {
  860. s = &brush->sides[i];
  861. w = s->winding;
  862. if (!w)
  863. continue;
  864. ClipWindingEpsilon (w, plane->normal, plane->dist,
  865. 0 /*PLANESIDE_EPSILON*/, &cw[0], &cw[1]);
  866. for (j=0 ; j<2 ; j++)
  867. {
  868. if (!cw[j])
  869. continue;
  870. #if 0
  871. if (WindingIsTiny (cw[j]))
  872. {
  873. FreeWinding (cw[j]);
  874. continue;
  875. }
  876. #endif
  877. cs = &b[j]->sides[b[j]->numsides];
  878. b[j]->numsides++;
  879. *cs = *s;
  880. // cs->planenum = s->planenum;
  881. // cs->texinfo = s->texinfo;
  882. // cs->visible = s->visible;
  883. // cs->original = s->original;
  884. cs->winding = cw[j];
  885. cs->tested = false;
  886. }
  887. }
  888. // see if we have valid polygons on both sides
  889. for (i=0 ; i<2 ; i++)
  890. {
  891. BoundBrush (b[i]);
  892. for (j=0 ; j<3 ; j++)
  893. {
  894. if (b[i]->mins[j] < -4096 || b[i]->maxs[j] > 4096)
  895. {
  896. qprintf ("bogus brush after clip\n");
  897. break;
  898. }
  899. }
  900. if (b[i]->numsides < 3 || j < 3)
  901. {
  902. FreeBrush (b[i]);
  903. b[i] = NULL;
  904. }
  905. }
  906. if ( !(b[0] && b[1]) )
  907. {
  908. if (!b[0] && !b[1])
  909. qprintf ("split removed brush\n");
  910. else
  911. qprintf ("split not on both sides\n");
  912. if (b[0])
  913. {
  914. FreeBrush (b[0]);
  915. *front = CopyBrush (brush);
  916. }
  917. if (b[1])
  918. {
  919. FreeBrush (b[1]);
  920. *back = CopyBrush (brush);
  921. }
  922. return;
  923. }
  924. // add the midwinding to both sides
  925. for (i=0 ; i<2 ; i++)
  926. {
  927. cs = &b[i]->sides[b[i]->numsides];
  928. b[i]->numsides++;
  929. cs->planenum = planenum^i^1;
  930. cs->texinfo = TEXINFO_NODE;
  931. cs->visible = false;
  932. cs->tested = false;
  933. if (i==0)
  934. cs->winding = CopyWinding (midwinding);
  935. else
  936. cs->winding = midwinding;
  937. }
  938. {
  939. vec_t v1;
  940. int i;
  941. for (i=0 ; i<2 ; i++)
  942. {
  943. v1 = BrushVolume (b[i]);
  944. if (v1 < 1.0)
  945. {
  946. FreeBrush (b[i]);
  947. b[i] = NULL;
  948. // qprintf ("tiny volume after clip\n");
  949. }
  950. }
  951. }
  952. *front = b[0];
  953. *back = b[1];
  954. }
  955. /*
  956. ================
  957. SplitBrushList
  958. ================
  959. */
  960. void SplitBrushList (bspbrush_t *brushes,
  961. node_t *node, bspbrush_t **front, bspbrush_t **back)
  962. {
  963. bspbrush_t *brush, *newbrush, *newbrush2;
  964. side_t *side;
  965. int sides;
  966. int i;
  967. *front = *back = NULL;
  968. for (brush = brushes ; brush ; brush=brush->next)
  969. {
  970. sides = brush->side;
  971. if (sides == PSIDE_BOTH)
  972. { // split into two brushes
  973. SplitBrush (brush, node->planenum, &newbrush, &newbrush2);
  974. if (newbrush)
  975. {
  976. newbrush->next = *front;
  977. *front = newbrush;
  978. }
  979. if (newbrush2)
  980. {
  981. newbrush2->next = *back;
  982. *back = newbrush2;
  983. }
  984. continue;
  985. }
  986. newbrush = CopyBrush (brush);
  987. // if the planenum is actualy a part of the brush
  988. // find the plane and flag it as used so it won't be tried
  989. // as a splitter again
  990. if (sides & PSIDE_FACING)
  991. {
  992. for (i=0 ; i<newbrush->numsides ; i++)
  993. {
  994. side = newbrush->sides + i;
  995. if ( (side->planenum& ~1) == node->planenum)
  996. side->texinfo = TEXINFO_NODE;
  997. }
  998. }
  999. if (sides & PSIDE_FRONT)
  1000. {
  1001. newbrush->next = *front;
  1002. *front = newbrush;
  1003. continue;
  1004. }
  1005. if (sides & PSIDE_BACK)
  1006. {
  1007. newbrush->next = *back;
  1008. *back = newbrush;
  1009. continue;
  1010. }
  1011. }
  1012. }
  1013. /*
  1014. ================
  1015. BuildTree_r
  1016. ================
  1017. */
  1018. node_t *BuildTree_r (node_t *node, bspbrush_t *brushes)
  1019. {
  1020. node_t *newnode;
  1021. side_t *bestside;
  1022. int i;
  1023. bspbrush_t *children[2];
  1024. if (numthreads == 1)
  1025. c_nodes++;
  1026. if (drawflag)
  1027. DrawBrushList (brushes, node);
  1028. // find the best plane to use as a splitter
  1029. bestside = SelectSplitSide (brushes, node);
  1030. if (!bestside)
  1031. {
  1032. // leaf node
  1033. node->side = NULL;
  1034. node->planenum = -1;
  1035. LeafNode (node, brushes);
  1036. return node;
  1037. }
  1038. // this is a splitplane node
  1039. node->side = bestside;
  1040. node->planenum = bestside->planenum & ~1; // always use front facing
  1041. SplitBrushList (brushes, node, &children[0], &children[1]);
  1042. FreeBrushList (brushes);
  1043. // allocate children before recursing
  1044. for (i=0 ; i<2 ; i++)
  1045. {
  1046. newnode = AllocNode ();
  1047. newnode->parent = node;
  1048. node->children[i] = newnode;
  1049. }
  1050. SplitBrush (node->volume, node->planenum, &node->children[0]->volume,
  1051. &node->children[1]->volume);
  1052. // recursively process children
  1053. for (i=0 ; i<2 ; i++)
  1054. {
  1055. node->children[i] = BuildTree_r (node->children[i], children[i]);
  1056. }
  1057. return node;
  1058. }
  1059. //===========================================================
  1060. /*
  1061. =================
  1062. BrushBSP
  1063. The incoming list will be freed before exiting
  1064. =================
  1065. */
  1066. tree_t *BrushBSP (bspbrush_t *brushlist, vec3_t mins, vec3_t maxs)
  1067. {
  1068. node_t *node;
  1069. bspbrush_t *b;
  1070. int c_faces, c_nonvisfaces;
  1071. int c_brushes;
  1072. tree_t *tree;
  1073. int i;
  1074. vec_t volume;
  1075. qprintf ("--- BrushBSP ---\n");
  1076. tree = AllocTree ();
  1077. c_faces = 0;
  1078. c_nonvisfaces = 0;
  1079. c_brushes = 0;
  1080. for (b=brushlist ; b ; b=b->next)
  1081. {
  1082. c_brushes++;
  1083. volume = BrushVolume (b);
  1084. if (volume < microvolume)
  1085. {
  1086. printf ("WARNING: entity %i, brush %i: microbrush\n",
  1087. b->original->entitynum, b->original->brushnum);
  1088. }
  1089. for (i=0 ; i<b->numsides ; i++)
  1090. {
  1091. if (b->sides[i].bevel)
  1092. continue;
  1093. if (!b->sides[i].winding)
  1094. continue;
  1095. if (b->sides[i].texinfo == TEXINFO_NODE)
  1096. continue;
  1097. if (b->sides[i].visible)
  1098. c_faces++;
  1099. else
  1100. c_nonvisfaces++;
  1101. }
  1102. AddPointToBounds (b->mins, tree->mins, tree->maxs);
  1103. AddPointToBounds (b->maxs, tree->mins, tree->maxs);
  1104. }
  1105. qprintf ("%5i brushes\n", c_brushes);
  1106. qprintf ("%5i visible faces\n", c_faces);
  1107. qprintf ("%5i nonvisible faces\n", c_nonvisfaces);
  1108. c_nodes = 0;
  1109. c_nonvis = 0;
  1110. node = AllocNode ();
  1111. node->volume = BrushFromBounds (mins, maxs);
  1112. tree->headnode = node;
  1113. node = BuildTree_r (node, brushlist);
  1114. qprintf ("%5i visible nodes\n", c_nodes/2 - c_nonvis);
  1115. qprintf ("%5i nonvis nodes\n", c_nonvis);
  1116. qprintf ("%5i leafs\n", (c_nodes+1)/2);
  1117. #if 0
  1118. { // debug code
  1119. static node_t *tnode;
  1120. vec3_t p;
  1121. p[0] = -1469;
  1122. p[1] = -118;
  1123. p[2] = 119;
  1124. tnode = PointInLeaf (tree->headnode, p);
  1125. printf ("contents: %i\n", tnode->contents);
  1126. p[0] = 0;
  1127. }
  1128. #endif
  1129. return tree;
  1130. }