SURFACES.C 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513
  1. // divide.h
  2. #include "bsp5.h"
  3. surface_t newcopy_t;
  4. /*
  5. a surface has all of the faces that could be drawn on a given plane
  6. the outside filling stage can remove some of them so a better bsp can be generated
  7. */
  8. int subdivides;
  9. /*
  10. ===============
  11. SubdivideFace
  12. If the face is >256 in either texture direction, carve a valid sized
  13. piece off and insert the remainder in the next link
  14. ===============
  15. */
  16. void SubdivideFace (face_t *f, face_t **prevptr)
  17. {
  18. float mins, maxs;
  19. vec_t v;
  20. int axis, i;
  21. plane_t plane;
  22. face_t *front, *back, *next;
  23. texinfo_t *tex;
  24. // special (non-surface cached) faces don't need subdivision
  25. tex = &texinfo[f->texturenum];
  26. if ( tex->flags & TEX_SPECIAL)
  27. return;
  28. for (axis = 0 ; axis < 2 ; axis++)
  29. {
  30. while (1)
  31. {
  32. mins = 9999;
  33. maxs = -9999;
  34. for (i=0 ; i<f->numpoints ; i++)
  35. {
  36. v = DotProduct (f->pts[i], tex->vecs[axis]);
  37. if (v < mins)
  38. mins = v;
  39. if (v > maxs)
  40. maxs = v;
  41. }
  42. if (maxs - mins <= subdivide_size)
  43. break;
  44. // split it
  45. subdivides++;
  46. VectorCopy (tex->vecs[axis], plane.normal);
  47. v = VectorLength (plane.normal);
  48. VectorNormalize (plane.normal);
  49. plane.dist = (mins + subdivide_size - 16)/v;
  50. next = f->next;
  51. SplitFace (f, &plane, &front, &back);
  52. if (!front || !back)
  53. Error ("SubdivideFace: didn't split the polygon");
  54. *prevptr = back;
  55. back->next = front;
  56. front->next = next;
  57. f = back;
  58. }
  59. }
  60. }
  61. /*
  62. ================
  63. SubdivideFaces
  64. ================
  65. */
  66. void SubdivideFaces (surface_t *surfhead)
  67. {
  68. surface_t *surf;
  69. face_t *f , **prevptr;
  70. qprintf ("--- SubdivideFaces ---\n");
  71. subdivides = 0;
  72. for (surf = surfhead ; surf ; surf=surf->next)
  73. {
  74. prevptr = &surf->faces;
  75. while (1)
  76. {
  77. f = *prevptr;
  78. if (!f)
  79. break;
  80. SubdivideFace (f, prevptr);
  81. f = *prevptr;
  82. prevptr = &f->next;
  83. }
  84. }
  85. qprintf ("%i faces added by subdivision\n", subdivides);
  86. }
  87. /*
  88. =============================================================================
  89. GatherNodeFaces
  90. Frees the current node tree and returns a new chain of the surfaces that
  91. have inside faces.
  92. =============================================================================
  93. */
  94. void GatherNodeFaces_r (node_t *node)
  95. {
  96. face_t *f, *next;
  97. if (node->planenum != PLANENUM_LEAF)
  98. {
  99. //
  100. // decision node
  101. //
  102. for (f=node->faces ; f ; f=next)
  103. {
  104. next = f->next;
  105. if (!f->numpoints)
  106. { // face was removed outside
  107. FreeFace (f);
  108. }
  109. else
  110. {
  111. f->next = validfaces[f->planenum];
  112. validfaces[f->planenum] = f;
  113. }
  114. }
  115. GatherNodeFaces_r (node->children[0]);
  116. GatherNodeFaces_r (node->children[1]);
  117. free (node);
  118. }
  119. else
  120. {
  121. //
  122. // leaf node
  123. //
  124. free (node);
  125. }
  126. }
  127. /*
  128. ================
  129. GatherNodeFaces
  130. ================
  131. */
  132. surface_t *GatherNodeFaces (node_t *headnode)
  133. {
  134. memset (validfaces, 0, sizeof(validfaces));
  135. GatherNodeFaces_r (headnode);
  136. return BuildSurfaces ();
  137. }
  138. //===========================================================================
  139. typedef struct hashvert_s
  140. {
  141. struct hashvert_s *next;
  142. vec3_t point;
  143. int num;
  144. int numplanes; // for corner determination
  145. int planenums[2];
  146. int numedges;
  147. } hashvert_t;
  148. #define POINT_EPSILON 0.01
  149. int c_cornerverts;
  150. hashvert_t hvertex[MAX_MAP_VERTS];
  151. hashvert_t *hvert_p;
  152. face_t *edgefaces[MAX_MAP_EDGES][2];
  153. int firstmodeledge = 1;
  154. int firstmodelface;
  155. //============================================================================
  156. #define NUM_HASH 4096
  157. hashvert_t *hashverts[NUM_HASH];
  158. static vec3_t hash_min, hash_scale;
  159. static void InitHash (void)
  160. {
  161. vec3_t size;
  162. vec_t volume;
  163. vec_t scale;
  164. int newsize[2];
  165. int i;
  166. memset (hashverts, 0, sizeof(hashverts));
  167. for (i=0 ; i<3 ; i++)
  168. {
  169. hash_min[i] = -8000;
  170. size[i] = 16000;
  171. }
  172. volume = size[0]*size[1];
  173. scale = sqrt(volume / NUM_HASH);
  174. newsize[0] = size[0] / scale;
  175. newsize[1] = size[1] / scale;
  176. hash_scale[0] = newsize[0] / size[0];
  177. hash_scale[1] = newsize[1] / size[1];
  178. hash_scale[2] = newsize[1];
  179. hvert_p = hvertex;
  180. }
  181. static unsigned HashVec (vec3_t vec)
  182. {
  183. unsigned h;
  184. h = hash_scale[0] * (vec[0] - hash_min[0]) * hash_scale[2]
  185. + hash_scale[1] * (vec[1] - hash_min[1]);
  186. if ( h >= NUM_HASH)
  187. return NUM_HASH - 1;
  188. return h;
  189. }
  190. /*
  191. =============
  192. GetVertex
  193. =============
  194. */
  195. int GetVertex (vec3_t in, int planenum)
  196. {
  197. int h;
  198. int i;
  199. hashvert_t *hv;
  200. vec3_t vert;
  201. for (i=0 ; i<3 ; i++)
  202. {
  203. if ( fabs(in[i] - Q_rint(in[i])) < 0.001)
  204. vert[i] = Q_rint(in[i]);
  205. else
  206. vert[i] = in[i];
  207. }
  208. h = HashVec (vert);
  209. for (hv=hashverts[h] ; hv ; hv=hv->next)
  210. {
  211. if ( fabs(hv->point[0]-vert[0])<POINT_EPSILON
  212. && fabs(hv->point[1]-vert[1])<POINT_EPSILON
  213. && fabs(hv->point[2]-vert[2])<POINT_EPSILON )
  214. {
  215. hv->numedges++;
  216. if (hv->numplanes == 3)
  217. return hv->num; // allready known to be a corner
  218. for (i=0 ; i<hv->numplanes ; i++)
  219. if (hv->planenums[i] == planenum)
  220. return hv->num; // allready know this plane
  221. if (hv->numplanes == 2)
  222. c_cornerverts++;
  223. else
  224. hv->planenums[hv->numplanes] = planenum;
  225. hv->numplanes++;
  226. return hv->num;
  227. }
  228. }
  229. hv = hvert_p;
  230. hv->numedges = 1;
  231. hv->numplanes = 1;
  232. hv->planenums[0] = planenum;
  233. hv->next = hashverts[h];
  234. hashverts[h] = hv;
  235. VectorCopy (vert, hv->point);
  236. hv->num = numvertexes;
  237. if (hv->num==MAX_MAP_VERTS)
  238. Error ("GetVertex: MAX_MAP_VERTS");
  239. hvert_p++;
  240. // emit a vertex
  241. if (numvertexes == MAX_MAP_VERTS)
  242. Error ("numvertexes == MAX_MAP_VERTS");
  243. dvertexes[numvertexes].point[0] = vert[0];
  244. dvertexes[numvertexes].point[1] = vert[1];
  245. dvertexes[numvertexes].point[2] = vert[2];
  246. numvertexes++;
  247. return hv->num;
  248. }
  249. //===========================================================================
  250. /*
  251. ==================
  252. GetEdge
  253. Don't allow four way edges
  254. ==================
  255. */
  256. int c_tryedges;
  257. int GetEdge (vec3_t p1, vec3_t p2, face_t *f)
  258. {
  259. int v1, v2;
  260. dedge_t *edge;
  261. int i;
  262. if (!f->contents[0])
  263. Error ("GetEdge: 0 contents");
  264. c_tryedges++;
  265. v1 = GetVertex (p1, f->planenum);
  266. v2 = GetVertex (p2, f->planenum);
  267. for (i=firstmodeledge ; i < numedges ; i++)
  268. {
  269. edge = &dedges[i];
  270. if (v1 == edge->v[1] && v2 == edge->v[0]
  271. && !edgefaces[i][1]
  272. && edgefaces[i][0]->contents[0] == f->contents[0])
  273. {
  274. edgefaces[i][1] = f;
  275. return -i;
  276. }
  277. }
  278. // emit an edge
  279. if (numedges == MAX_MAP_EDGES)
  280. Error ("numedges == MAX_MAP_EDGES");
  281. edge = &dedges[numedges];
  282. numedges++;
  283. edge->v[0] = v1;
  284. edge->v[1] = v2;
  285. edgefaces[i][0] = f;
  286. return i;
  287. }
  288. /*
  289. ==================
  290. FindFaceEdges
  291. ==================
  292. */
  293. void FindFaceEdges (face_t *face)
  294. {
  295. int i;
  296. face->outputnumber = -1;
  297. if (face->numpoints > MAXEDGES)
  298. Error ("WriteFace: %i points", face->numpoints);
  299. for (i=0; i<face->numpoints ; i++)
  300. face->edges[i] = GetEdge
  301. (face->pts[i], face->pts[(i+1)%face->numpoints], face);
  302. }
  303. /*
  304. =============
  305. CheckVertexes
  306. // debugging
  307. =============
  308. */
  309. void CheckVertexes (void)
  310. {
  311. int cb, c0, c1, c2, c3;
  312. hashvert_t *hv;
  313. cb = c0 = c1 = c2 = c3 = 0;
  314. for (hv=hvertex ; hv!=hvert_p ; hv++)
  315. {
  316. if (hv->numedges < 0 || hv->numedges & 1)
  317. cb++;
  318. else if (!hv->numedges)
  319. c0++;
  320. else if (hv->numedges == 2)
  321. c1++;
  322. else if (hv->numedges == 4)
  323. c2++;
  324. else
  325. c3++;
  326. }
  327. qprintf ("%5i bad edge points\n", cb);
  328. qprintf ("%5i 0 edge points\n", c0);
  329. qprintf ("%5i 2 edge points\n", c1);
  330. qprintf ("%5i 4 edge points\n", c2);
  331. qprintf ("%5i 6+ edge points\n", c3);
  332. }
  333. /*
  334. =============
  335. CheckEdges
  336. // debugging
  337. =============
  338. */
  339. void CheckEdges (void)
  340. {
  341. dedge_t *edge;
  342. int i;
  343. dvertex_t *d1, *d2;
  344. face_t *f1, *f2;
  345. int c_nonconvex;
  346. int c_multitexture;
  347. c_nonconvex = c_multitexture = 0;
  348. // CheckVertexes ();
  349. for (i=1 ; i < numedges ; i++)
  350. {
  351. edge = &dedges[i];
  352. if (!edgefaces[i][1])
  353. {
  354. d1 = &dvertexes[edge->v[0]];
  355. d2 = &dvertexes[edge->v[1]];
  356. qprintf ("unshared edge at: (%8.2f, %8.2f, %8.2f) (%8.2f, %8.2f, %8.2f)\n",d1->point[0], d1->point[1], d1->point[2], d2->point[0], d2->point[1], d2->point[2]);
  357. }
  358. else
  359. {
  360. f1 = edgefaces[i][0];
  361. f2 = edgefaces[i][1];
  362. if (f1->planeside != f2->planeside)
  363. continue;
  364. if (f1->planenum != f2->planenum)
  365. continue;
  366. // on the same plane, might be discardable
  367. if (f1->texturenum == f2->texturenum)
  368. {
  369. hvertex[edge->v[0]].numedges-=2;
  370. hvertex[edge->v[1]].numedges-=2;
  371. c_nonconvex++;
  372. }
  373. else
  374. c_multitexture++;
  375. }
  376. }
  377. // qprintf ("%5i edges\n", i);
  378. // qprintf ("%5i c_nonconvex\n", c_nonconvex);
  379. // qprintf ("%5i c_multitexture\n", c_multitexture);
  380. // CheckVertexes ();
  381. }
  382. /*
  383. ================
  384. MakeFaceEdges_r
  385. ================
  386. */
  387. void MakeFaceEdges_r (node_t *node)
  388. {
  389. face_t *f;
  390. if (node->planenum == PLANENUM_LEAF)
  391. return;
  392. for (f=node->faces ; f ; f=f->next)
  393. FindFaceEdges (f);
  394. MakeFaceEdges_r (node->children[0]);
  395. MakeFaceEdges_r (node->children[1]);
  396. }
  397. /*
  398. ================
  399. MakeFaceEdges
  400. ================
  401. */
  402. void MakeFaceEdges (node_t *headnode)
  403. {
  404. qprintf ("----- MakeFaceEdges -----\n");
  405. InitHash ();
  406. c_tryedges = 0;
  407. c_cornerverts = 0;
  408. MakeFaceEdges_r (headnode);
  409. // CheckEdges ();
  410. GrowNodeRegions (headnode);
  411. firstmodeledge = numedges;
  412. firstmodelface = numfaces;
  413. }