2 #include "quakedef.h"
4 #define MAXRECURSIVEPORTALPLANES 1024
5 #define MAXRECURSIVEPORTALS 256
7 static tinyplane_t portalplanes[MAXRECURSIVEPORTALPLANES];
8 static int ranoutofportalplanes;
9 static int ranoutofportals;
10 static float portaltemppoints[2][256][3];
11 static float portaltemppoints2[256][3];
12 static int portal_markid = 0;
13 static float boxpoints[4*3];
15 int Portal_ClipPolygonToPlane(float *in, float *out, int inpoints, int maxoutpoints, tinyplane_t *p)
16 {
17         int i, outpoints, prevside, side;
18         float *prevpoint, prevdist, dist, dot;
20         if (inpoints < 3)
21                 return inpoints;
22         // begin with the last point, then enter the loop with the first point as current
23         prevpoint = in + 3 * (inpoints - 1);
24         prevdist = DotProduct(prevpoint, p->normal) - p->dist;
25         prevside = prevdist >= 0 ? SIDE_FRONT : SIDE_BACK;
26         i = 0;
27         outpoints = 0;
28         goto begin;
29         for (;i < inpoints;i++)
30         {
31                 prevpoint = in;
32                 prevdist = dist;
33                 prevside = side;
34                 in += 3;
36 begin:
37                 dist = DotProduct(in, p->normal) - p->dist;
38                 side = dist >= 0 ? SIDE_FRONT : SIDE_BACK;
40                 if (prevside == SIDE_FRONT)
41                 {
42                         if (outpoints >= maxoutpoints)
43                                 return -1;
44                         VectorCopy(prevpoint, out);
45                         out += 3;
46                         outpoints++;
47                         if (side == SIDE_FRONT)
48                                 continue;
49                 }
50                 else if (side == SIDE_BACK)
51                         continue;
53                 // generate a split point
54                 if (outpoints >= maxoutpoints)
55                         return -1;
56                 dot = prevdist / (prevdist - dist);
57                 out[0] = prevpoint[0] + dot * (in[0] - prevpoint[0]);
58                 out[1] = prevpoint[1] + dot * (in[1] - prevpoint[1]);
59                 out[2] = prevpoint[2] + dot * (in[2] - prevpoint[2]);
60                 out += 3;
61                 outpoints++;
62         }
64         return outpoints;
65 }
68 int Portal_PortalThroughPortalPlanes(tinyplane_t *clipplanes, int clipnumplanes, float *targpoints, int targnumpoints, float *out, int maxpoints)
69 {
70         int numpoints, i;
71         if (targnumpoints < 3)
72                 return targnumpoints;
73         if (maxpoints < 3)
74                 return -1;
75         numpoints = targnumpoints;
76         memcpy(&portaltemppoints[0][0][0], targpoints, numpoints * 3 * sizeof(float));
77         for (i = 0;i < clipnumplanes;i++)
78         {
79                 numpoints = Portal_ClipPolygonToPlane(&portaltemppoints[0][0][0], &portaltemppoints[1][0][0], numpoints, 256, clipplanes + i);
80                 if (numpoints < 3)
81                         return numpoints;
82                 memcpy(&portaltemppoints[0][0][0], &portaltemppoints[1][0][0], numpoints * 3 * sizeof(float));
83         }
84         if (numpoints > maxpoints)
85                 return -1;
86         memcpy(out, &portaltemppoints[0][0][0], numpoints * 3 * sizeof(float));
87         return numpoints;
88 }
90 int Portal_RecursiveFlowSearch (mleaf_t *leaf, vec3_t eye, int firstclipplane, int numclipplanes)
91 {
92         mportal_t *p;
93         int newpoints, i, prev;
94         vec3_t center, v1, v2;
95         tinyplane_t *newplanes;
97         if (leaf->portalmarkid == portal_markid)
98                 return true;
100         // follow portals into other leafs
101         for (p = leaf->portals;p;p = p->next)
102         {
103                 // only flow through portals facing away from the viewer
104                 if (PlaneDiff(eye, (&p->plane)) < 0)
105                 {
106                         newpoints = Portal_PortalThroughPortalPlanes(&portalplanes[firstclipplane], numclipplanes, (float *) p->points, p->numpoints, &portaltemppoints2[0][0], 256);
107                         if (newpoints < 3)
108                                 continue;
109                         else if (firstclipplane + numclipplanes + newpoints > MAXRECURSIVEPORTALPLANES)
110                                 ranoutofportalplanes = true;
111                         else
112                         {
113                                 // find the center by averaging
114                                 VectorClear(center);
115                                 for (i = 0;i < newpoints;i++)
117                                 // ixtable is a 1.0f / N table
118                                 VectorScale(center, ixtable[newpoints], center);
119                                 // calculate the planes, and make sure the polygon can see it's own center
120                                 newplanes = &portalplanes[firstclipplane + numclipplanes];
121                                 for (prev = newpoints - 1, i = 0;i < newpoints;prev = i, i++)
122                                 {
123                                         VectorSubtract(eye, portaltemppoints2[i], v1);
124                                         VectorSubtract(portaltemppoints2[prev], portaltemppoints2[i], v2);
125                                         CrossProduct(v1, v2, newplanes[i].normal);
126                                         VectorNormalizeFast(newplanes[i].normal);
127                                         newplanes[i].dist = DotProduct(eye, newplanes[i].normal);
128                                         if (DotProduct(newplanes[i].normal, center) <= newplanes[i].dist)
129                                         {
130                                                 // polygon can't see it's own center, discard and use parent portal
131                                                 break;
132                                         }
133                                 }
134                                 if (i == newpoints)
135                                 {
136                                         if (Portal_RecursiveFlowSearch(p->past, eye, firstclipplane + numclipplanes, newpoints))
137                                                 return true;
138                                 }
139                                 else
140                                 {
141                                         if (Portal_RecursiveFlowSearch(p->past, eye, firstclipplane, numclipplanes))
142                                                 return true;
143                                 }
144                         }
145                 }
146         }
148         return false;
149 }
151 void Portal_PolygonRecursiveMarkLeafs(mnode_t *node, float *polypoints, int numpoints)
152 {
153         int i, front;
154         float *p;
156 loc0:
157         if (!node->plane)
158         {
159                 ((mleaf_t *)node)->portalmarkid = portal_markid;
160                 return;
161         }
163         front = 0;
164         for (i = 0, p = polypoints;i < numpoints;i++, p += 3)
165         {
166                 if (DotProduct(p, node->plane->normal) > node->plane->dist)
167                         front++;
168         }
169         if (front > 0)
170         {
171                 if (front == numpoints)
172                 {
173                         node = node->children[0];
174                         goto loc0;
175                 }
176                 else
177                         Portal_PolygonRecursiveMarkLeafs(node->children[0], polypoints, numpoints);
178         }
179         node = node->children[1];
180         goto loc0;
181 }
183 int Portal_CheckPolygon(model_t *model, vec3_t eye, float *polypoints, int numpoints)
184 {
185         int i, prev, returnvalue;
186         mleaf_t *eyeleaf;
187         vec3_t center, v1, v2;
189         // if there is no model, it can not block visibility
190         if (model == NULL || !model->brush.PointInLeaf)
191                 return true;
193         portal_markid++;
196         Portal_PolygonRecursiveMarkLeafs(model->brush.data_nodes, polypoints, numpoints);
198         eyeleaf = model->brush.PointInLeaf(model, eye);
200         // find the center by averaging
201         VectorClear(center);
202         for (i = 0;i < numpoints;i++)
203                 VectorAdd(center, (&polypoints[i * 3]), center);
204         // ixtable is a 1.0f / N table
205         VectorScale(center, ixtable[numpoints], center);
207         // calculate the planes, and make sure the polygon can see it's own center
208         for (prev = numpoints - 1, i = 0;i < numpoints;prev = i, i++)
209         {
210                 VectorSubtract(eye, (&polypoints[i * 3]), v1);
211                 VectorSubtract((&polypoints[prev * 3]), (&polypoints[i * 3]), v2);
212                 CrossProduct(v1, v2, portalplanes[i].normal);
213                 VectorNormalizeFast(portalplanes[i].normal);
214                 portalplanes[i].dist = DotProduct(eye, portalplanes[i].normal);
215                 if (DotProduct(portalplanes[i].normal, center) <= portalplanes[i].dist)
216                 {
217                         // polygon can't see it's own center, discard
218                         return false;
219                 }
220         }
222         ranoutofportalplanes = false;
223         ranoutofportals = false;
225         returnvalue = Portal_RecursiveFlowSearch(eyeleaf, eye, 0, numpoints);
227         if (ranoutofportalplanes)
228                 Con_Printf("Portal_RecursiveFlowSearch: ran out of %d plane stack when recursing through portals\n", MAXRECURSIVEPORTALPLANES);
229         if (ranoutofportals)
230                 Con_Printf("Portal_RecursiveFlowSearch: ran out of %d portal stack when recursing through portals\n", MAXRECURSIVEPORTALS);
232         return returnvalue;
233 }
235 #define Portal_MinsBoxPolygon(axis, axisvalue, x1, y1, z1, x2, y2, z2, x3, y3, z3, x4, y4, z4) \
236 {\
237         if (eye[(axis)] < ((axisvalue) - 0.5f))\
238         {\
239                 boxpoints[ 0] = x1;boxpoints[ 1] = y1;boxpoints[ 2] = z1;\
240                 boxpoints[ 3] = x2;boxpoints[ 4] = y2;boxpoints[ 5] = z2;\
241                 boxpoints[ 6] = x3;boxpoints[ 7] = y3;boxpoints[ 8] = z3;\
242                 boxpoints[ 9] = x4;boxpoints[10] = y4;boxpoints[11] = z4;\
243                 if (Portal_CheckPolygon(model, eye, boxpoints, 4))\
244                         return true;\
245         }\
246 }
248 #define Portal_MaxsBoxPolygon(axis, axisvalue, x1, y1, z1, x2, y2, z2, x3, y3, z3, x4, y4, z4) \
249 {\
250         if (eye[(axis)] > ((axisvalue) + 0.5f))\
251         {\
252                 boxpoints[ 0] = x1;boxpoints[ 1] = y1;boxpoints[ 2] = z1;\
253                 boxpoints[ 3] = x2;boxpoints[ 4] = y2;boxpoints[ 5] = z2;\
254                 boxpoints[ 6] = x3;boxpoints[ 7] = y3;boxpoints[ 8] = z3;\
255                 boxpoints[ 9] = x4;boxpoints[10] = y4;boxpoints[11] = z4;\
256                 if (Portal_CheckPolygon(model, eye, boxpoints, 4))\
257                         return true;\
258         }\
259 }
261 int Portal_CheckBox(model_t *model, vec3_t eye, vec3_t a, vec3_t b)
262 {
263         if (eye[0] >= (a[0] - 1.0f) && eye[0] < (b[0] + 1.0f)
264          && eye[1] >= (a[1] - 1.0f) && eye[1] < (b[1] + 1.0f)
265          && eye[2] >= (a[2] - 1.0f) && eye[2] < (b[2] + 1.0f))
266                 return true;
268         Portal_MinsBoxPolygon
269         (
270                 0, a[0],
271                 a[0], a[1], a[2],
272                 a[0], b[1], a[2],
273                 a[0], b[1], b[2],
274                 a[0], a[1], b[2]
275         );
276         Portal_MaxsBoxPolygon
277         (
278                 0, b[0],
279                 b[0], b[1], a[2],
280                 b[0], a[1], a[2],
281                 b[0], a[1], b[2],
282                 b[0], b[1], b[2]
283         );
284         Portal_MinsBoxPolygon
285         (
286                 1, a[1],
287                 b[0], a[1], a[2],
288                 a[0], a[1], a[2],
289                 a[0], a[1], b[2],
290                 b[0], a[1], b[2]
291         );
292         Portal_MaxsBoxPolygon
293         (
294                 1, b[1],
295                 a[0], b[1], a[2],
296                 b[0], b[1], a[2],
297                 b[0], b[1], b[2],
298                 a[0], b[1], b[2]
299         );
300         Portal_MinsBoxPolygon
301         (
302                 2, a[2],
303                 a[0], a[1], a[2],
304                 b[0], a[1], a[2],
305                 b[0], b[1], a[2],
306                 a[0], b[1], a[2]
307         );
308         Portal_MaxsBoxPolygon
309         (
310                 2, b[2],
311                 b[0], a[1], b[2],
312                 a[0], a[1], b[2],
313                 a[0], b[1], b[2],
314                 b[0], b[1], b[2]
315         );
317         return false;
318 }
320 vec3_t trianglepoints[3];
322 typedef struct portalrecursioninfo_s
323 {
324         int exact;
325         int numfrustumplanes;
326         vec3_t boxmins;
327         vec3_t boxmaxs;
328         int numsurfaces;
329         int *surfacelist;
330         qbyte *surfacepvs;
331         int numleafs;
332         int *leaflist;
333         qbyte *leafpvs;
334         model_t *model;
335         vec3_t eye;
336         float *updateleafsmins;
337         float *updateleafsmaxs;
338 }
339 portalrecursioninfo_t;
341 void Portal_RecursiveFlow_ExactLeafFaces(portalrecursioninfo_t *info, int *mark, int numleafsurfaces, int firstclipplane, int numclipplanes)
342 {
343         int i, j, *elements;
344         float *v[3], *vertex3f;
345         vec3_t trimins, trimaxs;
346         msurface_t *surface;
347         for (i = 0;i < numleafsurfaces;i++, mark++)
348         {
349                 if (!CHECKPVSBIT(info->surfacepvs, *mark))
350                 {
351                         surface = info->model->brush.data_surfaces + *mark;
352                         if (!BoxesOverlap(surface->mins, surface->maxs, info->boxmins, info->boxmaxs))
353                                 continue;
354                         vertex3f = surface->groupmesh->data_vertex3f;
355                         for (j = 0, elements = (surface->groupmesh->data_element3i + 3 * surface->num_firsttriangle);j < surface->num_triangles;j++, elements += 3)
356                         {
357                                 v[0] = vertex3f + elements[0] * 3;
358                                 v[1] = vertex3f + elements[1] * 3;
359                                 v[2] = vertex3f + elements[2] * 3;
360                                 if (PointInfrontOfTriangle(info->eye, v[0], v[1], v[2]))
361                                 {
362                                         trimins[0] = min(v[0][0], min(v[1][0], v[2][0]));
363                                         trimaxs[0] = max(v[0][0], max(v[1][0], v[2][0]));
364                                         trimins[1] = min(v[0][1], min(v[1][1], v[2][1]));
365                                         trimaxs[1] = max(v[0][1], max(v[1][1], v[2][1]));
366                                         trimins[2] = min(v[0][2], min(v[1][2], v[2][2]));
367                                         trimaxs[2] = max(v[0][2], max(v[1][2], v[2][2]));
368                                         if (BoxesOverlap(trimins, trimaxs, info->boxmins, info->boxmaxs))
369                                                 if (Portal_PortalThroughPortalPlanes(&portalplanes[firstclipplane], numclipplanes, v[0], 3, &portaltemppoints2[0][0], 256) >= 3)
370                                                         break;
371                                 }
372                         }
373                         if (j == surface->num_triangles)
374                                 continue;
375                         SETPVSBIT(info->surfacepvs, *mark);
376                         info->surfacelist[info->numsurfaces++] = *mark;
377                 }
378         }
379 }
381 void Portal_RecursiveFlow (portalrecursioninfo_t *info, mleaf_t *leaf, int firstclipplane, int numclipplanes)
382 {
383         mportal_t *p;
384         int newpoints, i, prev;
385         float dist;
386         vec3_t center, v1, v2;
387         tinyplane_t *newplanes;
389         for (i = 0;i < 3;i++)
390         {
391                 if (info->updateleafsmins && info->updateleafsmins[i] > leaf->mins[i]) info->updateleafsmins[i] = leaf->mins[i];
392                 if (info->updateleafsmaxs && info->updateleafsmaxs[i] < leaf->maxs[i]) info->updateleafsmaxs[i] = leaf->maxs[i];
393         }
396         if (info->leafpvs)
397         {
398                 int leafindex = leaf - info->model->brush.data_leafs;
399                 if (!CHECKPVSBIT(info->leafpvs, leafindex))
400                 {
401                         SETPVSBIT(info->leafpvs, leafindex);
402                         info->leaflist[info->numleafs++] = leafindex;
403                 }
404         }
406         // mark surfaces in leaf that can be seen through portal
407         if (leaf->numleafsurfaces && info->surfacepvs)
408         {
409                 if (info->exact)
410                         Portal_RecursiveFlow_ExactLeafFaces(info, leaf->firstleafsurface, leaf->numleafsurfaces, firstclipplane, numclipplanes);
411                 else
412                 {
413                         for (i = 0;i < leaf->numleafsurfaces;i++)
414                         {
415                                 int surfaceindex = leaf->firstleafsurface[i];
416                                 if (!CHECKPVSBIT(info->surfacepvs, surfaceindex))
417                                 {
418                                         msurface_t *surface = info->model->brush.data_surfaces + surfaceindex;
419                                         if (BoxesOverlap(surface->mins, surface->maxs, info->boxmins, info->boxmaxs))
420                                         {
421                                                 SETPVSBIT(info->surfacepvs, surfaceindex);
422                                                 info->surfacelist[info->numsurfaces++] = surfaceindex;
423                                         }
424                                 }
425                         }
426                 }
427         }
429         // follow portals into other leafs
430         for (p = leaf->portals;p;p = p->next)
431         {
432                 // only flow through portals facing the viewer
433                 dist = PlaneDiff(info->eye, (&p->plane));
434                 if (dist < 0 && BoxesOverlap(p->past->mins, p->past->maxs, info->boxmins, info->boxmaxs))
435                 {
436                         newpoints = Portal_PortalThroughPortalPlanes(&portalplanes[firstclipplane], numclipplanes, (float *) p->points, p->numpoints, &portaltemppoints2[0][0], 256);
437                         if (newpoints < 3)
438                                 continue;
439                         else if (firstclipplane + numclipplanes + newpoints > MAXRECURSIVEPORTALPLANES)
440                                 ranoutofportalplanes = true;
441                         else
442                         {
443                                 // find the center by averaging
444                                 VectorClear(center);
445                                 for (i = 0;i < newpoints;i++)
447                                 // ixtable is a 1.0f / N table
448                                 VectorScale(center, ixtable[newpoints], center);
449                                 // calculate the planes, and make sure the polygon can see it's own center
450                                 newplanes = &portalplanes[firstclipplane + numclipplanes];
451                                 for (prev = newpoints - 1, i = 0;i < newpoints;prev = i, i++)
452                                 {
453                                         VectorSubtract(portaltemppoints2[prev], portaltemppoints2[i], v1);
454                                         VectorSubtract(info->eye, portaltemppoints2[i], v2);
455                                         CrossProduct(v1, v2, newplanes[i].normal);
456                                         VectorNormalizeFast(newplanes[i].normal);
457                                         newplanes[i].dist = DotProduct(info->eye, newplanes[i].normal);
458                                         if (DotProduct(newplanes[i].normal, center) <= newplanes[i].dist)
459                                         {
460                                                 // polygon can't see it's own center, discard and use parent portal
461                                                 break;
462                                         }
463                                 }
464                                 if (i == newpoints)
465                                         Portal_RecursiveFlow(info, p->past, firstclipplane + numclipplanes, newpoints);
466                                 else
467                                         Portal_RecursiveFlow(info, p->past, firstclipplane, numclipplanes);
468                         }
469                 }
470         }
471 }
473 void Portal_RecursiveFindLeafForFlow(portalrecursioninfo_t *info, mnode_t *node)
474 {
475         if (node->plane)
476         {
477                 float f = DotProduct(info->eye, node->plane->normal) - node->plane->dist;
478                 if (f > -0.1)
479                         Portal_RecursiveFindLeafForFlow(info, node->children[0]);
480                 if (f < 0.1)
481                         Portal_RecursiveFindLeafForFlow(info, node->children[1]);
482         }
483         else
484         {
485                 mleaf_t *leaf = (mleaf_t *)node;
486                 if (leaf->portals)
487                         Portal_RecursiveFlow(info, leaf, 0, info->numfrustumplanes);
488         }
489 }
491 void Portal_Visibility(model_t *model, const vec3_t eye, int *leaflist, qbyte *leafpvs, int *numleafspointer, int *surfacelist, qbyte *surfacepvs, int *numsurfacespointer, const mplane_t *frustumplanes, int numfrustumplanes, int exact, const float *boxmins, const float *boxmaxs, float *updateleafsmins, float *updateleafsmaxs)
492 {
493         int i;
494         portalrecursioninfo_t info;
496         // if there is no model, it can not block visibility
497         if (model == NULL)
498         {
499                 Con_Print("Portal_Visibility: NULL model\n");
500                 return;
501         }
505         if (!model->brush.num_portals)
506         {
507                 Con_Print("Portal_Visibility: not a brush model\n");
508                 return;
509         }
511         // put frustum planes (if any) into tinyplane format at start of buffer
512         for (i = 0;i < numfrustumplanes;i++)
513         {
514                 VectorCopy(frustumplanes[i].normal, portalplanes[i].normal);
515                 portalplanes[i].dist = frustumplanes[i].dist;
516         }
518         ranoutofportalplanes = false;
519         ranoutofportals = false;
521         VectorCopy(boxmins, info.boxmins);
522         VectorCopy(boxmaxs, info.boxmaxs);
523         info.exact = exact;
524         info.numsurfaces = 0;
525         info.surfacelist = surfacelist;
526         info.surfacepvs = surfacepvs;
527         info.numleafs = 0;
528         info.leaflist = leaflist;
529         info.leafpvs = leafpvs;
530         info.model = model;
531         VectorCopy(eye, info.eye);
532         info.numfrustumplanes = numfrustumplanes;
533         info.updateleafsmins = updateleafsmins;
534         info.updateleafsmaxs = updateleafsmaxs;
536         Portal_RecursiveFindLeafForFlow(&info, model->brush.data_nodes);
538         if (ranoutofportalplanes)
539                 Con_Printf("Portal_RecursiveFlow: ran out of %d plane stack when recursing through portals\n", MAXRECURSIVEPORTALPLANES);
540         if (ranoutofportals)
541                 Con_Printf("Portal_RecursiveFlow: ran out of %d portal stack when recursing through portals\n", MAXRECURSIVEPORTALS);
542         if (numsurfacespointer)
543                 *numsurfacespointer = info.numsurfaces;
544         if (numleafspointer)
545                 *numleafspointer = info.numleafs;
546 }