]> de.git.xonotic.org Git - xonotic/darkplaces.git/blob - portals.c
fix a couple compatibility issues with Nehahra
[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(dp_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(dp_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         unsigned char *visitingleafpvs; // used to prevent infinite loops
277         int *leaflist;
278         unsigned char *leafpvs;
279         unsigned char *shadowtrispvs;
280         unsigned char *lighttrispvs;
281         dp_model_t *model;
282         vec3_t eye;
283         float *updateleafsmins;
284         float *updateleafsmaxs;
285 }
286 portalrecursioninfo_t;
287
288 static void Portal_RecursiveFlow (portalrecursioninfo_t *info, mleaf_t *leaf, int firstclipplane, int numclipplanes)
289 {
290         mportal_t *p;
291         int newpoints, i, prev;
292         float dist;
293         vec3_t center;
294         tinyplane_t *newplanes;
295         int leafindex = leaf - info->model->brush.data_leafs;
296
297         if (CHECKPVSBIT(info->visitingleafpvs, leafindex))
298                 return; // recursive loop of leafs (cmc.bsp for megatf coop)
299
300         SETPVSBIT(info->visitingleafpvs, leafindex);
301
302         for (i = 0;i < 3;i++)
303         {
304                 if (info->updateleafsmins && info->updateleafsmins[i] > leaf->mins[i]) info->updateleafsmins[i] = leaf->mins[i];
305                 if (info->updateleafsmaxs && info->updateleafsmaxs[i] < leaf->maxs[i]) info->updateleafsmaxs[i] = leaf->maxs[i];
306         }
307
308
309         if (info->leafpvs)
310         {
311                 if (!CHECKPVSBIT(info->leafpvs, leafindex))
312                 {
313                         SETPVSBIT(info->leafpvs, leafindex);
314                         info->leaflist[info->numleafs++] = leafindex;
315                 }
316         }
317
318         // mark surfaces in leaf that can be seen through portal
319         if (leaf->numleafsurfaces && info->surfacepvs)
320         {
321                 for (i = 0;i < leaf->numleafsurfaces;i++)
322                 {
323                         int surfaceindex = leaf->firstleafsurface[i];
324                         msurface_t *surface = info->model->data_surfaces + surfaceindex;
325                         if (BoxesOverlap(surface->mins, surface->maxs, info->boxmins, info->boxmaxs))
326                         {
327                                 qboolean insidebox = BoxInsideBox(surface->mins, surface->maxs, info->boxmins, info->boxmaxs);
328                                 qboolean addedtris = false;
329                                 int t, tend;
330                                 const int *elements;
331                                 const float *vertex3f;
332                                 float v[9];
333                                 vertex3f = info->model->surfmesh.data_vertex3f;
334                                 elements = (info->model->surfmesh.data_element3i + 3 * surface->num_firsttriangle);
335                                 for (t = surface->num_firsttriangle, tend = t + surface->num_triangles;t < tend;t++, elements += 3)
336                                 {
337                                         VectorCopy(vertex3f + elements[0] * 3, v + 0);
338                                         VectorCopy(vertex3f + elements[1] * 3, v + 3);
339                                         VectorCopy(vertex3f + elements[2] * 3, v + 6);
340                                         if (PointInfrontOfTriangle(info->eye, v + 0, v + 3, v + 6)
341                                          && (insidebox || TriangleOverlapsBox(v, v + 3, v + 6, info->boxmins, info->boxmaxs))
342                                          && (!info->exact || Portal_PortalThroughPortalPlanes(&portalplanes[firstclipplane], numclipplanes, v, 3, &portaltemppoints2[0][0], 256) > 0))
343                                         {
344                                                 addedtris = true;
345                                                 if (info->shadowtrispvs)
346                                                         SETPVSBIT(info->shadowtrispvs, t);
347                                                 if (info->lighttrispvs)
348                                                         SETPVSBIT(info->lighttrispvs, t);
349                                         }
350                                 }
351                                 if (addedtris && !CHECKPVSBIT(info->surfacepvs, surfaceindex))
352                                 {
353                                         SETPVSBIT(info->surfacepvs, surfaceindex);
354                                         info->surfacelist[info->numsurfaces++] = surfaceindex;
355                                 }
356                         }
357                 }
358         }
359
360         // follow portals into other leafs
361         for (p = leaf->portals;p;p = p->next)
362         {
363                 // only flow through portals facing the viewer
364                 dist = PlaneDiff(info->eye, (&p->plane));
365                 if (dist < 0 && BoxesOverlap(p->past->mins, p->past->maxs, info->boxmins, info->boxmaxs))
366                 {
367                         newpoints = Portal_PortalThroughPortalPlanes(&portalplanes[firstclipplane], numclipplanes, (float *) p->points, p->numpoints, &portaltemppoints2[0][0], 256);
368                         if (newpoints < 3)
369                                 continue;
370                         else if (firstclipplane + numclipplanes + newpoints > MAXRECURSIVEPORTALPLANES)
371                                 ranoutofportalplanes = true;
372                         else
373                         {
374                                 // find the center by averaging
375                                 VectorClear(center);
376                                 for (i = 0;i < newpoints;i++)
377                                         VectorAdd(center, portaltemppoints2[i], center);
378                                 // ixtable is a 1.0f / N table
379                                 VectorScale(center, ixtable[newpoints], center);
380                                 // calculate the planes, and make sure the polygon can see its own center
381                                 newplanes = &portalplanes[firstclipplane + numclipplanes];
382                                 for (prev = newpoints - 1, i = 0;i < newpoints;prev = i, i++)
383                                 {
384                                         TriangleNormal(portaltemppoints2[prev], portaltemppoints2[i], info->eye, newplanes[i].normal);
385                                         VectorNormalize(newplanes[i].normal);
386                                         newplanes[i].dist = DotProduct(info->eye, newplanes[i].normal);
387                                         if (DotProduct(newplanes[i].normal, center) <= newplanes[i].dist)
388                                         {
389                                                 // polygon can't see its own center, discard and use parent portal
390                                                 break;
391                                         }
392                                 }
393                                 if (i == newpoints)
394                                         Portal_RecursiveFlow(info, p->past, firstclipplane + numclipplanes, newpoints);
395                                 else
396                                         Portal_RecursiveFlow(info, p->past, firstclipplane, numclipplanes);
397                         }
398                 }
399         }
400
401         CLEARPVSBIT(info->visitingleafpvs, leafindex);
402 }
403
404 static void Portal_RecursiveFindLeafForFlow(portalrecursioninfo_t *info, mnode_t *node)
405 {
406         if (node->plane)
407         {
408                 float f = DotProduct(info->eye, node->plane->normal) - node->plane->dist;
409                 if (f > -0.1)
410                         Portal_RecursiveFindLeafForFlow(info, node->children[0]);
411                 if (f < 0.1)
412                         Portal_RecursiveFindLeafForFlow(info, node->children[1]);
413         }
414         else
415         {
416                 mleaf_t *leaf = (mleaf_t *)node;
417                 if (leaf->clusterindex >= 0)
418                         Portal_RecursiveFlow(info, leaf, 0, info->numfrustumplanes);
419         }
420 }
421
422 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)
423 {
424         int i;
425         portalrecursioninfo_t info;
426
427         // if there is no model, it can not block visibility
428         if (model == NULL)
429         {
430                 Con_Print("Portal_Visibility: NULL model\n");
431                 return;
432         }
433
434         if (!model->brush.data_nodes)
435         {
436                 Con_Print("Portal_Visibility: not a brush model\n");
437                 return;
438         }
439
440         // put frustum planes (if any) into tinyplane format at start of buffer
441         for (i = 0;i < numfrustumplanes;i++)
442         {
443                 VectorCopy(frustumplanes[i].normal, portalplanes[i].normal);
444                 portalplanes[i].dist = frustumplanes[i].dist;
445         }
446
447         ranoutofportalplanes = false;
448         ranoutofportals = false;
449
450         VectorCopy(boxmins, info.boxmins);
451         VectorCopy(boxmaxs, info.boxmaxs);
452         info.exact = exact;
453         info.numsurfaces = 0;
454         info.surfacelist = surfacelist;
455         info.surfacepvs = surfacepvs;
456         info.numleafs = 0;
457         info.visitingleafpvs = visitingleafpvs;
458         info.leaflist = leaflist;
459         info.leafpvs = leafpvs;
460         info.model = model;
461         VectorCopy(eye, info.eye);
462         info.numfrustumplanes = numfrustumplanes;
463         info.updateleafsmins = updateleafsmins;
464         info.updateleafsmaxs = updateleafsmaxs;
465         info.shadowtrispvs = shadowtrispvs;
466         info.lighttrispvs = lighttrispvs;
467
468         Portal_RecursiveFindLeafForFlow(&info, model->brush.data_nodes);
469
470         if (ranoutofportalplanes)
471                 Con_Printf("Portal_RecursiveFlow: ran out of %d plane stack when recursing through portals\n", MAXRECURSIVEPORTALPLANES);
472         if (ranoutofportals)
473                 Con_Printf("Portal_RecursiveFlow: ran out of %d portal stack when recursing through portals\n", MAXRECURSIVEPORTALS);
474         if (numsurfacespointer)
475                 *numsurfacespointer = info.numsurfaces;
476         if (numleafspointer)
477                 *numleafspointer = info.numleafs;
478 }
479