+typedef struct portalrecursioninfo_s
+{
+ int exact;
+ int numfrustumplanes;
+ vec3_t boxmins;
+ vec3_t boxmaxs;
+ int numsurfaces;
+ int *surfacelist;
+ unsigned char *surfacepvs;
+ int numleafs;
+ unsigned char *visitingleafpvs; // used to prevent infinite loops
+ int *leaflist;
+ unsigned char *leafpvs;
+ unsigned char *shadowtrispvs;
+ unsigned char *lighttrispvs;
+ dp_model_t *model;
+ vec3_t eye;
+ float *updateleafsmins;
+ float *updateleafsmaxs;
+}
+portalrecursioninfo_t;
+
+static void Portal_RecursiveFlow (portalrecursioninfo_t *info, mleaf_t *leaf, int firstclipplane, int numclipplanes)
+{
+ mportal_t *p;
+ int newpoints, i, prev;
+ float dist;
+ vec3_t center;
+ tinyplane_t *newplanes;
+ int leafindex = leaf - info->model->brush.data_leafs;
+
+ if (CHECKPVSBIT(info->visitingleafpvs, leafindex))
+ return; // recursive loop of leafs (cmc.bsp for megatf coop)
+
+ SETPVSBIT(info->visitingleafpvs, leafindex);
+
+ for (i = 0;i < 3;i++)
+ {
+ if (info->updateleafsmins && info->updateleafsmins[i] > leaf->mins[i]) info->updateleafsmins[i] = leaf->mins[i];
+ if (info->updateleafsmaxs && info->updateleafsmaxs[i] < leaf->maxs[i]) info->updateleafsmaxs[i] = leaf->maxs[i];
+ }
+
+
+ if (info->leafpvs)
+ {
+ if (!CHECKPVSBIT(info->leafpvs, leafindex))
+ {
+ SETPVSBIT(info->leafpvs, leafindex);
+ info->leaflist[info->numleafs++] = leafindex;
+ }
+ }
+
+ // mark surfaces in leaf that can be seen through portal
+ if (leaf->numleafsurfaces && info->surfacepvs)
+ {
+ for (i = 0;i < leaf->numleafsurfaces;i++)
+ {
+ int surfaceindex = leaf->firstleafsurface[i];
+ msurface_t *surface = info->model->data_surfaces + surfaceindex;
+ if (BoxesOverlap(surface->mins, surface->maxs, info->boxmins, info->boxmaxs))
+ {
+ qboolean insidebox = BoxInsideBox(surface->mins, surface->maxs, info->boxmins, info->boxmaxs);
+ qboolean addedtris = false;
+ int t, tend;
+ const int *elements;
+ const float *vertex3f;
+ float v[9];
+ vertex3f = info->model->surfmesh.data_vertex3f;
+ elements = (info->model->surfmesh.data_element3i + 3 * surface->num_firsttriangle);
+ for (t = surface->num_firsttriangle, tend = t + surface->num_triangles;t < tend;t++, elements += 3)
+ {
+ VectorCopy(vertex3f + elements[0] * 3, v + 0);
+ VectorCopy(vertex3f + elements[1] * 3, v + 3);
+ VectorCopy(vertex3f + elements[2] * 3, v + 6);
+ if (PointInfrontOfTriangle(info->eye, v + 0, v + 3, v + 6)
+ && (insidebox || TriangleOverlapsBox(v, v + 3, v + 6, info->boxmins, info->boxmaxs))
+ && (!info->exact || Portal_PortalThroughPortalPlanes(&portalplanes[firstclipplane], numclipplanes, v, 3, &portaltemppoints2[0][0], 256) > 0))
+ {
+ addedtris = true;
+ if (info->shadowtrispvs)
+ SETPVSBIT(info->shadowtrispvs, t);
+ if (info->lighttrispvs)
+ SETPVSBIT(info->lighttrispvs, t);
+ }
+ }
+ if (addedtris && !CHECKPVSBIT(info->surfacepvs, surfaceindex))
+ {
+ SETPVSBIT(info->surfacepvs, surfaceindex);
+ info->surfacelist[info->numsurfaces++] = surfaceindex;
+ }
+ }
+ }
+ }
+
+ // follow portals into other leafs
+ for (p = leaf->portals;p;p = p->next)
+ {
+ // only flow through portals facing the viewer
+ dist = PlaneDiff(info->eye, (&p->plane));
+ if (dist < 0 && BoxesOverlap(p->past->mins, p->past->maxs, info->boxmins, info->boxmaxs))
+ {
+ newpoints = Portal_PortalThroughPortalPlanes(&portalplanes[firstclipplane], numclipplanes, (float *) p->points, p->numpoints, &portaltemppoints2[0][0], 256);
+ if (newpoints < 3)
+ continue;
+ else if (firstclipplane + numclipplanes + newpoints > MAXRECURSIVEPORTALPLANES)
+ ranoutofportalplanes = true;
+ else
+ {
+ // find the center by averaging
+ VectorClear(center);
+ for (i = 0;i < newpoints;i++)
+ VectorAdd(center, portaltemppoints2[i], center);
+ // ixtable is a 1.0f / N table
+ VectorScale(center, ixtable[newpoints], center);
+ // calculate the planes, and make sure the polygon can see its own center
+ newplanes = &portalplanes[firstclipplane + numclipplanes];
+ for (prev = newpoints - 1, i = 0;i < newpoints;prev = i, i++)
+ {
+ TriangleNormal(portaltemppoints2[prev], portaltemppoints2[i], info->eye, newplanes[i].normal);
+ VectorNormalize(newplanes[i].normal);
+ newplanes[i].dist = DotProduct(info->eye, newplanes[i].normal);
+ if (DotProduct(newplanes[i].normal, center) <= newplanes[i].dist)
+ {
+ // polygon can't see its own center, discard and use parent portal
+ break;
+ }
+ }
+ if (i == newpoints)
+ Portal_RecursiveFlow(info, p->past, firstclipplane + numclipplanes, newpoints);
+ else
+ Portal_RecursiveFlow(info, p->past, firstclipplane, numclipplanes);
+ }
+ }
+ }
+
+ CLEARPVSBIT(info->visitingleafpvs, leafindex);
+}
+
+static void Portal_RecursiveFindLeafForFlow(portalrecursioninfo_t *info, mnode_t *node)
+{
+ if (node->plane)
+ {
+ float f = DotProduct(info->eye, node->plane->normal) - node->plane->dist;
+ if (f > -0.1)
+ Portal_RecursiveFindLeafForFlow(info, node->children[0]);
+ if (f < 0.1)
+ Portal_RecursiveFindLeafForFlow(info, node->children[1]);
+ }
+ else
+ {
+ mleaf_t *leaf = (mleaf_t *)node;
+ if (leaf->clusterindex >= 0)
+ Portal_RecursiveFlow(info, leaf, 0, info->numfrustumplanes);
+ }
+}
+
+void Portal_Visibility(dp_model_t *model, const vec3_t eye, int *leaflist, unsigned char *leafpvs, int *numleafspointer, int *surfacelist, unsigned char *surfacepvs, int *numsurfacespointer, const mplane_t *frustumplanes, int numfrustumplanes, int exact, const float *boxmins, const float *boxmaxs, float *updateleafsmins, float *updateleafsmaxs, unsigned char *shadowtrispvs, unsigned char *lighttrispvs, unsigned char *visitingleafpvs)
+{
+ int i;
+ portalrecursioninfo_t info;
+
+ // if there is no model, it can not block visibility
+ if (model == NULL)
+ {
+ Con_Print("Portal_Visibility: NULL model\n");
+ return;
+ }
+
+ if (!model->brush.data_nodes)
+ {
+ Con_Print("Portal_Visibility: not a brush model\n");
+ return;
+ }
+
+ // put frustum planes (if any) into tinyplane format at start of buffer
+ for (i = 0;i < numfrustumplanes;i++)
+ {
+ VectorCopy(frustumplanes[i].normal, portalplanes[i].normal);
+ portalplanes[i].dist = frustumplanes[i].dist;
+ }
+
+ ranoutofportalplanes = false;
+ ranoutofportals = false;
+
+ VectorCopy(boxmins, info.boxmins);
+ VectorCopy(boxmaxs, info.boxmaxs);
+ info.exact = exact;
+ info.numsurfaces = 0;
+ info.surfacelist = surfacelist;
+ info.surfacepvs = surfacepvs;
+ info.numleafs = 0;
+ info.visitingleafpvs = visitingleafpvs;
+ info.leaflist = leaflist;
+ info.leafpvs = leafpvs;
+ info.model = model;
+ VectorCopy(eye, info.eye);
+ info.numfrustumplanes = numfrustumplanes;
+ info.updateleafsmins = updateleafsmins;
+ info.updateleafsmaxs = updateleafsmaxs;
+ info.shadowtrispvs = shadowtrispvs;
+ info.lighttrispvs = lighttrispvs;
+
+ Portal_RecursiveFindLeafForFlow(&info, model->brush.data_nodes);
+
+ if (ranoutofportalplanes)
+ Con_Printf("Portal_RecursiveFlow: ran out of %d plane stack when recursing through portals\n", MAXRECURSIVEPORTALPLANES);
+ if (ranoutofportals)
+ Con_Printf("Portal_RecursiveFlow: ran out of %d portal stack when recursing through portals\n", MAXRECURSIVEPORTALS);
+ if (numsurfacespointer)
+ *numsurfacespointer = info.numsurfaces;
+ if (numleafspointer)
+ *numleafspointer = info.numleafs;
+}
+