]> icculus.org git repositories - divverent/darkplaces.git/blob - portals.c
remove stray \n characters in Cmd_AddCommand calls
[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         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];
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) && TriangleOverlapsBox(v, v + 3, v + 6, info->boxmins, info->boxmaxs) && Portal_PortalThroughPortalPlanes(&portalplanes[firstclipplane], numclipplanes, v, 3, &portaltemppoints2[0][0], 256) > 0)
335                                                         {
336                                                                 SETPVSBIT(info->surfacepvs, surfaceindex);
337                                                                 info->surfacelist[info->numsurfaces++] = surfaceindex;
338                                                                 break;
339                                                         }
340                                                 }
341                                         }
342                                         else
343                                         {
344                                                 SETPVSBIT(info->surfacepvs, surfaceindex);
345                                                 info->surfacelist[info->numsurfaces++] = surfaceindex;
346                                         }
347                                 }
348                         }
349                 }
350         }
351
352         // follow portals into other leafs
353         for (p = leaf->portals;p;p = p->next)
354         {
355                 // only flow through portals facing the viewer
356                 dist = PlaneDiff(info->eye, (&p->plane));
357                 if (dist < 0 && BoxesOverlap(p->past->mins, p->past->maxs, info->boxmins, info->boxmaxs))
358                 {
359                         newpoints = Portal_PortalThroughPortalPlanes(&portalplanes[firstclipplane], numclipplanes, (float *) p->points, p->numpoints, &portaltemppoints2[0][0], 256);
360                         if (newpoints < 3)
361                                 continue;
362                         else if (firstclipplane + numclipplanes + newpoints > MAXRECURSIVEPORTALPLANES)
363                                 ranoutofportalplanes = true;
364                         else
365                         {
366                                 // find the center by averaging
367                                 VectorClear(center);
368                                 for (i = 0;i < newpoints;i++)
369                                         VectorAdd(center, portaltemppoints2[i], center);
370                                 // ixtable is a 1.0f / N table
371                                 VectorScale(center, ixtable[newpoints], center);
372                                 // calculate the planes, and make sure the polygon can see its own center
373                                 newplanes = &portalplanes[firstclipplane + numclipplanes];
374                                 for (prev = newpoints - 1, i = 0;i < newpoints;prev = i, i++)
375                                 {
376                                         TriangleNormal(portaltemppoints2[prev], portaltemppoints2[i], info->eye, newplanes[i].normal);
377                                         VectorNormalize(newplanes[i].normal);
378                                         newplanes[i].dist = DotProduct(info->eye, newplanes[i].normal);
379                                         if (DotProduct(newplanes[i].normal, center) <= newplanes[i].dist)
380                                         {
381                                                 // polygon can't see its own center, discard and use parent portal
382                                                 break;
383                                         }
384                                 }
385                                 if (i == newpoints)
386                                         Portal_RecursiveFlow(info, p->past, firstclipplane + numclipplanes, newpoints);
387                                 else
388                                         Portal_RecursiveFlow(info, p->past, firstclipplane, numclipplanes);
389                         }
390                 }
391         }
392 }
393
394 static void Portal_RecursiveFindLeafForFlow(portalrecursioninfo_t *info, mnode_t *node)
395 {
396         if (node->plane)
397         {
398                 float f = DotProduct(info->eye, node->plane->normal) - node->plane->dist;
399                 if (f > -0.1)
400                         Portal_RecursiveFindLeafForFlow(info, node->children[0]);
401                 if (f < 0.1)
402                         Portal_RecursiveFindLeafForFlow(info, node->children[1]);
403         }
404         else
405         {
406                 mleaf_t *leaf = (mleaf_t *)node;
407                 if (leaf->clusterindex >= 0)
408                         Portal_RecursiveFlow(info, leaf, 0, info->numfrustumplanes);
409         }
410 }
411
412 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)
413 {
414         int i;
415         portalrecursioninfo_t info;
416
417         // if there is no model, it can not block visibility
418         if (model == NULL)
419         {
420                 Con_Print("Portal_Visibility: NULL model\n");
421                 return;
422         }
423
424         if (!model->brush.data_nodes)
425         {
426                 Con_Print("Portal_Visibility: not a brush model\n");
427                 return;
428         }
429
430         // put frustum planes (if any) into tinyplane format at start of buffer
431         for (i = 0;i < numfrustumplanes;i++)
432         {
433                 VectorCopy(frustumplanes[i].normal, portalplanes[i].normal);
434                 portalplanes[i].dist = frustumplanes[i].dist;
435         }
436
437         ranoutofportalplanes = false;
438         ranoutofportals = false;
439
440         VectorCopy(boxmins, info.boxmins);
441         VectorCopy(boxmaxs, info.boxmaxs);
442         info.exact = exact;
443         info.numsurfaces = 0;
444         info.surfacelist = surfacelist;
445         info.surfacepvs = surfacepvs;
446         info.numleafs = 0;
447         info.leaflist = leaflist;
448         info.leafpvs = leafpvs;
449         info.model = model;
450         VectorCopy(eye, info.eye);
451         info.numfrustumplanes = numfrustumplanes;
452         info.updateleafsmins = updateleafsmins;
453         info.updateleafsmaxs = updateleafsmaxs;
454
455         Portal_RecursiveFindLeafForFlow(&info, model->brush.data_nodes);
456
457         if (ranoutofportalplanes)
458                 Con_Printf("Portal_RecursiveFlow: ran out of %d plane stack when recursing through portals\n", MAXRECURSIVEPORTALPLANES);
459         if (ranoutofportals)
460                 Con_Printf("Portal_RecursiveFlow: ran out of %d portal stack when recursing through portals\n", MAXRECURSIVEPORTALS);
461         if (numsurfacespointer)
462                 *numsurfacespointer = info.numsurfaces;
463         if (numleafspointer)
464                 *numleafspointer = info.numleafs;
465 }
466