4 #define MAXRECURSIVEPORTALPLANES 1024
5 #define MAXRECURSIVEPORTALS 256
7 static tinyplane_t portalplanes[MAXRECURSIVEPORTALPLANES];
8 static int ranoutofportalplanes;
9 static int ranoutofportals;
10 static float portaltemppoints[2][256][3];
11 static float portaltemppoints2[256][3];
12 static int portal_markid = 0;
13 static float boxpoints[4*3];
15 int Portal_ClipPolygonToPlane(float *in, float *out, int inpoints, int maxoutpoints, tinyplane_t *p)
17 int i, outpoints, prevside, side;
18 float *prevpoint, prevdist, dist, dot;
22 // begin with the last point, then enter the loop with the first point as current
23 prevpoint = in + 3 * (inpoints - 1);
24 prevdist = DotProduct(prevpoint, p->normal) - p->dist;
25 prevside = prevdist >= 0 ? SIDE_FRONT : SIDE_BACK;
29 for (;i < inpoints;i++)
37 dist = DotProduct(in, p->normal) - p->dist;
38 side = dist >= 0 ? SIDE_FRONT : SIDE_BACK;
40 if (prevside == SIDE_FRONT)
42 if (outpoints >= maxoutpoints)
44 VectorCopy(prevpoint, out);
47 if (side == SIDE_FRONT)
50 else if (side == SIDE_BACK)
53 // generate a split point
54 if (outpoints >= maxoutpoints)
56 dot = prevdist / (prevdist - dist);
57 out[0] = prevpoint[0] + dot * (in[0] - prevpoint[0]);
58 out[1] = prevpoint[1] + dot * (in[1] - prevpoint[1]);
59 out[2] = prevpoint[2] + dot * (in[2] - prevpoint[2]);
68 int Portal_PortalThroughPortalPlanes(tinyplane_t *clipplanes, int clipnumplanes, float *targpoints, int targnumpoints, float *out, int maxpoints)
71 if (targnumpoints < 3)
75 numpoints = targnumpoints;
76 memcpy(&portaltemppoints[0][0][0], targpoints, numpoints * 3 * sizeof(float));
77 for (i = 0;i < clipnumplanes;i++)
79 numpoints = Portal_ClipPolygonToPlane(&portaltemppoints[0][0][0], &portaltemppoints[1][0][0], numpoints, 256, clipplanes + i);
82 memcpy(&portaltemppoints[0][0][0], &portaltemppoints[1][0][0], numpoints * 3 * sizeof(float));
84 if (numpoints > maxpoints)
86 memcpy(out, &portaltemppoints[0][0][0], numpoints * 3 * sizeof(float));
90 int Portal_RecursiveFlowSearch (mleaf_t *leaf, vec3_t eye, int firstclipplane, int numclipplanes)
93 int newpoints, i, prev;
94 vec3_t center, v1, v2;
95 tinyplane_t *newplanes;
97 if (leaf->portalmarkid == portal_markid)
100 // follow portals into other leafs
101 for (p = leaf->portals;p;p = p->next)
103 // only flow through portals facing away from the viewer
104 if (PlaneDiff(eye, (&p->plane)) < 0)
106 newpoints = Portal_PortalThroughPortalPlanes(&portalplanes[firstclipplane], numclipplanes, (float *) p->points, p->numpoints, &portaltemppoints2[0][0], 256);
109 else if (firstclipplane + numclipplanes + newpoints > MAXRECURSIVEPORTALPLANES)
110 ranoutofportalplanes = true;
113 // find the center by averaging
115 for (i = 0;i < newpoints;i++)
116 VectorAdd(center, portaltemppoints2[i], center);
117 // ixtable is a 1.0f / N table
118 VectorScale(center, ixtable[newpoints], center);
119 // calculate the planes, and make sure the polygon can see it's own center
120 newplanes = &portalplanes[firstclipplane + numclipplanes];
121 for (prev = newpoints - 1, i = 0;i < newpoints;prev = i, i++)
123 VectorSubtract(eye, portaltemppoints2[i], v1);
124 VectorSubtract(portaltemppoints2[prev], portaltemppoints2[i], v2);
125 CrossProduct(v1, v2, newplanes[i].normal);
126 VectorNormalizeFast(newplanes[i].normal);
127 newplanes[i].dist = DotProduct(eye, newplanes[i].normal);
128 if (DotProduct(newplanes[i].normal, center) <= newplanes[i].dist)
130 // polygon can't see it's own center, discard and use parent portal
136 if (Portal_RecursiveFlowSearch(p->past, eye, firstclipplane + numclipplanes, newpoints))
141 if (Portal_RecursiveFlowSearch(p->past, eye, firstclipplane, numclipplanes))
151 void Portal_PolygonRecursiveMarkLeafs(mnode_t *node, float *polypoints, int numpoints)
159 ((mleaf_t *)node)->portalmarkid = portal_markid;
164 for (i = 0, p = polypoints;i < numpoints;i++, p += 3)
166 if (DotProduct(p, node->plane->normal) > node->plane->dist)
171 if (front == numpoints)
173 node = node->children[0];
177 Portal_PolygonRecursiveMarkLeafs(node->children[0], polypoints, numpoints);
179 node = node->children[1];
183 int Portal_CheckPolygon(model_t *model, vec3_t eye, float *polypoints, int numpoints)
185 int i, prev, returnvalue;
187 vec3_t center, v1, v2;
189 // if there is no model, it can not block visibility
190 if (model == NULL || !model->brush.PointInLeaf)
195 Mod_CheckLoaded(model);
196 Portal_PolygonRecursiveMarkLeafs(model->brush.data_nodes, polypoints, numpoints);
198 eyeleaf = model->brush.PointInLeaf(model, eye);
200 // find the center by averaging
202 for (i = 0;i < numpoints;i++)
203 VectorAdd(center, (&polypoints[i * 3]), center);
204 // ixtable is a 1.0f / N table
205 VectorScale(center, ixtable[numpoints], center);
207 // calculate the planes, and make sure the polygon can see it's own center
208 for (prev = numpoints - 1, i = 0;i < numpoints;prev = i, i++)
210 VectorSubtract(eye, (&polypoints[i * 3]), v1);
211 VectorSubtract((&polypoints[prev * 3]), (&polypoints[i * 3]), v2);
212 CrossProduct(v1, v2, portalplanes[i].normal);
213 VectorNormalizeFast(portalplanes[i].normal);
214 portalplanes[i].dist = DotProduct(eye, portalplanes[i].normal);
215 if (DotProduct(portalplanes[i].normal, center) <= portalplanes[i].dist)
217 // polygon can't see it's own center, discard
222 ranoutofportalplanes = false;
223 ranoutofportals = false;
225 returnvalue = Portal_RecursiveFlowSearch(eyeleaf, eye, 0, numpoints);
227 if (ranoutofportalplanes)
228 Con_Printf("Portal_RecursiveFlowSearch: ran out of %d plane stack when recursing through portals\n", MAXRECURSIVEPORTALPLANES);
230 Con_Printf("Portal_RecursiveFlowSearch: ran out of %d portal stack when recursing through portals\n", MAXRECURSIVEPORTALS);
235 #define Portal_MinsBoxPolygon(axis, axisvalue, x1, y1, z1, x2, y2, z2, x3, y3, z3, x4, y4, z4) \
237 if (eye[(axis)] < ((axisvalue) - 0.5f))\
239 boxpoints[ 0] = x1;boxpoints[ 1] = y1;boxpoints[ 2] = z1;\
240 boxpoints[ 3] = x2;boxpoints[ 4] = y2;boxpoints[ 5] = z2;\
241 boxpoints[ 6] = x3;boxpoints[ 7] = y3;boxpoints[ 8] = z3;\
242 boxpoints[ 9] = x4;boxpoints[10] = y4;boxpoints[11] = z4;\
243 if (Portal_CheckPolygon(model, eye, boxpoints, 4))\
248 #define Portal_MaxsBoxPolygon(axis, axisvalue, x1, y1, z1, x2, y2, z2, x3, y3, z3, x4, y4, z4) \
250 if (eye[(axis)] > ((axisvalue) + 0.5f))\
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))\
261 int Portal_CheckBox(model_t *model, vec3_t eye, vec3_t a, vec3_t b)
263 if (eye[0] >= (a[0] - 1.0f) && eye[0] < (b[0] + 1.0f)
264 && eye[1] >= (a[1] - 1.0f) && eye[1] < (b[1] + 1.0f)
265 && eye[2] >= (a[2] - 1.0f) && eye[2] < (b[2] + 1.0f))
268 Portal_MinsBoxPolygon
276 Portal_MaxsBoxPolygon
284 Portal_MinsBoxPolygon
292 Portal_MaxsBoxPolygon
300 Portal_MinsBoxPolygon
308 Portal_MaxsBoxPolygon
320 vec3_t trianglepoints[3];
322 typedef struct portalrecursioninfo_s
325 int numfrustumplanes;
336 float *updateleafsmins;
337 float *updateleafsmaxs;
339 portalrecursioninfo_t;
341 void Portal_RecursiveFlow (portalrecursioninfo_t *info, mleaf_t *leaf, int firstclipplane, int numclipplanes)
344 int newpoints, i, prev;
346 vec3_t center, v1, v2;
347 tinyplane_t *newplanes;
349 for (i = 0;i < 3;i++)
351 if (info->updateleafsmins && info->updateleafsmins[i] > leaf->mins[i]) info->updateleafsmins[i] = leaf->mins[i];
352 if (info->updateleafsmaxs && info->updateleafsmaxs[i] < leaf->maxs[i]) info->updateleafsmaxs[i] = leaf->maxs[i];
358 int leafindex = leaf - info->model->brush.data_leafs;
359 if (!CHECKPVSBIT(info->leafpvs, leafindex))
361 SETPVSBIT(info->leafpvs, leafindex);
362 info->leaflist[info->numleafs++] = leafindex;
366 // mark surfaces in leaf that can be seen through portal
367 if (leaf->numleafsurfaces && info->surfacepvs)
369 for (i = 0;i < leaf->numleafsurfaces;i++)
371 int surfaceindex = leaf->firstleafsurface[i];
372 if (!CHECKPVSBIT(info->surfacepvs, surfaceindex))
374 msurface_t *surface = info->model->brush.data_surfaces + surfaceindex;
375 if (BoxesOverlap(surface->mins, surface->maxs, info->boxmins, info->boxmaxs))
381 const float *vertex3f;
382 float v[9], trimins[3], trimaxs[3];
383 vertex3f = surface->groupmesh->data_vertex3f;
384 elements = (surface->groupmesh->data_element3i + 3 * surface->num_firsttriangle);
385 for (j = 0;j < surface->num_triangles;j++, elements += 3)
387 VectorCopy(vertex3f + elements[0] * 3, v + 0);
388 VectorCopy(vertex3f + elements[1] * 3, v + 3);
389 VectorCopy(vertex3f + elements[2] * 3, v + 6);
390 if (PointInfrontOfTriangle(info->eye, v + 0, v + 3, v + 6))
392 trimins[0] = min(v[0], min(v[3], v[6]));
393 trimaxs[0] = max(v[0], max(v[3], v[6]));
394 trimins[1] = min(v[1], min(v[4], v[7]));
395 trimaxs[1] = max(v[1], max(v[4], v[7]));
396 trimins[2] = min(v[2], min(v[5], v[8]));
397 trimaxs[2] = max(v[2], max(v[5], v[8]));
398 if (BoxesOverlap(trimins, trimaxs, info->boxmins, info->boxmaxs) && Portal_PortalThroughPortalPlanes(&portalplanes[firstclipplane], numclipplanes, v, 3, &portaltemppoints2[0][0], 256) >= 3)
400 SETPVSBIT(info->surfacepvs, surfaceindex);
401 info->surfacelist[info->numsurfaces++] = surfaceindex;
409 SETPVSBIT(info->surfacepvs, surfaceindex);
410 info->surfacelist[info->numsurfaces++] = surfaceindex;
417 // follow portals into other leafs
418 for (p = leaf->portals;p;p = p->next)
420 // only flow through portals facing the viewer
421 dist = PlaneDiff(info->eye, (&p->plane));
422 if (dist < 0 && BoxesOverlap(p->past->mins, p->past->maxs, info->boxmins, info->boxmaxs))
424 newpoints = Portal_PortalThroughPortalPlanes(&portalplanes[firstclipplane], numclipplanes, (float *) p->points, p->numpoints, &portaltemppoints2[0][0], 256);
427 else if (firstclipplane + numclipplanes + newpoints > MAXRECURSIVEPORTALPLANES)
428 ranoutofportalplanes = true;
431 // find the center by averaging
433 for (i = 0;i < newpoints;i++)
434 VectorAdd(center, portaltemppoints2[i], center);
435 // ixtable is a 1.0f / N table
436 VectorScale(center, ixtable[newpoints], center);
437 // calculate the planes, and make sure the polygon can see it's own center
438 newplanes = &portalplanes[firstclipplane + numclipplanes];
439 for (prev = newpoints - 1, i = 0;i < newpoints;prev = i, i++)
441 VectorSubtract(portaltemppoints2[prev], portaltemppoints2[i], v1);
442 VectorSubtract(info->eye, portaltemppoints2[i], v2);
443 CrossProduct(v1, v2, newplanes[i].normal);
444 VectorNormalizeFast(newplanes[i].normal);
445 newplanes[i].dist = DotProduct(info->eye, newplanes[i].normal);
446 if (DotProduct(newplanes[i].normal, center) <= newplanes[i].dist)
448 // polygon can't see it's own center, discard and use parent portal
453 Portal_RecursiveFlow(info, p->past, firstclipplane + numclipplanes, newpoints);
455 Portal_RecursiveFlow(info, p->past, firstclipplane, numclipplanes);
461 void Portal_RecursiveFindLeafForFlow(portalrecursioninfo_t *info, mnode_t *node)
465 float f = DotProduct(info->eye, node->plane->normal) - node->plane->dist;
467 Portal_RecursiveFindLeafForFlow(info, node->children[0]);
469 Portal_RecursiveFindLeafForFlow(info, node->children[1]);
473 mleaf_t *leaf = (mleaf_t *)node;
475 Portal_RecursiveFlow(info, leaf, 0, info->numfrustumplanes);
479 void Portal_Visibility(model_t *model, const vec3_t eye, int *leaflist, qbyte *leafpvs, int *numleafspointer, int *surfacelist, qbyte *surfacepvs, int *numsurfacespointer, const mplane_t *frustumplanes, int numfrustumplanes, int exact, const float *boxmins, const float *boxmaxs, float *updateleafsmins, float *updateleafsmaxs)
482 portalrecursioninfo_t info;
484 // if there is no model, it can not block visibility
487 Con_Print("Portal_Visibility: NULL model\n");
491 Mod_CheckLoaded(model);
493 if (!model->brush.num_portals)
495 Con_Print("Portal_Visibility: not a brush model\n");
499 // put frustum planes (if any) into tinyplane format at start of buffer
500 for (i = 0;i < numfrustumplanes;i++)
502 VectorCopy(frustumplanes[i].normal, portalplanes[i].normal);
503 portalplanes[i].dist = frustumplanes[i].dist;
506 ranoutofportalplanes = false;
507 ranoutofportals = false;
509 VectorCopy(boxmins, info.boxmins);
510 VectorCopy(boxmaxs, info.boxmaxs);
512 info.numsurfaces = 0;
513 info.surfacelist = surfacelist;
514 info.surfacepvs = surfacepvs;
516 info.leaflist = leaflist;
517 info.leafpvs = leafpvs;
519 VectorCopy(eye, info.eye);
520 info.numfrustumplanes = numfrustumplanes;
521 info.updateleafsmins = updateleafsmins;
522 info.updateleafsmaxs = updateleafsmaxs;
524 Portal_RecursiveFindLeafForFlow(&info, model->brush.data_nodes);
526 if (ranoutofportalplanes)
527 Con_Printf("Portal_RecursiveFlow: ran out of %d plane stack when recursing through portals\n", MAXRECURSIVEPORTALPLANES);
529 Con_Printf("Portal_RecursiveFlow: ran out of %d portal stack when recursing through portals\n", MAXRECURSIVEPORTALS);
530 if (numsurfacespointer)
531 *numsurfacespointer = info.numsurfaces;
533 *numleafspointer = info.numleafs;