]> icculus.org git repositories - divverent/darkplaces.git/blob - portals.c
portal code now uses Polygon_DivideF instead of its own polygon clipper
[divverent/darkplaces.git] / portals.c
1
2 #include "quakedef.h"
3
4 #define MAXRECURSIVEPORTALPLANES 1024
5 #define MAXRECURSIVEPORTALS 256
6
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];
14
15 int Portal_PortalThroughPortalPlanes(tinyplane_t *clipplanes, int clipnumplanes, float *targpoints, int targnumpoints, float *out, int maxpoints)
16 {
17         int numpoints, i;
18         if (targnumpoints < 3)
19                 return targnumpoints;
20         if (maxpoints < 3)
21                 return -1;
22         numpoints = targnumpoints;
23         memcpy(&portaltemppoints[0][0][0], targpoints, numpoints * 3 * sizeof(float));
24         for (i = 0;i < clipnumplanes;i++)
25         {
26                 PolygonF_Divide(numpoints, &portaltemppoints[0][0][0], clipplanes[i].normal[0], clipplanes[i].normal[1], clipplanes[i].normal[2], clipplanes[i].dist, 1.0f/32.0f, 256, &portaltemppoints[1][0][0], &numpoints, 0, NULL, NULL);
27                 if (numpoints < 3)
28                         return numpoints;
29                 memcpy(&portaltemppoints[0][0][0], &portaltemppoints[1][0][0], numpoints * 3 * sizeof(float));
30         }
31         if (numpoints > maxpoints)
32                 return -1;
33         memcpy(out, &portaltemppoints[0][0][0], numpoints * 3 * sizeof(float));
34         return numpoints;
35 }
36
37 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                                         VectorNormalizeFast(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 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         Mod_CheckLoaded(model);
143         Portal_PolygonRecursiveMarkLeafs(model->brush.data_nodes, polypoints, numpoints);
144
145         eyeleaf = model->brush.PointInLeaf(model, eye);
146
147         // find the center by averaging
148         VectorClear(center);
149         for (i = 0;i < numpoints;i++)
150                 VectorAdd(center, (&polypoints[i * 3]), center);
151         // ixtable is a 1.0f / N table
152         VectorScale(center, ixtable[numpoints], center);
153
154         // calculate the planes, and make sure the polygon can see it's own center
155         for (prev = numpoints - 1, i = 0;i < numpoints;prev = i, i++)
156         {
157                 VectorSubtract(eye, (&polypoints[i * 3]), v1);
158                 VectorSubtract((&polypoints[prev * 3]), (&polypoints[i * 3]), v2);
159                 CrossProduct(v1, v2, portalplanes[i].normal);
160                 VectorNormalizeFast(portalplanes[i].normal);
161                 portalplanes[i].dist = DotProduct(eye, portalplanes[i].normal);
162                 if (DotProduct(portalplanes[i].normal, center) <= portalplanes[i].dist)
163                 {
164                         // polygon can't see it's own center, discard
165                         return false;
166                 }
167         }
168
169         ranoutofportalplanes = false;
170         ranoutofportals = false;
171
172         returnvalue = Portal_RecursiveFlowSearch(eyeleaf, eye, 0, numpoints);
173
174         if (ranoutofportalplanes)
175                 Con_Printf("Portal_RecursiveFlowSearch: ran out of %d plane stack when recursing through portals\n", MAXRECURSIVEPORTALPLANES);
176         if (ranoutofportals)
177                 Con_Printf("Portal_RecursiveFlowSearch: ran out of %d portal stack when recursing through portals\n", MAXRECURSIVEPORTALS);
178
179         return returnvalue;
180 }
181
182 #define Portal_MinsBoxPolygon(axis, axisvalue, x1, y1, z1, x2, y2, z2, x3, y3, z3, x4, y4, z4) \
183 {\
184         if (eye[(axis)] < ((axisvalue) - 0.5f))\
185         {\
186                 boxpoints[ 0] = x1;boxpoints[ 1] = y1;boxpoints[ 2] = z1;\
187                 boxpoints[ 3] = x2;boxpoints[ 4] = y2;boxpoints[ 5] = z2;\
188                 boxpoints[ 6] = x3;boxpoints[ 7] = y3;boxpoints[ 8] = z3;\
189                 boxpoints[ 9] = x4;boxpoints[10] = y4;boxpoints[11] = z4;\
190                 if (Portal_CheckPolygon(model, eye, boxpoints, 4))\
191                         return true;\
192         }\
193 }
194
195 #define Portal_MaxsBoxPolygon(axis, axisvalue, x1, y1, z1, x2, y2, z2, x3, y3, z3, x4, y4, z4) \
196 {\
197         if (eye[(axis)] > ((axisvalue) + 0.5f))\
198         {\
199                 boxpoints[ 0] = x1;boxpoints[ 1] = y1;boxpoints[ 2] = z1;\
200                 boxpoints[ 3] = x2;boxpoints[ 4] = y2;boxpoints[ 5] = z2;\
201                 boxpoints[ 6] = x3;boxpoints[ 7] = y3;boxpoints[ 8] = z3;\
202                 boxpoints[ 9] = x4;boxpoints[10] = y4;boxpoints[11] = z4;\
203                 if (Portal_CheckPolygon(model, eye, boxpoints, 4))\
204                         return true;\
205         }\
206 }
207
208 int Portal_CheckBox(model_t *model, vec3_t eye, vec3_t a, vec3_t b)
209 {
210         if (eye[0] >= (a[0] - 1.0f) && eye[0] < (b[0] + 1.0f)
211          && eye[1] >= (a[1] - 1.0f) && eye[1] < (b[1] + 1.0f)
212          && eye[2] >= (a[2] - 1.0f) && eye[2] < (b[2] + 1.0f))
213                 return true;
214
215         Portal_MinsBoxPolygon
216         (
217                 0, a[0],
218                 a[0], a[1], a[2],
219                 a[0], b[1], a[2],
220                 a[0], b[1], b[2],
221                 a[0], a[1], b[2]
222         );
223         Portal_MaxsBoxPolygon
224         (
225                 0, b[0],
226                 b[0], b[1], a[2],
227                 b[0], a[1], a[2],
228                 b[0], a[1], b[2],
229                 b[0], b[1], b[2]
230         );
231         Portal_MinsBoxPolygon
232         (
233                 1, a[1],
234                 b[0], a[1], a[2],
235                 a[0], a[1], a[2],
236                 a[0], a[1], b[2],
237                 b[0], a[1], b[2]
238         );
239         Portal_MaxsBoxPolygon
240         (
241                 1, b[1],
242                 a[0], b[1], a[2],
243                 b[0], b[1], a[2],
244                 b[0], b[1], b[2],
245                 a[0], b[1], b[2]
246         );
247         Portal_MinsBoxPolygon
248         (
249                 2, a[2],
250                 a[0], a[1], a[2],
251                 b[0], a[1], a[2],
252                 b[0], b[1], a[2],
253                 a[0], b[1], a[2]
254         );
255         Portal_MaxsBoxPolygon
256         (
257                 2, b[2],
258                 b[0], a[1], b[2],
259                 a[0], a[1], b[2],
260                 a[0], b[1], b[2],
261                 b[0], b[1], b[2]
262         );
263
264         return false;
265 }
266
267 vec3_t trianglepoints[3];
268
269 typedef struct portalrecursioninfo_s
270 {
271         int exact;
272         int numfrustumplanes;
273         vec3_t boxmins;
274         vec3_t boxmaxs;
275         int numsurfaces;
276         int *surfacelist;
277         qbyte *surfacepvs;
278         int numleafs;
279         int *leaflist;
280         qbyte *leafpvs;
281         model_t *model;
282         vec3_t eye;
283         float *updateleafsmins;
284         float *updateleafsmaxs;
285 }
286 portalrecursioninfo_t;
287
288 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, v1, v2;
294         tinyplane_t *newplanes;
295
296         for (i = 0;i < 3;i++)
297         {
298                 if (info->updateleafsmins && info->updateleafsmins[i] > leaf->mins[i]) info->updateleafsmins[i] = leaf->mins[i];
299                 if (info->updateleafsmaxs && info->updateleafsmaxs[i] < leaf->maxs[i]) info->updateleafsmaxs[i] = leaf->maxs[i];
300         }
301
302
303         if (info->leafpvs)
304         {
305                 int leafindex = leaf - info->model->brush.data_leafs;
306                 if (!CHECKPVSBIT(info->leafpvs, leafindex))
307                 {
308                         SETPVSBIT(info->leafpvs, leafindex);
309                         info->leaflist[info->numleafs++] = leafindex;
310                 }
311         }
312
313         // mark surfaces in leaf that can be seen through portal
314         if (leaf->numleafsurfaces && info->surfacepvs)
315         {
316                 for (i = 0;i < leaf->numleafsurfaces;i++)
317                 {
318                         int surfaceindex = leaf->firstleafsurface[i];
319                         if (!CHECKPVSBIT(info->surfacepvs, surfaceindex))
320                         {
321                                 msurface_t *surface = info->model->brush.data_surfaces + surfaceindex;
322                                 if (BoxesOverlap(surface->mins, surface->maxs, info->boxmins, info->boxmaxs))
323                                 {
324                                         if (info->exact)
325                                         {
326                                                 int j;
327                                                 const int *elements;
328                                                 const float *vertex3f;
329                                                 float v[9], trimins[3], trimaxs[3];
330                                                 vertex3f = surface->groupmesh->data_vertex3f;
331                                                 elements = (surface->groupmesh->data_element3i + 3 * surface->num_firsttriangle);
332                                                 for (j = 0;j < surface->num_triangles;j++, elements += 3)
333                                                 {
334                                                         VectorCopy(vertex3f + elements[0] * 3, v + 0);
335                                                         VectorCopy(vertex3f + elements[1] * 3, v + 3);
336                                                         VectorCopy(vertex3f + elements[2] * 3, v + 6);
337                                                         if (PointInfrontOfTriangle(info->eye, v + 0, v + 3, v + 6))
338                                                         {
339                                                                 trimins[0] = min(v[0], min(v[3], v[6]));
340                                                                 trimaxs[0] = max(v[0], max(v[3], v[6]));
341                                                                 trimins[1] = min(v[1], min(v[4], v[7]));
342                                                                 trimaxs[1] = max(v[1], max(v[4], v[7]));
343                                                                 trimins[2] = min(v[2], min(v[5], v[8]));
344                                                                 trimaxs[2] = max(v[2], max(v[5], v[8]));
345                                                                 if (BoxesOverlap(trimins, trimaxs, info->boxmins, info->boxmaxs) && Portal_PortalThroughPortalPlanes(&portalplanes[firstclipplane], numclipplanes, v, 3, &portaltemppoints2[0][0], 256) >= 3)
346                                                                 {
347                                                                         SETPVSBIT(info->surfacepvs, surfaceindex);
348                                                                         info->surfacelist[info->numsurfaces++] = surfaceindex;
349                                                                         break;
350                                                                 }
351                                                         }
352                                                 }
353                                         }
354                                         else
355                                         {
356                                                 SETPVSBIT(info->surfacepvs, surfaceindex);
357                                                 info->surfacelist[info->numsurfaces++] = surfaceindex;
358                                         }
359                                 }
360                         }
361                 }
362         }
363
364         // follow portals into other leafs
365         for (p = leaf->portals;p;p = p->next)
366         {
367                 // only flow through portals facing the viewer
368                 dist = PlaneDiff(info->eye, (&p->plane));
369                 if (dist < 0 && BoxesOverlap(p->past->mins, p->past->maxs, info->boxmins, info->boxmaxs))
370                 {
371                         newpoints = Portal_PortalThroughPortalPlanes(&portalplanes[firstclipplane], numclipplanes, (float *) p->points, p->numpoints, &portaltemppoints2[0][0], 256);
372                         if (newpoints < 3)
373                                 continue;
374                         else if (firstclipplane + numclipplanes + newpoints > MAXRECURSIVEPORTALPLANES)
375                                 ranoutofportalplanes = true;
376                         else
377                         {
378                                 // find the center by averaging
379                                 VectorClear(center);
380                                 for (i = 0;i < newpoints;i++)
381                                         VectorAdd(center, portaltemppoints2[i], center);
382                                 // ixtable is a 1.0f / N table
383                                 VectorScale(center, ixtable[newpoints], center);
384                                 // calculate the planes, and make sure the polygon can see it's own center
385                                 newplanes = &portalplanes[firstclipplane + numclipplanes];
386                                 for (prev = newpoints - 1, i = 0;i < newpoints;prev = i, i++)
387                                 {
388                                         VectorSubtract(portaltemppoints2[prev], portaltemppoints2[i], v1);
389                                         VectorSubtract(info->eye, portaltemppoints2[i], v2);
390                                         CrossProduct(v1, v2, newplanes[i].normal);
391                                         VectorNormalizeFast(newplanes[i].normal);
392                                         newplanes[i].dist = DotProduct(info->eye, newplanes[i].normal);
393                                         if (DotProduct(newplanes[i].normal, center) <= newplanes[i].dist)
394                                         {
395                                                 // polygon can't see it's own center, discard and use parent portal
396                                                 break;
397                                         }
398                                 }
399                                 if (i == newpoints)
400                                         Portal_RecursiveFlow(info, p->past, firstclipplane + numclipplanes, newpoints);
401                                 else
402                                         Portal_RecursiveFlow(info, p->past, firstclipplane, numclipplanes);
403                         }
404                 }
405         }
406 }
407
408 void Portal_RecursiveFindLeafForFlow(portalrecursioninfo_t *info, mnode_t *node)
409 {
410         if (node->plane)
411         {
412                 float f = DotProduct(info->eye, node->plane->normal) - node->plane->dist;
413                 if (f > -0.1)
414                         Portal_RecursiveFindLeafForFlow(info, node->children[0]);
415                 if (f < 0.1)
416                         Portal_RecursiveFindLeafForFlow(info, node->children[1]);
417         }
418         else
419         {
420                 mleaf_t *leaf = (mleaf_t *)node;
421                 if (leaf->portals)
422                         Portal_RecursiveFlow(info, leaf, 0, info->numfrustumplanes);
423         }
424 }
425
426 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)
427 {
428         int i;
429         portalrecursioninfo_t info;
430
431         // if there is no model, it can not block visibility
432         if (model == NULL)
433         {
434                 Con_Print("Portal_Visibility: NULL model\n");
435                 return;
436         }
437
438         Mod_CheckLoaded(model);
439
440         if (!model->brush.num_portals)
441         {
442                 Con_Print("Portal_Visibility: not a brush model\n");
443                 return;
444         }
445
446         // put frustum planes (if any) into tinyplane format at start of buffer
447         for (i = 0;i < numfrustumplanes;i++)
448         {
449                 VectorCopy(frustumplanes[i].normal, portalplanes[i].normal);
450                 portalplanes[i].dist = frustumplanes[i].dist;
451         }
452
453         ranoutofportalplanes = false;
454         ranoutofportals = false;
455
456         VectorCopy(boxmins, info.boxmins);
457         VectorCopy(boxmaxs, info.boxmaxs);
458         info.exact = exact;
459         info.numsurfaces = 0;
460         info.surfacelist = surfacelist;
461         info.surfacepvs = surfacepvs;
462         info.numleafs = 0;
463         info.leaflist = leaflist;
464         info.leafpvs = leafpvs;
465         info.model = model;
466         VectorCopy(eye, info.eye);
467         info.numfrustumplanes = numfrustumplanes;
468         info.updateleafsmins = updateleafsmins;
469         info.updateleafsmaxs = updateleafsmaxs;
470
471         Portal_RecursiveFindLeafForFlow(&info, model->brush.data_nodes);
472
473         if (ranoutofportalplanes)
474                 Con_Printf("Portal_RecursiveFlow: ran out of %d plane stack when recursing through portals\n", MAXRECURSIVEPORTALPLANES);
475         if (ranoutofportals)
476                 Con_Printf("Portal_RecursiveFlow: ran out of %d portal stack when recursing through portals\n", MAXRECURSIVEPORTALS);
477         if (numsurfacespointer)
478                 *numsurfacespointer = info.numsurfaces;
479         if (numleafspointer)
480                 *numleafspointer = info.numleafs;
481 }
482