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