faces.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979
  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. // faces.c
  19. #include "qbsp.h"
  20. #include "l_mem.h"
  21. /*
  22. some faces will be removed before saving, but still form nodes:
  23. the insides of sky volumes
  24. meeting planes of different water current volumes
  25. */
  26. // undefine for dumb linear searches
  27. #define USE_HASHING
  28. #define INTEGRAL_EPSILON 0.01
  29. #define POINT_EPSILON 0.5
  30. #define OFF_EPSILON 0.5
  31. int c_merge;
  32. int c_subdivide;
  33. int c_totalverts;
  34. int c_uniqueverts;
  35. int c_degenerate;
  36. int c_tjunctions;
  37. int c_faceoverflows;
  38. int c_facecollapse;
  39. int c_badstartverts;
  40. #define MAX_SUPERVERTS 512
  41. int superverts[MAX_SUPERVERTS];
  42. int numsuperverts;
  43. face_t *edgefaces[MAX_MAP_EDGES][2];
  44. int firstmodeledge = 1;
  45. int firstmodelface;
  46. int c_tryedges;
  47. vec3_t edge_dir;
  48. vec3_t edge_start;
  49. vec_t edge_len;
  50. int num_edge_verts;
  51. int edge_verts[MAX_MAP_VERTS];
  52. face_t *NewFaceFromFace (face_t *f);
  53. //===========================================================================
  54. typedef struct hashvert_s
  55. {
  56. struct hashvert_s *next;
  57. int num;
  58. } hashvert_t;
  59. #define HASH_SIZE 64
  60. int vertexchain[MAX_MAP_VERTS]; // the next vertex in a hash chain
  61. int hashverts[HASH_SIZE*HASH_SIZE]; // a vertex number, or 0 for no verts
  62. face_t *edgefaces[MAX_MAP_EDGES][2];
  63. //============================================================================
  64. unsigned HashVec (vec3_t vec)
  65. {
  66. int x, y;
  67. x = (4096 + (int)(vec[0]+0.5)) >> 7;
  68. y = (4096 + (int)(vec[1]+0.5)) >> 7;
  69. if ( x < 0 || x >= HASH_SIZE || y < 0 || y >= HASH_SIZE )
  70. Error ("HashVec: point outside valid range");
  71. return y*HASH_SIZE + x;
  72. }
  73. #ifdef USE_HASHING
  74. /*
  75. =============
  76. GetVertex
  77. Uses hashing
  78. =============
  79. */
  80. int GetVertexnum (vec3_t in)
  81. {
  82. int h;
  83. int i;
  84. float *p;
  85. vec3_t vert;
  86. int vnum;
  87. c_totalverts++;
  88. for (i=0 ; i<3 ; i++)
  89. {
  90. if ( fabs(in[i] - Q_rint(in[i])) < INTEGRAL_EPSILON)
  91. vert[i] = Q_rint(in[i]);
  92. else
  93. vert[i] = in[i];
  94. }
  95. h = HashVec (vert);
  96. for (vnum=hashverts[h] ; vnum ; vnum=vertexchain[vnum])
  97. {
  98. p = dvertexes[vnum].point;
  99. if ( fabs(p[0]-vert[0])<POINT_EPSILON
  100. && fabs(p[1]-vert[1])<POINT_EPSILON
  101. && fabs(p[2]-vert[2])<POINT_EPSILON )
  102. return vnum;
  103. }
  104. // emit a vertex
  105. if (numvertexes == MAX_MAP_VERTS)
  106. Error ("numvertexes == MAX_MAP_VERTS");
  107. dvertexes[numvertexes].point[0] = vert[0];
  108. dvertexes[numvertexes].point[1] = vert[1];
  109. dvertexes[numvertexes].point[2] = vert[2];
  110. vertexchain[numvertexes] = hashverts[h];
  111. hashverts[h] = numvertexes;
  112. c_uniqueverts++;
  113. numvertexes++;
  114. return numvertexes-1;
  115. }
  116. #else
  117. /*
  118. ==================
  119. GetVertexnum
  120. Dumb linear search
  121. ==================
  122. */
  123. int GetVertexnum (vec3_t v)
  124. {
  125. int i, j;
  126. dvertex_t *dv;
  127. vec_t d;
  128. c_totalverts++;
  129. // make really close values exactly integral
  130. for (i=0 ; i<3 ; i++)
  131. {
  132. if ( fabs(v[i] - (int)(v[i]+0.5)) < INTEGRAL_EPSILON )
  133. v[i] = (int)(v[i]+0.5);
  134. if (v[i] < -4096 || v[i] > 4096)
  135. Error ("GetVertexnum: outside +/- 4096");
  136. }
  137. // search for an existing vertex match
  138. for (i=0, dv=dvertexes ; i<numvertexes ; i++, dv++)
  139. {
  140. for (j=0 ; j<3 ; j++)
  141. {
  142. d = v[j] - dv->point[j];
  143. if ( d > POINT_EPSILON || d < -POINT_EPSILON)
  144. break;
  145. }
  146. if (j == 3)
  147. return i; // a match
  148. }
  149. // new point
  150. if (numvertexes == MAX_MAP_VERTS)
  151. Error ("MAX_MAP_VERTS");
  152. VectorCopy (v, dv->point);
  153. numvertexes++;
  154. c_uniqueverts++;
  155. return numvertexes-1;
  156. }
  157. #endif
  158. /*
  159. ==================
  160. FaceFromSuperverts
  161. The faces vertexes have been added to the superverts[] array,
  162. and there may be more there than can be held in a face (MAXEDGES).
  163. If less, the faces vertexnums[] will be filled in, otherwise
  164. face will reference a tree of split[] faces until all of the
  165. vertexnums can be added.
  166. superverts[base] will become face->vertexnums[0], and the others
  167. will be circularly filled in.
  168. ==================
  169. */
  170. void FaceFromSuperverts (node_t *node, face_t *f, int base)
  171. {
  172. face_t *newf;
  173. int remaining;
  174. int i;
  175. remaining = numsuperverts;
  176. while (remaining > MAXEDGES)
  177. { // must split into two faces, because of vertex overload
  178. c_faceoverflows++;
  179. newf = f->split[0] = NewFaceFromFace (f);
  180. newf = f->split[0];
  181. newf->next = node->faces;
  182. node->faces = newf;
  183. newf->numpoints = MAXEDGES;
  184. for (i=0 ; i<MAXEDGES ; i++)
  185. newf->vertexnums[i] = superverts[(i+base)%numsuperverts];
  186. f->split[1] = NewFaceFromFace (f);
  187. f = f->split[1];
  188. f->next = node->faces;
  189. node->faces = f;
  190. remaining -= (MAXEDGES-2);
  191. base = (base+MAXEDGES-1)%numsuperverts;
  192. }
  193. // copy the vertexes back to the face
  194. f->numpoints = remaining;
  195. for (i=0 ; i<remaining ; i++)
  196. f->vertexnums[i] = superverts[(i+base)%numsuperverts];
  197. }
  198. /*
  199. ==================
  200. EmitFaceVertexes
  201. ==================
  202. */
  203. void EmitFaceVertexes (node_t *node, face_t *f)
  204. {
  205. winding_t *w;
  206. int i;
  207. if (f->merged || f->split[0] || f->split[1])
  208. return;
  209. w = f->w;
  210. for (i=0 ; i<w->numpoints ; i++)
  211. {
  212. if (noweld)
  213. { // make every point unique
  214. if (numvertexes == MAX_MAP_VERTS)
  215. Error ("MAX_MAP_VERTS");
  216. superverts[i] = numvertexes;
  217. VectorCopy (w->p[i], dvertexes[numvertexes].point);
  218. numvertexes++;
  219. c_uniqueverts++;
  220. c_totalverts++;
  221. }
  222. else
  223. superverts[i] = GetVertexnum (w->p[i]);
  224. }
  225. numsuperverts = w->numpoints;
  226. // this may fragment the face if > MAXEDGES
  227. FaceFromSuperverts (node, f, 0);
  228. }
  229. /*
  230. ==================
  231. EmitVertexes_r
  232. ==================
  233. */
  234. void EmitVertexes_r (node_t *node)
  235. {
  236. int i;
  237. face_t *f;
  238. if (node->planenum == PLANENUM_LEAF)
  239. return;
  240. for (f=node->faces ; f ; f=f->next)
  241. {
  242. EmitFaceVertexes (node, f);
  243. }
  244. for (i=0 ; i<2 ; i++)
  245. EmitVertexes_r (node->children[i]);
  246. }
  247. #ifdef USE_HASHING
  248. /*
  249. ==========
  250. FindEdgeVerts
  251. Uses the hash tables to cut down to a small number
  252. ==========
  253. */
  254. void FindEdgeVerts (vec3_t v1, vec3_t v2)
  255. {
  256. int x1, x2, y1, y2, t;
  257. int x, y;
  258. int vnum;
  259. #if 0
  260. {
  261. int i;
  262. num_edge_verts = numvertexes-1;
  263. for (i=0 ; i<numvertexes-1 ; i++)
  264. edge_verts[i] = i+1;
  265. }
  266. #endif
  267. x1 = (4096 + (int)(v1[0]+0.5)) >> 7;
  268. y1 = (4096 + (int)(v1[1]+0.5)) >> 7;
  269. x2 = (4096 + (int)(v2[0]+0.5)) >> 7;
  270. y2 = (4096 + (int)(v2[1]+0.5)) >> 7;
  271. if (x1 > x2)
  272. {
  273. t = x1;
  274. x1 = x2;
  275. x2 = t;
  276. }
  277. if (y1 > y2)
  278. {
  279. t = y1;
  280. y1 = y2;
  281. y2 = t;
  282. }
  283. #if 0
  284. x1--;
  285. x2++;
  286. y1--;
  287. y2++;
  288. if (x1 < 0)
  289. x1 = 0;
  290. if (x2 >= HASH_SIZE)
  291. x2 = HASH_SIZE;
  292. if (y1 < 0)
  293. y1 = 0;
  294. if (y2 >= HASH_SIZE)
  295. y2 = HASH_SIZE;
  296. #endif
  297. num_edge_verts = 0;
  298. for (x=x1 ; x <= x2 ; x++)
  299. {
  300. for (y=y1 ; y <= y2 ; y++)
  301. {
  302. for (vnum=hashverts[y*HASH_SIZE+x] ; vnum ; vnum=vertexchain[vnum])
  303. {
  304. edge_verts[num_edge_verts++] = vnum;
  305. }
  306. }
  307. }
  308. }
  309. #else
  310. /*
  311. ==========
  312. FindEdgeVerts
  313. Forced a dumb check of everything
  314. ==========
  315. */
  316. void FindEdgeVerts (vec3_t v1, vec3_t v2)
  317. {
  318. int i;
  319. num_edge_verts = numvertexes-1;
  320. for (i=0 ; i<num_edge_verts ; i++)
  321. edge_verts[i] = i+1;
  322. }
  323. #endif
  324. /*
  325. ==========
  326. TestEdge
  327. Can be recursively reentered
  328. ==========
  329. */
  330. void TestEdge (vec_t start, vec_t end, int p1, int p2, int startvert)
  331. {
  332. int j, k;
  333. vec_t dist;
  334. vec3_t delta;
  335. vec3_t exact;
  336. vec3_t off;
  337. vec_t error;
  338. vec3_t p;
  339. if (p1 == p2)
  340. {
  341. c_degenerate++;
  342. return; // degenerate edge
  343. }
  344. for (k=startvert ; k<num_edge_verts ; k++)
  345. {
  346. j = edge_verts[k];
  347. if (j==p1 || j == p2)
  348. continue;
  349. VectorCopy (dvertexes[j].point, p);
  350. VectorSubtract (p, edge_start, delta);
  351. dist = DotProduct (delta, edge_dir);
  352. if (dist <=start || dist >= end)
  353. continue; // off an end
  354. VectorMA (edge_start, dist, edge_dir, exact);
  355. VectorSubtract (p, exact, off);
  356. error = VectorLength (off);
  357. if (fabs(error) > OFF_EPSILON)
  358. continue; // not on the edge
  359. // break the edge
  360. c_tjunctions++;
  361. TestEdge (start, dist, p1, j, k+1);
  362. TestEdge (dist, end, j, p2, k+1);
  363. return;
  364. }
  365. // the edge p1 to p2 is now free of tjunctions
  366. if (numsuperverts >= MAX_SUPERVERTS)
  367. Error ("MAX_SUPERVERTS");
  368. superverts[numsuperverts] = p1;
  369. numsuperverts++;
  370. }
  371. /*
  372. ==================
  373. FixFaceEdges
  374. ==================
  375. */
  376. void FixFaceEdges (node_t *node, face_t *f)
  377. {
  378. int p1, p2;
  379. int i;
  380. vec3_t e2;
  381. vec_t len;
  382. int count[MAX_SUPERVERTS], start[MAX_SUPERVERTS];
  383. int base;
  384. if (f->merged || f->split[0] || f->split[1])
  385. return;
  386. numsuperverts = 0;
  387. for (i=0 ; i<f->numpoints ; i++)
  388. {
  389. p1 = f->vertexnums[i];
  390. p2 = f->vertexnums[(i+1)%f->numpoints];
  391. VectorCopy (dvertexes[p1].point, edge_start);
  392. VectorCopy (dvertexes[p2].point, e2);
  393. FindEdgeVerts (edge_start, e2);
  394. VectorSubtract (e2, edge_start, edge_dir);
  395. len = VectorNormalize(edge_dir);
  396. start[i] = numsuperverts;
  397. TestEdge (0, len, p1, p2, 0);
  398. count[i] = numsuperverts - start[i];
  399. }
  400. if (numsuperverts < 3)
  401. { // entire face collapsed
  402. f->numpoints = 0;
  403. c_facecollapse++;
  404. return;
  405. }
  406. // we want to pick a vertex that doesn't have tjunctions
  407. // on either side, which can cause artifacts on trifans,
  408. // especially underwater
  409. for (i=0 ; i<f->numpoints ; i++)
  410. {
  411. if (count[i] == 1 && count[(i+f->numpoints-1)%f->numpoints] == 1)
  412. break;
  413. }
  414. if (i == f->numpoints)
  415. {
  416. f->badstartvert = true;
  417. c_badstartverts++;
  418. base = 0;
  419. }
  420. else
  421. { // rotate the vertex order
  422. base = start[i];
  423. }
  424. // this may fragment the face if > MAXEDGES
  425. FaceFromSuperverts (node, f, base);
  426. }
  427. /*
  428. ==================
  429. FixEdges_r
  430. ==================
  431. */
  432. void FixEdges_r (node_t *node)
  433. {
  434. int i;
  435. face_t *f;
  436. if (node->planenum == PLANENUM_LEAF)
  437. return;
  438. for (f=node->faces ; f ; f=f->next)
  439. FixFaceEdges (node, f);
  440. for (i=0 ; i<2 ; i++)
  441. FixEdges_r (node->children[i]);
  442. }
  443. /*
  444. ===========
  445. FixTjuncs
  446. ===========
  447. */
  448. void FixTjuncs (node_t *headnode)
  449. {
  450. // snap and merge all vertexes
  451. qprintf ("---- snap verts ----\n");
  452. memset (hashverts, 0, sizeof(hashverts));
  453. c_totalverts = 0;
  454. c_uniqueverts = 0;
  455. c_faceoverflows = 0;
  456. EmitVertexes_r (headnode);
  457. qprintf ("%i unique from %i\n", c_uniqueverts, c_totalverts);
  458. // break edges on tjunctions
  459. qprintf ("---- tjunc ----\n");
  460. c_tryedges = 0;
  461. c_degenerate = 0;
  462. c_facecollapse = 0;
  463. c_tjunctions = 0;
  464. if (!notjunc)
  465. FixEdges_r (headnode);
  466. qprintf ("%5i edges degenerated\n", c_degenerate);
  467. qprintf ("%5i faces degenerated\n", c_facecollapse);
  468. qprintf ("%5i edges added by tjunctions\n", c_tjunctions);
  469. qprintf ("%5i faces added by tjunctions\n", c_faceoverflows);
  470. qprintf ("%5i bad start verts\n", c_badstartverts);
  471. }
  472. //========================================================
  473. int c_faces;
  474. face_t *AllocFace (void)
  475. {
  476. face_t *f;
  477. f = GetMemory(sizeof(*f));
  478. memset (f, 0, sizeof(*f));
  479. c_faces++;
  480. return f;
  481. }
  482. face_t *NewFaceFromFace (face_t *f)
  483. {
  484. face_t *newf;
  485. newf = AllocFace ();
  486. *newf = *f;
  487. newf->merged = NULL;
  488. newf->split[0] = newf->split[1] = NULL;
  489. newf->w = NULL;
  490. return newf;
  491. }
  492. void FreeFace (face_t *f)
  493. {
  494. if (f->w)
  495. FreeWinding (f->w);
  496. FreeMemory(f);
  497. c_faces--;
  498. }
  499. //========================================================
  500. /*
  501. ==================
  502. GetEdge
  503. Called by writebsp.
  504. Don't allow four way edges
  505. ==================
  506. */
  507. int GetEdge2 (int v1, int v2, face_t *f)
  508. {
  509. dedge_t *edge;
  510. int i;
  511. c_tryedges++;
  512. if (!noshare)
  513. {
  514. for (i=firstmodeledge ; i < numedges ; i++)
  515. {
  516. edge = &dedges[i];
  517. if (v1 == edge->v[1] && v2 == edge->v[0]
  518. && edgefaces[i][0]->contents == f->contents)
  519. {
  520. if (edgefaces[i][1])
  521. // printf ("WARNING: multiple backward edge\n");
  522. continue;
  523. edgefaces[i][1] = f;
  524. return -i;
  525. }
  526. #if 0
  527. if (v1 == edge->v[0] && v2 == edge->v[1])
  528. {
  529. printf ("WARNING: multiple forward edge\n");
  530. return i;
  531. }
  532. #endif
  533. }
  534. }
  535. // emit an edge
  536. if (numedges >= MAX_MAP_EDGES)
  537. Error ("numedges == MAX_MAP_EDGES");
  538. edge = &dedges[numedges];
  539. numedges++;
  540. edge->v[0] = v1;
  541. edge->v[1] = v2;
  542. edgefaces[numedges-1][0] = f;
  543. return numedges-1;
  544. }
  545. /*
  546. ===========================================================================
  547. FACE MERGING
  548. ===========================================================================
  549. */
  550. /*
  551. =============
  552. TryMerge
  553. If two polygons share a common edge and the edges that meet at the
  554. common points are both inside the other polygons, merge them
  555. Returns NULL if the faces couldn't be merged, or the new face.
  556. The originals will NOT be freed.
  557. =============
  558. */
  559. face_t *TryMerge (face_t *f1, face_t *f2, vec3_t planenormal)
  560. {
  561. face_t *newf;
  562. winding_t *nw;
  563. if (!f1->w || !f2->w)
  564. return NULL;
  565. if (f1->texinfo != f2->texinfo)
  566. return NULL;
  567. if (f1->planenum != f2->planenum) // on front and back sides
  568. return NULL;
  569. if (f1->contents != f2->contents)
  570. return NULL;
  571. nw = TryMergeWinding (f1->w, f2->w, planenormal);
  572. if (!nw)
  573. return NULL;
  574. c_merge++;
  575. newf = NewFaceFromFace (f1);
  576. newf->w = nw;
  577. f1->merged = newf;
  578. f2->merged = newf;
  579. return newf;
  580. }
  581. /*
  582. ===============
  583. MergeNodeFaces
  584. ===============
  585. */
  586. void MergeNodeFaces (node_t *node)
  587. {
  588. face_t *f1, *f2, *end;
  589. face_t *merged;
  590. plane_t *plane;
  591. plane = &mapplanes[node->planenum];
  592. merged = NULL;
  593. for (f1 = node->faces ; f1 ; f1 = f1->next)
  594. {
  595. if (f1->merged || f1->split[0] || f1->split[1])
  596. continue;
  597. for (f2 = node->faces ; f2 != f1 ; f2=f2->next)
  598. {
  599. if (f2->merged || f2->split[0] || f2->split[1])
  600. continue;
  601. //IDBUG: always passes the face's node's normal to TryMerge()
  602. //regardless of which side the face is on. Approximately 50% of
  603. //the time the face will be on the other side of node, and thus
  604. //the result of the convex/concave test in TryMergeWinding(),
  605. //which depends on the normal, is flipped. This causes faces
  606. //that shouldn't be merged to be merged and faces that
  607. //should be merged to not be merged.
  608. //the following added line fixes this bug
  609. //thanks to: Alexander Malmberg <alexander@malmberg.org>
  610. plane = &mapplanes[f1->planenum];
  611. //
  612. merged = TryMerge (f1, f2, plane->normal);
  613. if (!merged)
  614. continue;
  615. // add merged to the end of the node face list
  616. // so it will be checked against all the faces again
  617. for (end = node->faces ; end->next ; end = end->next)
  618. ;
  619. merged->next = NULL;
  620. end->next = merged;
  621. break;
  622. }
  623. }
  624. }
  625. //=====================================================================
  626. /*
  627. ===============
  628. SubdivideFace
  629. Chop up faces that are larger than we want in the surface cache
  630. ===============
  631. */
  632. void SubdivideFace (node_t *node, face_t *f)
  633. {
  634. float mins, maxs;
  635. vec_t v;
  636. int axis, i;
  637. texinfo_t *tex;
  638. vec3_t temp;
  639. vec_t dist;
  640. winding_t *w, *frontw, *backw;
  641. if (f->merged)
  642. return;
  643. // special (non-surface cached) faces don't need subdivision
  644. tex = &texinfo[f->texinfo];
  645. if ( tex->flags & (SURF_WARP|SURF_SKY) )
  646. {
  647. return;
  648. }
  649. for (axis = 0 ; axis < 2 ; axis++)
  650. {
  651. while (1)
  652. {
  653. mins = 999999;
  654. maxs = -999999;
  655. VectorCopy (tex->vecs[axis], temp);
  656. w = f->w;
  657. for (i=0 ; i<w->numpoints ; i++)
  658. {
  659. v = DotProduct (w->p[i], temp);
  660. if (v < mins)
  661. mins = v;
  662. if (v > maxs)
  663. maxs = v;
  664. }
  665. #if 0
  666. if (maxs - mins <= 0)
  667. Error ("zero extents");
  668. #endif
  669. if (axis == 2)
  670. { // allow double high walls
  671. if (maxs - mins <= subdivide_size/* *2 */)
  672. break;
  673. }
  674. else if (maxs - mins <= subdivide_size)
  675. break;
  676. // split it
  677. c_subdivide++;
  678. v = VectorNormalize (temp);
  679. dist = (mins + subdivide_size - 16)/v;
  680. ClipWindingEpsilon (w, temp, dist, ON_EPSILON, &frontw, &backw);
  681. if (!frontw || !backw)
  682. Error ("SubdivideFace: didn't split the polygon");
  683. f->split[0] = NewFaceFromFace (f);
  684. f->split[0]->w = frontw;
  685. f->split[0]->next = node->faces;
  686. node->faces = f->split[0];
  687. f->split[1] = NewFaceFromFace (f);
  688. f->split[1]->w = backw;
  689. f->split[1]->next = node->faces;
  690. node->faces = f->split[1];
  691. SubdivideFace (node, f->split[0]);
  692. SubdivideFace (node, f->split[1]);
  693. return;
  694. }
  695. }
  696. }
  697. void SubdivideNodeFaces (node_t *node)
  698. {
  699. face_t *f;
  700. for (f = node->faces ; f ; f=f->next)
  701. {
  702. SubdivideFace (node, f);
  703. }
  704. }
  705. //===========================================================================
  706. int c_nodefaces;
  707. /*
  708. ============
  709. FaceFromPortal
  710. ============
  711. */
  712. face_t *FaceFromPortal (portal_t *p, int pside)
  713. {
  714. face_t *f;
  715. side_t *side;
  716. side = p->side;
  717. if (!side)
  718. return NULL; // portal does not bridge different visible contents
  719. f = AllocFace ();
  720. f->texinfo = side->texinfo;
  721. f->planenum = (side->planenum & ~1) | pside;
  722. f->portal = p;
  723. if ( (p->nodes[pside]->contents & CONTENTS_WINDOW)
  724. && VisibleContents(p->nodes[!pside]->contents^p->nodes[pside]->contents) == CONTENTS_WINDOW )
  725. return NULL; // don't show insides of windows
  726. if (pside)
  727. {
  728. f->w = ReverseWinding(p->winding);
  729. f->contents = p->nodes[1]->contents;
  730. }
  731. else
  732. {
  733. f->w = CopyWinding(p->winding);
  734. f->contents = p->nodes[0]->contents;
  735. }
  736. return f;
  737. }
  738. /*
  739. ===============
  740. MakeFaces_r
  741. If a portal will make a visible face,
  742. mark the side that originally created it
  743. solid / empty : solid
  744. solid / water : solid
  745. water / empty : water
  746. water / water : none
  747. ===============
  748. */
  749. void MakeFaces_r (node_t *node)
  750. {
  751. portal_t *p;
  752. int s;
  753. // recurse down to leafs
  754. if (node->planenum != PLANENUM_LEAF)
  755. {
  756. MakeFaces_r (node->children[0]);
  757. MakeFaces_r (node->children[1]);
  758. // merge together all visible faces on the node
  759. if (!nomerge)
  760. MergeNodeFaces (node);
  761. if (!nosubdiv)
  762. SubdivideNodeFaces (node);
  763. return;
  764. }
  765. // solid leafs never have visible faces
  766. if (node->contents & CONTENTS_SOLID)
  767. return;
  768. // see which portals are valid
  769. for (p=node->portals ; p ; p = p->next[s])
  770. {
  771. s = (p->nodes[1] == node);
  772. p->face[s] = FaceFromPortal (p, s);
  773. if (p->face[s])
  774. {
  775. c_nodefaces++;
  776. p->face[s]->next = p->onnode->faces;
  777. p->onnode->faces = p->face[s];
  778. }
  779. }
  780. }
  781. /*
  782. ============
  783. MakeFaces
  784. ============
  785. */
  786. void MakeFaces (node_t *node)
  787. {
  788. qprintf ("--- MakeFaces ---\n");
  789. c_merge = 0;
  790. c_subdivide = 0;
  791. c_nodefaces = 0;
  792. MakeFaces_r (node);
  793. qprintf ("%5i makefaces\n", c_nodefaces);
  794. qprintf ("%5i merged\n", c_merge);
  795. qprintf ("%5i subdivided\n", c_subdivide);
  796. }