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