]> icculus.org git repositories - divverent/darkplaces.git/blob - portals.c
Lots of speedups and cleanups in client code, dynamically allocated cl_entities ...
[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 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         portal_markid++;
191
192         Mod_CheckLoaded(model);
193         Portal_PolygonRecursiveMarkLeafs(model->nodes, polypoints, numpoints);
194
195         eyeleaf = Mod_PointInLeaf(eye, model);
196
197         // find the center by averaging
198         VectorClear(center);
199         for (i = 0;i < numpoints;i++)
200                 VectorAdd(center, (&polypoints[i * 3]), center);
201         // ixtable is a 1.0f / N table
202         VectorScale(center, ixtable[numpoints], center);
203
204         // calculate the planes, and make sure the polygon can see it's own center
205         for (prev = numpoints - 1, i = 0;i < numpoints;prev = i, i++)
206         {
207                 VectorSubtract(eye, (&polypoints[i * 3]), v1);
208                 VectorSubtract((&polypoints[prev * 3]), (&polypoints[i * 3]), v2);
209                 CrossProduct(v1, v2, portalplanes[i].normal);
210                 VectorNormalizeFast(portalplanes[i].normal);
211                 portalplanes[i].dist = DotProduct(eye, portalplanes[i].normal);
212                 if (DotProduct(portalplanes[i].normal, center) <= portalplanes[i].dist)
213                 {
214                         // polygon can't see it's own center, discard
215                         return false;
216                 }
217         }
218
219         portalplanecount = 0;
220         ranoutofportalplanes = false;
221         ranoutofportals = false;
222
223         returnvalue = Portal_RecursiveFlowSearch(eyeleaf, eye, 0, numpoints);
224
225         if (ranoutofportalplanes)
226                 Con_Printf("Portal_RecursiveFlowSearch: ran out of %d plane stack when recursing through portals\n", MAXRECURSIVEPORTALPLANES);
227         if (ranoutofportals)
228                 Con_Printf("Portal_RecursiveFlowSearch: ran out of %d portal stack when recursing through portals\n", MAXRECURSIVEPORTALS);
229
230         return returnvalue;
231 }
232
233 #define Portal_MinsBoxPolygon(axis, axisvalue, x1, y1, z1, x2, y2, z2, x3, y3, z3, x4, y4, z4) \
234 {\
235         if (eye[(axis)] < ((axisvalue) - 0.5f))\
236         {\
237                 boxpoints[ 0] = x1;boxpoints[ 1] = y1;boxpoints[ 2] = z1;\
238                 boxpoints[ 3] = x2;boxpoints[ 4] = y2;boxpoints[ 5] = z2;\
239                 boxpoints[ 6] = x3;boxpoints[ 7] = y3;boxpoints[ 8] = z3;\
240                 boxpoints[ 9] = x4;boxpoints[10] = y4;boxpoints[11] = z4;\
241                 if (Portal_CheckPolygon(model, eye, boxpoints, 4))\
242                         return true;\
243         }\
244 }
245
246 #define Portal_MaxsBoxPolygon(axis, axisvalue, x1, y1, z1, x2, y2, z2, x3, y3, z3, x4, y4, z4) \
247 {\
248         if (eye[(axis)] > ((axisvalue) + 0.5f))\
249         {\
250                 boxpoints[ 0] = x1;boxpoints[ 1] = y1;boxpoints[ 2] = z1;\
251                 boxpoints[ 3] = x2;boxpoints[ 4] = y2;boxpoints[ 5] = z2;\
252                 boxpoints[ 6] = x3;boxpoints[ 7] = y3;boxpoints[ 8] = z3;\
253                 boxpoints[ 9] = x4;boxpoints[10] = y4;boxpoints[11] = z4;\
254                 if (Portal_CheckPolygon(model, eye, boxpoints, 4))\
255                         return true;\
256         }\
257 }
258
259 int Portal_CheckBox(model_t *model, vec3_t eye, vec3_t a, vec3_t b)
260 {
261         if (eye[0] >= (a[0] - 1.0f) && eye[0] < (b[0] + 1.0f)
262          && eye[1] >= (a[1] - 1.0f) && eye[1] < (b[1] + 1.0f)
263          && eye[2] >= (a[2] - 1.0f) && eye[2] < (b[2] + 1.0f))
264                 return true;
265
266         Portal_MinsBoxPolygon
267         (
268                 0, a[0],
269                 a[0], a[1], a[2],
270                 a[0], b[1], a[2],
271                 a[0], b[1], b[2],
272                 a[0], a[1], b[2]
273         );
274         Portal_MaxsBoxPolygon
275         (
276                 0, b[0],
277                 b[0], b[1], a[2],
278                 b[0], a[1], a[2],
279                 b[0], a[1], b[2],
280                 b[0], b[1], b[2]
281         );
282         Portal_MinsBoxPolygon
283         (
284                 1, a[1],
285                 b[0], a[1], a[2],
286                 a[0], a[1], a[2],
287                 a[0], a[1], b[2],
288                 b[0], a[1], b[2]
289         );
290         Portal_MaxsBoxPolygon
291         (
292                 1, b[1],
293                 a[0], b[1], a[2],
294                 b[0], b[1], a[2],
295                 b[0], b[1], b[2],
296                 a[0], b[1], b[2]
297         );
298         Portal_MinsBoxPolygon
299         (
300                 2, a[2],
301                 a[0], a[1], a[2],
302                 b[0], a[1], a[2],
303                 b[0], b[1], a[2],
304                 a[0], b[1], a[2]
305         );
306         Portal_MaxsBoxPolygon
307         (
308                 2, b[2],
309                 b[0], a[1], b[2],
310                 a[0], a[1], b[2],
311                 a[0], b[1], b[2],
312                 b[0], b[1], b[2]
313         );
314
315         return false;
316 }
317