PageRenderTime 49ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/portals.c

https://gitlab.com/madscientist159/darkplaces
C | 480 lines | 413 code | 47 blank | 20 comment | 88 complexity | 651d7d8630330a99119cb90b42749a1c MD5 | raw file
  1. #include "quakedef.h"
  2. #include "polygon.h"
  3. #include "portals.h"
  4. #define MAXRECURSIVEPORTALPLANES 1024
  5. #define MAXRECURSIVEPORTALS 256
  6. static tinyplane_t portalplanes[MAXRECURSIVEPORTALPLANES];
  7. static int ranoutofportalplanes;
  8. static int ranoutofportals;
  9. static float portaltemppoints[2][256][3];
  10. static float portaltemppoints2[256][3];
  11. static int portal_markid = 0;
  12. static float boxpoints[4*3];
  13. static int Portal_PortalThroughPortalPlanes(tinyplane_t *clipplanes, int clipnumplanes, float *targpoints, int targnumpoints, float *out, int maxpoints)
  14. {
  15. int numpoints = targnumpoints, i, w;
  16. if (numpoints < 1)
  17. return numpoints;
  18. if (maxpoints > 256)
  19. maxpoints = 256;
  20. w = 0;
  21. memcpy(&portaltemppoints[w][0][0], targpoints, numpoints * 3 * sizeof(float));
  22. for (i = 0;i < clipnumplanes && numpoints > 0;i++)
  23. {
  24. PolygonF_Divide(numpoints, &portaltemppoints[w][0][0], clipplanes[i].normal[0], clipplanes[i].normal[1], clipplanes[i].normal[2], clipplanes[i].dist, 1.0f/32.0f, 256, &portaltemppoints[1-w][0][0], &numpoints, 0, NULL, NULL, NULL);
  25. w = 1-w;
  26. numpoints = min(numpoints, 256);
  27. }
  28. numpoints = min(numpoints, maxpoints);
  29. if (numpoints > 0)
  30. memcpy(out, &portaltemppoints[w][0][0], numpoints * 3 * sizeof(float));
  31. return numpoints;
  32. }
  33. static int Portal_RecursiveFlowSearch (mleaf_t *leaf, vec3_t eye, int firstclipplane, int numclipplanes)
  34. {
  35. mportal_t *p;
  36. int newpoints, i, prev;
  37. vec3_t center, v1, v2;
  38. tinyplane_t *newplanes;
  39. if (leaf->portalmarkid == portal_markid)
  40. return true;
  41. // follow portals into other leafs
  42. for (p = leaf->portals;p;p = p->next)
  43. {
  44. // only flow through portals facing away from the viewer
  45. if (PlaneDiff(eye, (&p->plane)) < 0)
  46. {
  47. newpoints = Portal_PortalThroughPortalPlanes(&portalplanes[firstclipplane], numclipplanes, (float *) p->points, p->numpoints, &portaltemppoints2[0][0], 256);
  48. if (newpoints < 3)
  49. continue;
  50. else if (firstclipplane + numclipplanes + newpoints > MAXRECURSIVEPORTALPLANES)
  51. ranoutofportalplanes = true;
  52. else
  53. {
  54. // find the center by averaging
  55. VectorClear(center);
  56. for (i = 0;i < newpoints;i++)
  57. VectorAdd(center, portaltemppoints2[i], center);
  58. // ixtable is a 1.0f / N table
  59. VectorScale(center, ixtable[newpoints], center);
  60. // calculate the planes, and make sure the polygon can see it's own center
  61. newplanes = &portalplanes[firstclipplane + numclipplanes];
  62. for (prev = newpoints - 1, i = 0;i < newpoints;prev = i, i++)
  63. {
  64. VectorSubtract(eye, portaltemppoints2[i], v1);
  65. VectorSubtract(portaltemppoints2[prev], portaltemppoints2[i], v2);
  66. CrossProduct(v1, v2, newplanes[i].normal);
  67. VectorNormalize(newplanes[i].normal);
  68. newplanes[i].dist = DotProduct(eye, newplanes[i].normal);
  69. if (DotProduct(newplanes[i].normal, center) <= newplanes[i].dist)
  70. {
  71. // polygon can't see it's own center, discard and use parent portal
  72. break;
  73. }
  74. }
  75. if (i == newpoints)
  76. {
  77. if (Portal_RecursiveFlowSearch(p->past, eye, firstclipplane + numclipplanes, newpoints))
  78. return true;
  79. }
  80. else
  81. {
  82. if (Portal_RecursiveFlowSearch(p->past, eye, firstclipplane, numclipplanes))
  83. return true;
  84. }
  85. }
  86. }
  87. }
  88. return false;
  89. }
  90. static void Portal_PolygonRecursiveMarkLeafs(mnode_t *node, float *polypoints, int numpoints)
  91. {
  92. int i, front;
  93. float *p;
  94. loc0:
  95. if (!node->plane)
  96. {
  97. ((mleaf_t *)node)->portalmarkid = portal_markid;
  98. return;
  99. }
  100. front = 0;
  101. for (i = 0, p = polypoints;i < numpoints;i++, p += 3)
  102. {
  103. if (DotProduct(p, node->plane->normal) > node->plane->dist)
  104. front++;
  105. }
  106. if (front > 0)
  107. {
  108. if (front == numpoints)
  109. {
  110. node = node->children[0];
  111. goto loc0;
  112. }
  113. else
  114. Portal_PolygonRecursiveMarkLeafs(node->children[0], polypoints, numpoints);
  115. }
  116. node = node->children[1];
  117. goto loc0;
  118. }
  119. int Portal_CheckPolygon(dp_model_t *model, vec3_t eye, float *polypoints, int numpoints)
  120. {
  121. int i, prev, returnvalue;
  122. mleaf_t *eyeleaf;
  123. vec3_t center, v1, v2;
  124. // if there is no model, it can not block visibility
  125. if (model == NULL || !model->brush.PointInLeaf)
  126. return true;
  127. portal_markid++;
  128. Portal_PolygonRecursiveMarkLeafs(model->brush.data_nodes, polypoints, numpoints);
  129. eyeleaf = model->brush.PointInLeaf(model, eye);
  130. // find the center by averaging
  131. VectorClear(center);
  132. for (i = 0;i < numpoints;i++)
  133. VectorAdd(center, (&polypoints[i * 3]), center);
  134. // ixtable is a 1.0f / N table
  135. VectorScale(center, ixtable[numpoints], center);
  136. // calculate the planes, and make sure the polygon can see it's own center
  137. for (prev = numpoints - 1, i = 0;i < numpoints;prev = i, i++)
  138. {
  139. VectorSubtract(eye, (&polypoints[i * 3]), v1);
  140. VectorSubtract((&polypoints[prev * 3]), (&polypoints[i * 3]), v2);
  141. CrossProduct(v1, v2, portalplanes[i].normal);
  142. VectorNormalize(portalplanes[i].normal);
  143. portalplanes[i].dist = DotProduct(eye, portalplanes[i].normal);
  144. if (DotProduct(portalplanes[i].normal, center) <= portalplanes[i].dist)
  145. {
  146. // polygon can't see it's own center, discard
  147. return false;
  148. }
  149. }
  150. ranoutofportalplanes = false;
  151. ranoutofportals = false;
  152. returnvalue = Portal_RecursiveFlowSearch(eyeleaf, eye, 0, numpoints);
  153. if (ranoutofportalplanes)
  154. Con_Printf("Portal_RecursiveFlowSearch: ran out of %d plane stack when recursing through portals\n", MAXRECURSIVEPORTALPLANES);
  155. if (ranoutofportals)
  156. Con_Printf("Portal_RecursiveFlowSearch: ran out of %d portal stack when recursing through portals\n", MAXRECURSIVEPORTALS);
  157. return returnvalue;
  158. }
  159. #define Portal_MinsBoxPolygon(axis, axisvalue, x1, y1, z1, x2, y2, z2, x3, y3, z3, x4, y4, z4) \
  160. {\
  161. if (eye[(axis)] < ((axisvalue) - 0.5f))\
  162. {\
  163. boxpoints[ 0] = x1;boxpoints[ 1] = y1;boxpoints[ 2] = z1;\
  164. boxpoints[ 3] = x2;boxpoints[ 4] = y2;boxpoints[ 5] = z2;\
  165. boxpoints[ 6] = x3;boxpoints[ 7] = y3;boxpoints[ 8] = z3;\
  166. boxpoints[ 9] = x4;boxpoints[10] = y4;boxpoints[11] = z4;\
  167. if (Portal_CheckPolygon(model, eye, boxpoints, 4))\
  168. return true;\
  169. }\
  170. }
  171. #define Portal_MaxsBoxPolygon(axis, axisvalue, x1, y1, z1, x2, y2, z2, x3, y3, z3, x4, y4, z4) \
  172. {\
  173. if (eye[(axis)] > ((axisvalue) + 0.5f))\
  174. {\
  175. boxpoints[ 0] = x1;boxpoints[ 1] = y1;boxpoints[ 2] = z1;\
  176. boxpoints[ 3] = x2;boxpoints[ 4] = y2;boxpoints[ 5] = z2;\
  177. boxpoints[ 6] = x3;boxpoints[ 7] = y3;boxpoints[ 8] = z3;\
  178. boxpoints[ 9] = x4;boxpoints[10] = y4;boxpoints[11] = z4;\
  179. if (Portal_CheckPolygon(model, eye, boxpoints, 4))\
  180. return true;\
  181. }\
  182. }
  183. int Portal_CheckBox(dp_model_t *model, vec3_t eye, vec3_t a, vec3_t b)
  184. {
  185. if (eye[0] >= (a[0] - 1.0f) && eye[0] < (b[0] + 1.0f)
  186. && eye[1] >= (a[1] - 1.0f) && eye[1] < (b[1] + 1.0f)
  187. && eye[2] >= (a[2] - 1.0f) && eye[2] < (b[2] + 1.0f))
  188. return true;
  189. Portal_MinsBoxPolygon
  190. (
  191. 0, a[0],
  192. a[0], a[1], a[2],
  193. a[0], b[1], a[2],
  194. a[0], b[1], b[2],
  195. a[0], a[1], b[2]
  196. );
  197. Portal_MaxsBoxPolygon
  198. (
  199. 0, b[0],
  200. b[0], b[1], a[2],
  201. b[0], a[1], a[2],
  202. b[0], a[1], b[2],
  203. b[0], b[1], b[2]
  204. );
  205. Portal_MinsBoxPolygon
  206. (
  207. 1, a[1],
  208. b[0], a[1], a[2],
  209. a[0], a[1], a[2],
  210. a[0], a[1], b[2],
  211. b[0], a[1], b[2]
  212. );
  213. Portal_MaxsBoxPolygon
  214. (
  215. 1, b[1],
  216. a[0], b[1], a[2],
  217. b[0], b[1], a[2],
  218. b[0], b[1], b[2],
  219. a[0], b[1], b[2]
  220. );
  221. Portal_MinsBoxPolygon
  222. (
  223. 2, a[2],
  224. a[0], a[1], a[2],
  225. b[0], a[1], a[2],
  226. b[0], b[1], a[2],
  227. a[0], b[1], a[2]
  228. );
  229. Portal_MaxsBoxPolygon
  230. (
  231. 2, b[2],
  232. b[0], a[1], b[2],
  233. a[0], a[1], b[2],
  234. a[0], b[1], b[2],
  235. b[0], b[1], b[2]
  236. );
  237. return false;
  238. }
  239. typedef struct portalrecursioninfo_s
  240. {
  241. int exact;
  242. int numfrustumplanes;
  243. vec3_t boxmins;
  244. vec3_t boxmaxs;
  245. int numsurfaces;
  246. int *surfacelist;
  247. unsigned char *surfacepvs;
  248. int numleafs;
  249. unsigned char *visitingleafpvs; // used to prevent infinite loops
  250. int *leaflist;
  251. unsigned char *leafpvs;
  252. unsigned char *shadowtrispvs;
  253. unsigned char *lighttrispvs;
  254. dp_model_t *model;
  255. vec3_t eye;
  256. float *updateleafsmins;
  257. float *updateleafsmaxs;
  258. }
  259. portalrecursioninfo_t;
  260. static void Portal_RecursiveFlow (portalrecursioninfo_t *info, mleaf_t *leaf, int firstclipplane, int numclipplanes)
  261. {
  262. mportal_t *p;
  263. int newpoints, i, prev;
  264. float dist;
  265. vec3_t center;
  266. tinyplane_t *newplanes;
  267. int leafindex = leaf - info->model->brush.data_leafs;
  268. if (CHECKPVSBIT(info->visitingleafpvs, leafindex))
  269. return; // recursive loop of leafs (cmc.bsp for megatf coop)
  270. SETPVSBIT(info->visitingleafpvs, leafindex);
  271. for (i = 0;i < 3;i++)
  272. {
  273. if (info->updateleafsmins && info->updateleafsmins[i] > leaf->mins[i]) info->updateleafsmins[i] = leaf->mins[i];
  274. if (info->updateleafsmaxs && info->updateleafsmaxs[i] < leaf->maxs[i]) info->updateleafsmaxs[i] = leaf->maxs[i];
  275. }
  276. if (info->leafpvs)
  277. {
  278. if (!CHECKPVSBIT(info->leafpvs, leafindex))
  279. {
  280. SETPVSBIT(info->leafpvs, leafindex);
  281. info->leaflist[info->numleafs++] = leafindex;
  282. }
  283. }
  284. // mark surfaces in leaf that can be seen through portal
  285. if (leaf->numleafsurfaces && info->surfacepvs)
  286. {
  287. for (i = 0;i < leaf->numleafsurfaces;i++)
  288. {
  289. int surfaceindex = leaf->firstleafsurface[i];
  290. msurface_t *surface = info->model->data_surfaces + surfaceindex;
  291. if (BoxesOverlap(surface->mins, surface->maxs, info->boxmins, info->boxmaxs))
  292. {
  293. qboolean insidebox = BoxInsideBox(surface->mins, surface->maxs, info->boxmins, info->boxmaxs);
  294. qboolean addedtris = false;
  295. int t, tend;
  296. const int *elements;
  297. const float *vertex3f;
  298. float v[9];
  299. vertex3f = info->model->surfmesh.data_vertex3f;
  300. elements = (info->model->surfmesh.data_element3i + 3 * surface->num_firsttriangle);
  301. for (t = surface->num_firsttriangle, tend = t + surface->num_triangles;t < tend;t++, elements += 3)
  302. {
  303. VectorCopy(vertex3f + elements[0] * 3, v + 0);
  304. VectorCopy(vertex3f + elements[1] * 3, v + 3);
  305. VectorCopy(vertex3f + elements[2] * 3, v + 6);
  306. if (PointInfrontOfTriangle(info->eye, v + 0, v + 3, v + 6)
  307. && (insidebox || TriangleBBoxOverlapsBox(v, v + 3, v + 6, info->boxmins, info->boxmaxs))
  308. && (!info->exact || Portal_PortalThroughPortalPlanes(&portalplanes[firstclipplane], numclipplanes, v, 3, &portaltemppoints2[0][0], 256) > 0))
  309. {
  310. addedtris = true;
  311. if (info->shadowtrispvs)
  312. SETPVSBIT(info->shadowtrispvs, t);
  313. if (info->lighttrispvs)
  314. SETPVSBIT(info->lighttrispvs, t);
  315. }
  316. }
  317. if (addedtris && !CHECKPVSBIT(info->surfacepvs, surfaceindex))
  318. {
  319. SETPVSBIT(info->surfacepvs, surfaceindex);
  320. info->surfacelist[info->numsurfaces++] = surfaceindex;
  321. }
  322. }
  323. }
  324. }
  325. // follow portals into other leafs
  326. for (p = leaf->portals;p;p = p->next)
  327. {
  328. // only flow through portals facing the viewer
  329. dist = PlaneDiff(info->eye, (&p->plane));
  330. if (dist < 0 && BoxesOverlap(p->past->mins, p->past->maxs, info->boxmins, info->boxmaxs))
  331. {
  332. newpoints = Portal_PortalThroughPortalPlanes(&portalplanes[firstclipplane], numclipplanes, (float *) p->points, p->numpoints, &portaltemppoints2[0][0], 256);
  333. if (newpoints < 3)
  334. continue;
  335. else if (firstclipplane + numclipplanes + newpoints > MAXRECURSIVEPORTALPLANES)
  336. ranoutofportalplanes = true;
  337. else
  338. {
  339. // find the center by averaging
  340. VectorClear(center);
  341. for (i = 0;i < newpoints;i++)
  342. VectorAdd(center, portaltemppoints2[i], center);
  343. // ixtable is a 1.0f / N table
  344. VectorScale(center, ixtable[newpoints], center);
  345. // calculate the planes, and make sure the polygon can see its own center
  346. newplanes = &portalplanes[firstclipplane + numclipplanes];
  347. for (prev = newpoints - 1, i = 0;i < newpoints;prev = i, i++)
  348. {
  349. TriangleNormal(portaltemppoints2[prev], portaltemppoints2[i], info->eye, newplanes[i].normal);
  350. VectorNormalize(newplanes[i].normal);
  351. newplanes[i].dist = DotProduct(info->eye, newplanes[i].normal);
  352. if (DotProduct(newplanes[i].normal, center) <= newplanes[i].dist)
  353. {
  354. // polygon can't see its own center, discard and use parent portal
  355. break;
  356. }
  357. }
  358. if (i == newpoints)
  359. Portal_RecursiveFlow(info, p->past, firstclipplane + numclipplanes, newpoints);
  360. else
  361. Portal_RecursiveFlow(info, p->past, firstclipplane, numclipplanes);
  362. }
  363. }
  364. }
  365. CLEARPVSBIT(info->visitingleafpvs, leafindex);
  366. }
  367. static void Portal_RecursiveFindLeafForFlow(portalrecursioninfo_t *info, mnode_t *node)
  368. {
  369. if (node->plane)
  370. {
  371. float f = DotProduct(info->eye, node->plane->normal) - node->plane->dist;
  372. if (f > -0.1)
  373. Portal_RecursiveFindLeafForFlow(info, node->children[0]);
  374. if (f < 0.1)
  375. Portal_RecursiveFindLeafForFlow(info, node->children[1]);
  376. }
  377. else
  378. {
  379. mleaf_t *leaf = (mleaf_t *)node;
  380. if (leaf->clusterindex >= 0)
  381. Portal_RecursiveFlow(info, leaf, 0, info->numfrustumplanes);
  382. }
  383. }
  384. void Portal_Visibility(dp_model_t *model, const vec3_t eye, int *leaflist, unsigned char *leafpvs, int *numleafspointer, int *surfacelist, unsigned char *surfacepvs, int *numsurfacespointer, const mplane_t *frustumplanes, int numfrustumplanes, int exact, const float *boxmins, const float *boxmaxs, float *updateleafsmins, float *updateleafsmaxs, unsigned char *shadowtrispvs, unsigned char *lighttrispvs, unsigned char *visitingleafpvs)
  385. {
  386. int i;
  387. portalrecursioninfo_t info;
  388. // if there is no model, it can not block visibility
  389. if (model == NULL)
  390. {
  391. Con_Print("Portal_Visibility: NULL model\n");
  392. return;
  393. }
  394. if (!model->brush.data_nodes)
  395. {
  396. Con_Print("Portal_Visibility: not a brush model\n");
  397. return;
  398. }
  399. // put frustum planes (if any) into tinyplane format at start of buffer
  400. for (i = 0;i < numfrustumplanes;i++)
  401. {
  402. VectorCopy(frustumplanes[i].normal, portalplanes[i].normal);
  403. portalplanes[i].dist = frustumplanes[i].dist;
  404. }
  405. ranoutofportalplanes = false;
  406. ranoutofportals = false;
  407. VectorCopy(boxmins, info.boxmins);
  408. VectorCopy(boxmaxs, info.boxmaxs);
  409. info.exact = exact;
  410. info.numsurfaces = 0;
  411. info.surfacelist = surfacelist;
  412. info.surfacepvs = surfacepvs;
  413. info.numleafs = 0;
  414. info.visitingleafpvs = visitingleafpvs;
  415. info.leaflist = leaflist;
  416. info.leafpvs = leafpvs;
  417. info.model = model;
  418. VectorCopy(eye, info.eye);
  419. info.numfrustumplanes = numfrustumplanes;
  420. info.updateleafsmins = updateleafsmins;
  421. info.updateleafsmaxs = updateleafsmaxs;
  422. info.shadowtrispvs = shadowtrispvs;
  423. info.lighttrispvs = lighttrispvs;
  424. Portal_RecursiveFindLeafForFlow(&info, model->brush.data_nodes);
  425. if (ranoutofportalplanes)
  426. Con_Printf("Portal_RecursiveFlow: ran out of %d plane stack when recursing through portals\n", MAXRECURSIVEPORTALPLANES);
  427. if (ranoutofportals)
  428. Con_Printf("Portal_RecursiveFlow: ran out of %d portal stack when recursing through portals\n", MAXRECURSIVEPORTALS);
  429. if (numsurfacespointer)
  430. *numsurfacespointer = info.numsurfaces;
  431. if (numleafspointer)
  432. *numleafspointer = info.numleafs;
  433. }