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