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