faces.c 20 KB

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