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