5 #define MAXRECURSIVEPORTALPLANES 1024
6 #define MAXRECURSIVEPORTALS 256
8 static tinyplane_t portalplanes[MAXRECURSIVEPORTALPLANES];
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 static int Portal_PortalThroughPortalPlanes(tinyplane_t *clipplanes, int clipnumplanes, float *targpoints, int targnumpoints, float *out, int maxpoints)
18 int numpoints = targnumpoints, i, w;
24 memcpy(&portaltemppoints[w][0][0], targpoints, numpoints * 3 * sizeof(float));
25 for (i = 0;i < clipnumplanes && numpoints > 0;i++)
27 PolygonF_Divide(numpoints, &portaltemppoints[w][0][0], clipplanes[i].normal[0], clipplanes[i].normal[1], clipplanes[i].normal[2], clipplanes[i].dist, 1.0f/32.0f, 256, &portaltemppoints[1-w][0][0], &numpoints, 0, NULL, NULL, NULL);
29 numpoints = min(numpoints, 256);
31 numpoints = min(numpoints, maxpoints);
33 memcpy(out, &portaltemppoints[w][0][0], numpoints * 3 * sizeof(float));
37 static int Portal_RecursiveFlowSearch (mleaf_t *leaf, vec3_t eye, int firstclipplane, int numclipplanes)
40 int newpoints, i, prev;
41 vec3_t center, v1, v2;
42 tinyplane_t *newplanes;
44 if (leaf->portalmarkid == portal_markid)
47 // follow portals into other leafs
48 for (p = leaf->portals;p;p = p->next)
50 // only flow through portals facing away from the viewer
51 if (PlaneDiff(eye, (&p->plane)) < 0)
53 newpoints = Portal_PortalThroughPortalPlanes(&portalplanes[firstclipplane], numclipplanes, (float *) p->points, p->numpoints, &portaltemppoints2[0][0], 256);
56 else if (firstclipplane + numclipplanes + newpoints > MAXRECURSIVEPORTALPLANES)
57 ranoutofportalplanes = true;
60 // find the center by averaging
62 for (i = 0;i < newpoints;i++)
63 VectorAdd(center, portaltemppoints2[i], center);
64 // ixtable is a 1.0f / N table
65 VectorScale(center, ixtable[newpoints], center);
66 // calculate the planes, and make sure the polygon can see it's own center
67 newplanes = &portalplanes[firstclipplane + numclipplanes];
68 for (prev = newpoints - 1, i = 0;i < newpoints;prev = i, i++)
70 VectorSubtract(eye, portaltemppoints2[i], v1);
71 VectorSubtract(portaltemppoints2[prev], portaltemppoints2[i], v2);
72 CrossProduct(v1, v2, newplanes[i].normal);
73 VectorNormalize(newplanes[i].normal);
74 newplanes[i].dist = DotProduct(eye, newplanes[i].normal);
75 if (DotProduct(newplanes[i].normal, center) <= newplanes[i].dist)
77 // polygon can't see it's own center, discard and use parent portal
83 if (Portal_RecursiveFlowSearch(p->past, eye, firstclipplane + numclipplanes, newpoints))
88 if (Portal_RecursiveFlowSearch(p->past, eye, firstclipplane, numclipplanes))
98 static void Portal_PolygonRecursiveMarkLeafs(mnode_t *node, float *polypoints, int numpoints)
106 ((mleaf_t *)node)->portalmarkid = portal_markid;
111 for (i = 0, p = polypoints;i < numpoints;i++, p += 3)
113 if (DotProduct(p, node->plane->normal) > node->plane->dist)
118 if (front == numpoints)
120 node = node->children[0];
124 Portal_PolygonRecursiveMarkLeafs(node->children[0], polypoints, numpoints);
126 node = node->children[1];
130 int Portal_CheckPolygon(dp_model_t *model, vec3_t eye, float *polypoints, int numpoints)
132 int i, prev, returnvalue;
134 vec3_t center, v1, v2;
136 // if there is no model, it can not block visibility
137 if (model == NULL || !model->brush.PointInLeaf)
142 Portal_PolygonRecursiveMarkLeafs(model->brush.data_nodes, polypoints, numpoints);
144 eyeleaf = model->brush.PointInLeaf(model, eye);
146 // find the center by averaging
148 for (i = 0;i < numpoints;i++)
149 VectorAdd(center, (&polypoints[i * 3]), center);
150 // ixtable is a 1.0f / N table
151 VectorScale(center, ixtable[numpoints], center);
153 // calculate the planes, and make sure the polygon can see it's own center
154 for (prev = numpoints - 1, i = 0;i < numpoints;prev = i, i++)
156 VectorSubtract(eye, (&polypoints[i * 3]), v1);
157 VectorSubtract((&polypoints[prev * 3]), (&polypoints[i * 3]), v2);
158 CrossProduct(v1, v2, portalplanes[i].normal);
159 VectorNormalize(portalplanes[i].normal);
160 portalplanes[i].dist = DotProduct(eye, portalplanes[i].normal);
161 if (DotProduct(portalplanes[i].normal, center) <= portalplanes[i].dist)
163 // polygon can't see it's own center, discard
168 ranoutofportalplanes = false;
169 ranoutofportals = false;
171 returnvalue = Portal_RecursiveFlowSearch(eyeleaf, eye, 0, numpoints);
173 if (ranoutofportalplanes)
174 Con_Printf("Portal_RecursiveFlowSearch: ran out of %d plane stack when recursing through portals\n", MAXRECURSIVEPORTALPLANES);
176 Con_Printf("Portal_RecursiveFlowSearch: ran out of %d portal stack when recursing through portals\n", MAXRECURSIVEPORTALS);
181 #define Portal_MinsBoxPolygon(axis, axisvalue, x1, y1, z1, x2, y2, z2, x3, y3, z3, x4, y4, z4) \
183 if (eye[(axis)] < ((axisvalue) - 0.5f))\
185 boxpoints[ 0] = x1;boxpoints[ 1] = y1;boxpoints[ 2] = z1;\
186 boxpoints[ 3] = x2;boxpoints[ 4] = y2;boxpoints[ 5] = z2;\
187 boxpoints[ 6] = x3;boxpoints[ 7] = y3;boxpoints[ 8] = z3;\
188 boxpoints[ 9] = x4;boxpoints[10] = y4;boxpoints[11] = z4;\
189 if (Portal_CheckPolygon(model, eye, boxpoints, 4))\
194 #define Portal_MaxsBoxPolygon(axis, axisvalue, x1, y1, z1, x2, y2, z2, x3, y3, z3, x4, y4, z4) \
196 if (eye[(axis)] > ((axisvalue) + 0.5f))\
198 boxpoints[ 0] = x1;boxpoints[ 1] = y1;boxpoints[ 2] = z1;\
199 boxpoints[ 3] = x2;boxpoints[ 4] = y2;boxpoints[ 5] = z2;\
200 boxpoints[ 6] = x3;boxpoints[ 7] = y3;boxpoints[ 8] = z3;\
201 boxpoints[ 9] = x4;boxpoints[10] = y4;boxpoints[11] = z4;\
202 if (Portal_CheckPolygon(model, eye, boxpoints, 4))\
207 int Portal_CheckBox(dp_model_t *model, vec3_t eye, vec3_t a, vec3_t b)
209 if (eye[0] >= (a[0] - 1.0f) && eye[0] < (b[0] + 1.0f)
210 && eye[1] >= (a[1] - 1.0f) && eye[1] < (b[1] + 1.0f)
211 && eye[2] >= (a[2] - 1.0f) && eye[2] < (b[2] + 1.0f))
214 Portal_MinsBoxPolygon
222 Portal_MaxsBoxPolygon
230 Portal_MinsBoxPolygon
238 Portal_MaxsBoxPolygon
246 Portal_MinsBoxPolygon
254 Portal_MaxsBoxPolygon
266 typedef struct portalrecursioninfo_s
269 int numfrustumplanes;
274 unsigned char *surfacepvs;
276 unsigned char *visitingleafpvs; // used to prevent infinite loops
278 unsigned char *leafpvs;
279 unsigned char *shadowtrispvs;
280 unsigned char *lighttrispvs;
283 float *updateleafsmins;
284 float *updateleafsmaxs;
286 portalrecursioninfo_t;
288 static void Portal_RecursiveFlow (portalrecursioninfo_t *info, mleaf_t *leaf, int firstclipplane, int numclipplanes)
291 int newpoints, i, prev;
294 tinyplane_t *newplanes;
295 int leafindex = leaf - info->model->brush.data_leafs;
297 if (CHECKPVSBIT(info->visitingleafpvs, leafindex))
298 return; // recursive loop of leafs (cmc.bsp for megatf coop)
300 SETPVSBIT(info->visitingleafpvs, leafindex);
302 for (i = 0;i < 3;i++)
304 if (info->updateleafsmins && info->updateleafsmins[i] > leaf->mins[i]) info->updateleafsmins[i] = leaf->mins[i];
305 if (info->updateleafsmaxs && info->updateleafsmaxs[i] < leaf->maxs[i]) info->updateleafsmaxs[i] = leaf->maxs[i];
311 if (!CHECKPVSBIT(info->leafpvs, leafindex))
313 SETPVSBIT(info->leafpvs, leafindex);
314 info->leaflist[info->numleafs++] = leafindex;
318 // mark surfaces in leaf that can be seen through portal
319 if (leaf->numleafsurfaces && info->surfacepvs)
321 for (i = 0;i < leaf->numleafsurfaces;i++)
323 int surfaceindex = leaf->firstleafsurface[i];
324 msurface_t *surface = info->model->data_surfaces + surfaceindex;
325 if (BoxesOverlap(surface->mins, surface->maxs, info->boxmins, info->boxmaxs))
327 qboolean insidebox = BoxInsideBox(surface->mins, surface->maxs, info->boxmins, info->boxmaxs);
328 qboolean addedtris = false;
331 const float *vertex3f;
333 vertex3f = info->model->surfmesh.data_vertex3f;
334 elements = (info->model->surfmesh.data_element3i + 3 * surface->num_firsttriangle);
335 for (t = surface->num_firsttriangle, tend = t + surface->num_triangles;t < tend;t++, elements += 3)
337 VectorCopy(vertex3f + elements[0] * 3, v + 0);
338 VectorCopy(vertex3f + elements[1] * 3, v + 3);
339 VectorCopy(vertex3f + elements[2] * 3, v + 6);
340 if (PointInfrontOfTriangle(info->eye, v + 0, v + 3, v + 6)
341 && (insidebox || TriangleOverlapsBox(v, v + 3, v + 6, info->boxmins, info->boxmaxs))
342 && (!info->exact || Portal_PortalThroughPortalPlanes(&portalplanes[firstclipplane], numclipplanes, v, 3, &portaltemppoints2[0][0], 256) > 0))
345 if (info->shadowtrispvs)
346 SETPVSBIT(info->shadowtrispvs, t);
347 if (info->lighttrispvs)
348 SETPVSBIT(info->lighttrispvs, t);
351 if (addedtris && !CHECKPVSBIT(info->surfacepvs, surfaceindex))
353 SETPVSBIT(info->surfacepvs, surfaceindex);
354 info->surfacelist[info->numsurfaces++] = surfaceindex;
360 // follow portals into other leafs
361 for (p = leaf->portals;p;p = p->next)
363 // only flow through portals facing the viewer
364 dist = PlaneDiff(info->eye, (&p->plane));
365 if (dist < 0 && BoxesOverlap(p->past->mins, p->past->maxs, info->boxmins, info->boxmaxs))
367 newpoints = Portal_PortalThroughPortalPlanes(&portalplanes[firstclipplane], numclipplanes, (float *) p->points, p->numpoints, &portaltemppoints2[0][0], 256);
370 else if (firstclipplane + numclipplanes + newpoints > MAXRECURSIVEPORTALPLANES)
371 ranoutofportalplanes = true;
374 // find the center by averaging
376 for (i = 0;i < newpoints;i++)
377 VectorAdd(center, portaltemppoints2[i], center);
378 // ixtable is a 1.0f / N table
379 VectorScale(center, ixtable[newpoints], center);
380 // calculate the planes, and make sure the polygon can see its own center
381 newplanes = &portalplanes[firstclipplane + numclipplanes];
382 for (prev = newpoints - 1, i = 0;i < newpoints;prev = i, i++)
384 TriangleNormal(portaltemppoints2[prev], portaltemppoints2[i], info->eye, newplanes[i].normal);
385 VectorNormalize(newplanes[i].normal);
386 newplanes[i].dist = DotProduct(info->eye, newplanes[i].normal);
387 if (DotProduct(newplanes[i].normal, center) <= newplanes[i].dist)
389 // polygon can't see its own center, discard and use parent portal
394 Portal_RecursiveFlow(info, p->past, firstclipplane + numclipplanes, newpoints);
396 Portal_RecursiveFlow(info, p->past, firstclipplane, numclipplanes);
401 CLEARPVSBIT(info->visitingleafpvs, leafindex);
404 static void Portal_RecursiveFindLeafForFlow(portalrecursioninfo_t *info, mnode_t *node)
408 float f = DotProduct(info->eye, node->plane->normal) - node->plane->dist;
410 Portal_RecursiveFindLeafForFlow(info, node->children[0]);
412 Portal_RecursiveFindLeafForFlow(info, node->children[1]);
416 mleaf_t *leaf = (mleaf_t *)node;
417 if (leaf->clusterindex >= 0)
418 Portal_RecursiveFlow(info, leaf, 0, info->numfrustumplanes);
422 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)
425 portalrecursioninfo_t info;
427 // if there is no model, it can not block visibility
430 Con_Print("Portal_Visibility: NULL model\n");
434 if (!model->brush.data_nodes)
436 Con_Print("Portal_Visibility: not a brush model\n");
440 // put frustum planes (if any) into tinyplane format at start of buffer
441 for (i = 0;i < numfrustumplanes;i++)
443 VectorCopy(frustumplanes[i].normal, portalplanes[i].normal);
444 portalplanes[i].dist = frustumplanes[i].dist;
447 ranoutofportalplanes = false;
448 ranoutofportals = false;
450 VectorCopy(boxmins, info.boxmins);
451 VectorCopy(boxmaxs, info.boxmaxs);
453 info.numsurfaces = 0;
454 info.surfacelist = surfacelist;
455 info.surfacepvs = surfacepvs;
457 info.visitingleafpvs = visitingleafpvs;
458 info.leaflist = leaflist;
459 info.leafpvs = leafpvs;
461 VectorCopy(eye, info.eye);
462 info.numfrustumplanes = numfrustumplanes;
463 info.updateleafsmins = updateleafsmins;
464 info.updateleafsmaxs = updateleafsmaxs;
465 info.shadowtrispvs = shadowtrispvs;
466 info.lighttrispvs = lighttrispvs;
468 Portal_RecursiveFindLeafForFlow(&info, model->brush.data_nodes);
470 if (ranoutofportalplanes)
471 Con_Printf("Portal_RecursiveFlow: ran out of %d plane stack when recursing through portals\n", MAXRECURSIVEPORTALPLANES);
473 Con_Printf("Portal_RecursiveFlow: ran out of %d portal stack when recursing through portals\n", MAXRECURSIVEPORTALS);
474 if (numsurfacespointer)
475 *numsurfacespointer = info.numsurfaces;
477 *numleafspointer = info.numleafs;