4 #define MAXRECURSIVEPORTALPLANES 1024
5 #define MAXRECURSIVEPORTALS 256
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];
16 int Portal_ClipPolygonToPlane(float *in, float *out, int inpoints, int maxoutpoints, tinyplane_t *p)
18 int i, outpoints, prevside, side;
19 float *prevpoint, prevdist, dist, dot;
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;
30 for (;i < inpoints;i++)
38 dist = DotProduct(in, p->normal) - p->dist;
39 side = dist >= 0 ? SIDE_FRONT : SIDE_BACK;
41 if (prevside == SIDE_FRONT)
43 if (outpoints >= maxoutpoints)
45 VectorCopy(prevpoint, out);
48 if (side == SIDE_FRONT)
51 else if (side == SIDE_BACK)
54 // generate a split point
55 if (outpoints >= maxoutpoints)
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]);
69 int Portal_PortalThroughPortalPlanes(tinyplane_t *clipplanes, int clipnumplanes, float *targpoints, int targnumpoints, float *out, int maxpoints)
72 if (targnumpoints < 3)
76 numpoints = targnumpoints;
77 memcpy(&portaltemppoints[0][0][0], targpoints, numpoints * 3 * sizeof(float));
78 for (i = 0;i < clipnumplanes;i++)
80 numpoints = Portal_ClipPolygonToPlane(&portaltemppoints[0][0][0], &portaltemppoints[1][0][0], numpoints, 256, clipplanes + i);
83 memcpy(&portaltemppoints[0][0][0], &portaltemppoints[1][0][0], numpoints * 3 * sizeof(float));
85 if (numpoints > maxpoints)
87 memcpy(out, &portaltemppoints[1][0][0], numpoints * 3 * sizeof(float));
91 int Portal_RecursiveFlowSearch (mleaf_t *leaf, vec3_t eye, int firstclipplane, int numclipplanes)
94 int newpoints, i, prev;
95 vec3_t center, v1, v2;
96 tinyplane_t *newplanes;
98 if (leaf->portalmarkid == portal_markid)
101 // follow portals into other leafs
102 for (p = leaf->portals;p;p = p->next)
104 // only flow through portals facing away from the viewer
105 if (PlaneDiff(eye, (&p->plane)) < 0)
107 newpoints = Portal_PortalThroughPortalPlanes(&portalplanes[firstclipplane], numclipplanes, (float *) p->points, p->numpoints, &portaltemppoints2[0][0], 256);
110 else if (firstclipplane + numclipplanes + newpoints > MAXRECURSIVEPORTALPLANES)
111 ranoutofportalplanes = true;
114 // find the center by averaging
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++)
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)
131 // polygon can't see it's own center, discard and use parent portal
137 if (Portal_RecursiveFlowSearch(p->past, eye, firstclipplane + numclipplanes, newpoints))
142 if (Portal_RecursiveFlowSearch(p->past, eye, firstclipplane, numclipplanes))
152 void Portal_PolygonRecursiveMarkLeafs(mnode_t *node, float *polypoints, int numpoints)
158 if (node->contents < 0)
160 ((mleaf_t *)node)->portalmarkid = portal_markid;
165 for (i = 0, p = polypoints;i < numpoints;i++, p += 3)
167 if (DotProduct(p, node->plane->normal) > node->plane->dist)
172 if (front == numpoints)
174 node = node->children[0];
178 Portal_PolygonRecursiveMarkLeafs(node->children[0], polypoints, numpoints);
180 node = node->children[1];
184 int Portal_CheckPolygon(model_t *model, vec3_t eye, float *polypoints, int numpoints)
186 int i, prev, returnvalue;
188 vec3_t center, v1, v2;
190 // if there is no model, it can not block visibility
196 Mod_CheckLoaded(model);
197 Portal_PolygonRecursiveMarkLeafs(model->nodes, polypoints, numpoints);
199 eyeleaf = Mod_PointInLeaf(eye, model);
201 // find the center by averaging
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);
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++)
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)
218 // polygon can't see it's own center, discard
223 portalplanecount = 0;
224 ranoutofportalplanes = false;
225 ranoutofportals = false;
227 returnvalue = Portal_RecursiveFlowSearch(eyeleaf, eye, 0, numpoints);
229 if (ranoutofportalplanes)
230 Con_Printf("Portal_RecursiveFlowSearch: ran out of %d plane stack when recursing through portals\n", MAXRECURSIVEPORTALPLANES);
232 Con_Printf("Portal_RecursiveFlowSearch: ran out of %d portal stack when recursing through portals\n", MAXRECURSIVEPORTALS);
237 #define Portal_MinsBoxPolygon(axis, axisvalue, x1, y1, z1, x2, y2, z2, x3, y3, z3, x4, y4, z4) \
239 if (eye[(axis)] < ((axisvalue) - 0.5f))\
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))\
250 #define Portal_MaxsBoxPolygon(axis, axisvalue, x1, y1, z1, x2, y2, z2, x3, y3, z3, x4, y4, z4) \
252 if (eye[(axis)] > ((axisvalue) + 0.5f))\
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))\
263 int Portal_CheckBox(model_t *model, vec3_t eye, vec3_t a, vec3_t b)
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))
270 Portal_MinsBoxPolygon
278 Portal_MaxsBoxPolygon
286 Portal_MinsBoxPolygon
294 Portal_MaxsBoxPolygon
302 Portal_MinsBoxPolygon
310 Portal_MaxsBoxPolygon