]> de.git.xonotic.org Git - xonotic/darkplaces.git/blob - portals.c
enlarge bbox for visibility purposes if there is a glow or other such effect
[xonotic/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 portalplanecount;
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_ClipPolygonToPlane(float *in, float *out, int inpoints, int maxoutpoints, tinyplane_t *p)
17 {
18         int i, outpoints, prevside, side;
19         float *prevpoint, prevdist, dist, dot;
20
21         if (inpoints < 3)
22                 return inpoints;
23         // begin with the last point, then enter the loop with the first point as current
24         prevpoint = in + 3 * (inpoints - 1);
25         prevdist = DotProduct(prevpoint, p->normal) - p->dist;
26         prevside = prevdist >= 0 ? SIDE_FRONT : SIDE_BACK;
27         i = 0;
28         outpoints = 0;
29         goto begin;
30         for (;i < inpoints;i++)
31         {
32                 prevpoint = in;
33                 prevdist = dist;
34                 prevside = side;
35                 in += 3;
36
37 begin:
38                 dist = DotProduct(in, p->normal) - p->dist;
39                 side = dist >= 0 ? SIDE_FRONT : SIDE_BACK;
40
41                 if (prevside == SIDE_FRONT)
42                 {
43                         if (outpoints >= maxoutpoints)
44                                 return -1;
45                         VectorCopy(prevpoint, out);
46                         out += 3;
47                         outpoints++;
48                         if (side == SIDE_FRONT)
49                                 continue;
50                 }
51                 else if (side == SIDE_BACK)
52                         continue;
53
54                 // generate a split point
55                 if (outpoints >= maxoutpoints)
56                         return -1;
57                 dot = prevdist / (prevdist - dist);
58                 out[0] = prevpoint[0] + dot * (in[0] - prevpoint[0]);
59                 out[1] = prevpoint[1] + dot * (in[1] - prevpoint[1]);
60                 out[2] = prevpoint[2] + dot * (in[2] - prevpoint[2]);
61                 out += 3;
62                 outpoints++;
63         }
64
65         return outpoints;
66 }
67
68
69 int Portal_PortalThroughPortalPlanes(tinyplane_t *clipplanes, int clipnumplanes, float *targpoints, int targnumpoints, float *out, int maxpoints)
70 {
71         int numpoints, i;
72         if (targnumpoints < 3)
73                 return targnumpoints;
74         if (maxpoints < 3)
75                 return -1;
76         numpoints = targnumpoints;
77         memcpy(&portaltemppoints[0][0][0], targpoints, numpoints * 3 * sizeof(float));
78         for (i = 0;i < clipnumplanes;i++)
79         {
80                 numpoints = Portal_ClipPolygonToPlane(&portaltemppoints[0][0][0], &portaltemppoints[1][0][0], numpoints, 256, clipplanes + i);
81                 if (numpoints < 3)
82                         return numpoints;
83                 memcpy(&portaltemppoints[0][0][0], &portaltemppoints[1][0][0], numpoints * 3 * sizeof(float));
84         }
85         if (numpoints > maxpoints)
86                 return -1;
87         memcpy(out, &portaltemppoints[1][0][0], numpoints * 3 * sizeof(float));
88         return numpoints;
89 }
90
91 int Portal_RecursiveFlowSearch (mleaf_t *leaf, vec3_t eye, int firstclipplane, int numclipplanes)
92 {
93         mportal_t *p;
94         int newpoints, i, prev;
95         vec3_t center, v1, v2;
96         tinyplane_t *newplanes;
97
98         if (leaf->portalmarkid == portal_markid)
99                 return true;
100
101         // follow portals into other leafs
102         for (p = leaf->portals;p;p = p->next)
103         {
104                 // only flow through portals facing away from the viewer
105                 if (PlaneDiff(eye, (&p->plane)) < 0)
106                 {
107                         newpoints = Portal_PortalThroughPortalPlanes(&portalplanes[firstclipplane], numclipplanes, (float *) p->points, p->numpoints, &portaltemppoints2[0][0], 256);
108                         if (newpoints < 3)
109                                 continue;
110                         else if (firstclipplane + numclipplanes + newpoints > MAXRECURSIVEPORTALPLANES)
111                                 ranoutofportalplanes = true;
112                         else
113                         {
114                                 // find the center by averaging
115                                 VectorClear(center);
116                                 for (i = 0;i < newpoints;i++)
117                                         VectorAdd(center, portaltemppoints2[i], center);
118                                 // ixtable is a 1.0f / N table
119                                 VectorScale(center, ixtable[newpoints], center);
120                                 // calculate the planes, and make sure the polygon can see it's own center
121                                 newplanes = &portalplanes[firstclipplane + numclipplanes];
122                                 for (prev = newpoints - 1, i = 0;i < newpoints;prev = i, i++)
123                                 {
124                                         VectorSubtract(eye, portaltemppoints2[i], v1);
125                                         VectorSubtract(portaltemppoints2[prev], portaltemppoints2[i], v2);
126                                         CrossProduct(v1, v2, newplanes[i].normal);
127                                         VectorNormalizeFast(newplanes[i].normal);
128                                         newplanes[i].dist = DotProduct(eye, newplanes[i].normal);
129                                         if (DotProduct(newplanes[i].normal, center) <= newplanes[i].dist)
130                                         {
131                                                 // polygon can't see it's own center, discard and use parent portal
132                                                 break;
133                                         }
134                                 }
135                                 if (i == newpoints)
136                                 {
137                                         if (Portal_RecursiveFlowSearch(p->past, eye, firstclipplane + numclipplanes, newpoints))
138                                                 return true;
139                         }
140                                 else
141                                 {
142                                         if (Portal_RecursiveFlowSearch(p->past, eye, firstclipplane, numclipplanes))
143                                                 return true;
144                         }
145                         }
146                 }
147         }
148
149         return false;
150 }
151
152 void Portal_PolygonRecursiveMarkLeafs(mnode_t *node, float *polypoints, int numpoints)
153 {
154         int i, front;
155         float *p;
156
157 loc0:
158         if (node->contents < 0)
159         {
160                 ((mleaf_t *)node)->portalmarkid = portal_markid;
161                 return;
162         }
163
164         front = 0;
165         for (i = 0, p = polypoints;i < numpoints;i++, p += 3)
166         {
167                 if (DotProduct(p, node->plane->normal) > node->plane->dist)
168                         front++;
169         }
170         if (front > 0)
171         {
172                 if (front == numpoints)
173                 {
174                         node = node->children[0];
175                         goto loc0;
176                 }
177                 else
178                         Portal_PolygonRecursiveMarkLeafs(node->children[0], polypoints, numpoints);
179         }
180         node = node->children[1];
181         goto loc0;
182 }
183
184 int Portal_CheckPolygon(model_t *model, vec3_t eye, float *polypoints, int numpoints)
185 {
186         int i, prev, returnvalue;
187         mleaf_t *eyeleaf;
188         vec3_t center, v1, v2;
189
190         // if there is no model, it can not block visibility
191         if (model == NULL)
192                 return true;
193
194         portal_markid++;
195
196         Mod_CheckLoaded(model);
197         Portal_PolygonRecursiveMarkLeafs(model->nodes, polypoints, numpoints);
198
199         eyeleaf = Mod_PointInLeaf(eye, model);
200
201         // find the center by averaging
202         VectorClear(center);
203         for (i = 0;i < numpoints;i++)
204                 VectorAdd(center, (&polypoints[i * 3]), center);
205         // ixtable is a 1.0f / N table
206         VectorScale(center, ixtable[numpoints], center);
207
208         // calculate the planes, and make sure the polygon can see it's own center
209         for (prev = numpoints - 1, i = 0;i < numpoints;prev = i, i++)
210         {
211                 VectorSubtract(eye, (&polypoints[i * 3]), v1);
212                 VectorSubtract((&polypoints[prev * 3]), (&polypoints[i * 3]), v2);
213                 CrossProduct(v1, v2, portalplanes[i].normal);
214                 VectorNormalizeFast(portalplanes[i].normal);
215                 portalplanes[i].dist = DotProduct(eye, portalplanes[i].normal);
216                 if (DotProduct(portalplanes[i].normal, center) <= portalplanes[i].dist)
217                 {
218                         // polygon can't see it's own center, discard
219                         return false;
220                 }
221         }
222
223         portalplanecount = 0;
224         ranoutofportalplanes = false;
225         ranoutofportals = false;
226
227         returnvalue = Portal_RecursiveFlowSearch(eyeleaf, eye, 0, numpoints);
228
229         if (ranoutofportalplanes)
230                 Con_Printf("Portal_RecursiveFlowSearch: ran out of %d plane stack when recursing through portals\n", MAXRECURSIVEPORTALPLANES);
231         if (ranoutofportals)
232                 Con_Printf("Portal_RecursiveFlowSearch: ran out of %d portal stack when recursing through portals\n", MAXRECURSIVEPORTALS);
233
234         return returnvalue;
235 }
236
237 #define Portal_MinsBoxPolygon(axis, axisvalue, x1, y1, z1, x2, y2, z2, x3, y3, z3, x4, y4, z4) \
238 {\
239         if (eye[(axis)] < ((axisvalue) - 0.5f))\
240         {\
241                 boxpoints[ 0] = x1;boxpoints[ 1] = y1;boxpoints[ 2] = z1;\
242                 boxpoints[ 3] = x2;boxpoints[ 4] = y2;boxpoints[ 5] = z2;\
243                 boxpoints[ 6] = x3;boxpoints[ 7] = y3;boxpoints[ 8] = z3;\
244                 boxpoints[ 9] = x4;boxpoints[10] = y4;boxpoints[11] = z4;\
245                 if (Portal_CheckPolygon(model, eye, boxpoints, 4))\
246                         return true;\
247         }\
248 }
249
250 #define Portal_MaxsBoxPolygon(axis, axisvalue, x1, y1, z1, x2, y2, z2, x3, y3, z3, x4, y4, z4) \
251 {\
252         if (eye[(axis)] > ((axisvalue) + 0.5f))\
253         {\
254                 boxpoints[ 0] = x1;boxpoints[ 1] = y1;boxpoints[ 2] = z1;\
255                 boxpoints[ 3] = x2;boxpoints[ 4] = y2;boxpoints[ 5] = z2;\
256                 boxpoints[ 6] = x3;boxpoints[ 7] = y3;boxpoints[ 8] = z3;\
257                 boxpoints[ 9] = x4;boxpoints[10] = y4;boxpoints[11] = z4;\
258                 if (Portal_CheckPolygon(model, eye, boxpoints, 4))\
259                         return true;\
260         }\
261 }
262
263 int Portal_CheckBox(model_t *model, vec3_t eye, vec3_t a, vec3_t b)
264 {
265         if (eye[0] >= (a[0] - 1.0f) && eye[0] < (b[0] + 1.0f)
266          && eye[1] >= (a[1] - 1.0f) && eye[1] < (b[1] + 1.0f)
267          && eye[2] >= (a[2] - 1.0f) && eye[2] < (b[2] + 1.0f))
268                 return true;
269
270         Portal_MinsBoxPolygon
271         (
272                 0, a[0],
273                 a[0], a[1], a[2],
274                 a[0], b[1], a[2],
275                 a[0], b[1], b[2],
276                 a[0], a[1], b[2]
277         );
278         Portal_MaxsBoxPolygon
279         (
280                 0, b[0],
281                 b[0], b[1], a[2],
282                 b[0], a[1], a[2],
283                 b[0], a[1], b[2],
284                 b[0], b[1], b[2]
285         );
286         Portal_MinsBoxPolygon
287         (
288                 1, a[1],
289                 b[0], a[1], a[2],
290                 a[0], a[1], a[2],
291                 a[0], a[1], b[2],
292                 b[0], a[1], b[2]
293         );
294         Portal_MaxsBoxPolygon
295         (
296                 1, b[1],
297                 a[0], b[1], a[2],
298                 b[0], b[1], a[2],
299                 b[0], b[1], b[2],
300                 a[0], b[1], b[2]
301         );
302         Portal_MinsBoxPolygon
303         (
304                 2, a[2],
305                 a[0], a[1], a[2],
306                 b[0], a[1], a[2],
307                 b[0], b[1], a[2],
308                 a[0], b[1], a[2]
309         );
310         Portal_MaxsBoxPolygon
311         (
312                 2, b[2],
313                 b[0], a[1], b[2],
314                 a[0], a[1], b[2],
315                 a[0], b[1], b[2],
316                 b[0], b[1], b[2]
317         );
318
319         return false;
320 }
321