6 #define MAXRECURSIVEPORTALPLANES 1024
7 #define MAXRECURSIVEPORTALS 256
9 static tinyplane_t portalplanes[MAXRECURSIVEPORTALPLANES];
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 static float boxpoints[4*3];
17 static int Portal_PortalThroughPortalPlanes(tinyplane_t *clipplanes, int clipnumplanes, float *targpoints, int targnumpoints, float *out, int maxpoints)
19 int numpoints = targnumpoints, i, w;
25 memcpy(&portaltemppoints[w][0][0], targpoints, numpoints * 3 * sizeof(float));
26 for (i = 0;i < clipnumplanes && numpoints > 0;i++)
28 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);
30 numpoints = min(numpoints, 256);
32 numpoints = min(numpoints, maxpoints);
34 memcpy(out, &portaltemppoints[w][0][0], numpoints * 3 * sizeof(float));
38 static int Portal_RecursiveFlowSearch (mleaf_t *leaf, vec3_t eye, int firstclipplane, int numclipplanes)
41 int newpoints, i, prev;
42 vec3_t center, v1, v2;
43 tinyplane_t *newplanes;
45 if (leaf->portalmarkid == portal_markid)
48 // follow portals into other leafs
49 for (p = leaf->portals;p;p = p->next)
51 // only flow through portals facing away from the viewer
52 if (PlaneDiff(eye, (&p->plane)) < 0)
54 newpoints = Portal_PortalThroughPortalPlanes(&portalplanes[firstclipplane], numclipplanes, (float *) p->points, p->numpoints, &portaltemppoints2[0][0], 256);
57 else if (firstclipplane + numclipplanes + newpoints > MAXRECURSIVEPORTALPLANES)
58 ranoutofportalplanes = true;
61 // find the center by averaging
63 for (i = 0;i < newpoints;i++)
64 VectorAdd(center, portaltemppoints2[i], center);
65 // ixtable is a 1.0f / N table
66 VectorScale(center, ixtable[newpoints], center);
67 // calculate the planes, and make sure the polygon can see it's own center
68 newplanes = &portalplanes[firstclipplane + numclipplanes];
69 for (prev = newpoints - 1, i = 0;i < newpoints;prev = i, i++)
71 VectorSubtract(eye, portaltemppoints2[i], v1);
72 VectorSubtract(portaltemppoints2[prev], portaltemppoints2[i], v2);
73 CrossProduct(v1, v2, newplanes[i].normal);
74 VectorNormalize(newplanes[i].normal);
75 newplanes[i].dist = DotProduct(eye, newplanes[i].normal);
76 if (DotProduct(newplanes[i].normal, center) <= newplanes[i].dist)
78 // polygon can't see it's own center, discard and use parent portal
84 if (Portal_RecursiveFlowSearch(p->past, eye, firstclipplane + numclipplanes, newpoints))
89 if (Portal_RecursiveFlowSearch(p->past, eye, firstclipplane, numclipplanes))
99 static void Portal_PolygonRecursiveMarkLeafs(mnode_t *node, float *polypoints, int numpoints)
107 ((mleaf_t *)node)->portalmarkid = portal_markid;
112 for (i = 0, p = polypoints;i < numpoints;i++, p += 3)
114 if (DotProduct(p, node->plane->normal) > node->plane->dist)
119 if (front == numpoints)
121 node = node->children[0];
125 Portal_PolygonRecursiveMarkLeafs(node->children[0], polypoints, numpoints);
127 node = node->children[1];
131 int Portal_CheckPolygon(dp_model_t *model, vec3_t eye, float *polypoints, int numpoints)
133 int i, prev, returnvalue;
135 vec3_t center, v1, v2;
137 // if there is no model, it can not block visibility
138 if (model == NULL || !model->brush.PointInLeaf)
143 Portal_PolygonRecursiveMarkLeafs(model->brush.data_nodes, polypoints, numpoints);
145 eyeleaf = model->brush.PointInLeaf(model, eye);
147 // find the center by averaging
149 for (i = 0;i < numpoints;i++)
150 VectorAdd(center, (&polypoints[i * 3]), center);
151 // ixtable is a 1.0f / N table
152 VectorScale(center, ixtable[numpoints], center);
154 // calculate the planes, and make sure the polygon can see it's own center
155 for (prev = numpoints - 1, i = 0;i < numpoints;prev = i, i++)
157 VectorSubtract(eye, (&polypoints[i * 3]), v1);
158 VectorSubtract((&polypoints[prev * 3]), (&polypoints[i * 3]), v2);
159 CrossProduct(v1, v2, portalplanes[i].normal);
160 VectorNormalize(portalplanes[i].normal);
161 portalplanes[i].dist = DotProduct(eye, portalplanes[i].normal);
162 if (DotProduct(portalplanes[i].normal, center) <= portalplanes[i].dist)
164 // polygon can't see it's own center, discard
169 ranoutofportalplanes = false;
170 ranoutofportals = false;
172 returnvalue = Portal_RecursiveFlowSearch(eyeleaf, eye, 0, numpoints);
174 if (ranoutofportalplanes)
175 Con_Printf("Portal_RecursiveFlowSearch: ran out of %d plane stack when recursing through portals\n", MAXRECURSIVEPORTALPLANES);
177 Con_Printf("Portal_RecursiveFlowSearch: ran out of %d portal stack when recursing through portals\n", MAXRECURSIVEPORTALS);
182 #define Portal_MinsBoxPolygon(axis, axisvalue, x1, y1, z1, x2, y2, z2, x3, y3, z3, x4, y4, z4) \
184 if (eye[(axis)] < ((axisvalue) - 0.5f))\
186 boxpoints[ 0] = x1;boxpoints[ 1] = y1;boxpoints[ 2] = z1;\
187 boxpoints[ 3] = x2;boxpoints[ 4] = y2;boxpoints[ 5] = z2;\
188 boxpoints[ 6] = x3;boxpoints[ 7] = y3;boxpoints[ 8] = z3;\
189 boxpoints[ 9] = x4;boxpoints[10] = y4;boxpoints[11] = z4;\
190 if (Portal_CheckPolygon(model, eye, boxpoints, 4))\
195 #define Portal_MaxsBoxPolygon(axis, axisvalue, x1, y1, z1, x2, y2, z2, x3, y3, z3, x4, y4, z4) \
197 if (eye[(axis)] > ((axisvalue) + 0.5f))\
199 boxpoints[ 0] = x1;boxpoints[ 1] = y1;boxpoints[ 2] = z1;\
200 boxpoints[ 3] = x2;boxpoints[ 4] = y2;boxpoints[ 5] = z2;\
201 boxpoints[ 6] = x3;boxpoints[ 7] = y3;boxpoints[ 8] = z3;\
202 boxpoints[ 9] = x4;boxpoints[10] = y4;boxpoints[11] = z4;\
203 if (Portal_CheckPolygon(model, eye, boxpoints, 4))\
208 int Portal_CheckBox(dp_model_t *model, vec3_t eye, vec3_t a, vec3_t b)
210 if (eye[0] >= (a[0] - 1.0f) && eye[0] < (b[0] + 1.0f)
211 && eye[1] >= (a[1] - 1.0f) && eye[1] < (b[1] + 1.0f)
212 && eye[2] >= (a[2] - 1.0f) && eye[2] < (b[2] + 1.0f))
215 Portal_MinsBoxPolygon
223 Portal_MaxsBoxPolygon
231 Portal_MinsBoxPolygon
239 Portal_MaxsBoxPolygon
247 Portal_MinsBoxPolygon
255 Portal_MaxsBoxPolygon
267 typedef struct portalrecursioninfo_s
270 int numfrustumplanes;
275 unsigned char *surfacepvs;
277 unsigned char *visitingleafpvs; // used to prevent infinite loops
279 unsigned char *leafpvs;
280 unsigned char *shadowtrispvs;
281 unsigned char *lighttrispvs;
284 float *updateleafsmins;
285 float *updateleafsmaxs;
287 portalrecursioninfo_t;
289 static void Portal_RecursiveFlow (portalrecursioninfo_t *info, mleaf_t *leaf, int firstclipplane, int numclipplanes)
292 int newpoints, i, prev;
295 tinyplane_t *newplanes;
296 int leafindex = leaf - info->model->brush.data_leafs;
298 if (CHECKPVSBIT(info->visitingleafpvs, leafindex))
299 return; // recursive loop of leafs (cmc.bsp for megatf coop)
301 SETPVSBIT(info->visitingleafpvs, leafindex);
303 for (i = 0;i < 3;i++)
305 if (info->updateleafsmins && info->updateleafsmins[i] > leaf->mins[i]) info->updateleafsmins[i] = leaf->mins[i];
306 if (info->updateleafsmaxs && info->updateleafsmaxs[i] < leaf->maxs[i]) info->updateleafsmaxs[i] = leaf->maxs[i];
312 if (!CHECKPVSBIT(info->leafpvs, leafindex))
314 SETPVSBIT(info->leafpvs, leafindex);
315 info->leaflist[info->numleafs++] = leafindex;
319 // mark surfaces in leaf that can be seen through portal
320 if (leaf->numleafsurfaces && info->surfacepvs)
322 for (i = 0;i < leaf->numleafsurfaces;i++)
324 int surfaceindex = leaf->firstleafsurface[i];
325 msurface_t *surface = info->model->data_surfaces + surfaceindex;
326 if (BoxesOverlap(surface->mins, surface->maxs, info->boxmins, info->boxmaxs))
328 qboolean insidebox = BoxInsideBox(surface->mins, surface->maxs, info->boxmins, info->boxmaxs);
329 qboolean addedtris = false;
332 const float *vertex3f;
334 vertex3f = info->model->surfmesh.data_vertex3f;
335 elements = (info->model->surfmesh.data_element3i + 3 * surface->num_firsttriangle);
336 for (t = surface->num_firsttriangle, tend = t + surface->num_triangles;t < tend;t++, elements += 3)
338 VectorCopy(vertex3f + elements[0] * 3, v + 0);
339 VectorCopy(vertex3f + elements[1] * 3, v + 3);
340 VectorCopy(vertex3f + elements[2] * 3, v + 6);
341 if (PointInfrontOfTriangle(info->eye, v + 0, v + 3, v + 6)
342 && (insidebox || TriangleOverlapsBox(v, v + 3, v + 6, info->boxmins, info->boxmaxs))
343 && (!info->exact || Portal_PortalThroughPortalPlanes(&portalplanes[firstclipplane], numclipplanes, v, 3, &portaltemppoints2[0][0], 256) > 0))
346 if (info->shadowtrispvs)
347 SETPVSBIT(info->shadowtrispvs, t);
348 if (info->lighttrispvs)
349 SETPVSBIT(info->lighttrispvs, t);
352 if (addedtris && !CHECKPVSBIT(info->surfacepvs, surfaceindex))
354 SETPVSBIT(info->surfacepvs, surfaceindex);
355 info->surfacelist[info->numsurfaces++] = surfaceindex;
361 // follow portals into other leafs
362 for (p = leaf->portals;p;p = p->next)
364 // only flow through portals facing the viewer
365 dist = PlaneDiff(info->eye, (&p->plane));
366 if (dist < 0 && BoxesOverlap(p->past->mins, p->past->maxs, info->boxmins, info->boxmaxs))
368 newpoints = Portal_PortalThroughPortalPlanes(&portalplanes[firstclipplane], numclipplanes, (float *) p->points, p->numpoints, &portaltemppoints2[0][0], 256);
371 else if (firstclipplane + numclipplanes + newpoints > MAXRECURSIVEPORTALPLANES)
372 ranoutofportalplanes = true;
375 // find the center by averaging
377 for (i = 0;i < newpoints;i++)
378 VectorAdd(center, portaltemppoints2[i], center);
379 // ixtable is a 1.0f / N table
380 VectorScale(center, ixtable[newpoints], center);
381 // calculate the planes, and make sure the polygon can see its own center
382 newplanes = &portalplanes[firstclipplane + numclipplanes];
383 for (prev = newpoints - 1, i = 0;i < newpoints;prev = i, i++)
385 TriangleNormal(portaltemppoints2[prev], portaltemppoints2[i], info->eye, newplanes[i].normal);
386 VectorNormalize(newplanes[i].normal);
387 newplanes[i].dist = DotProduct(info->eye, newplanes[i].normal);
388 if (DotProduct(newplanes[i].normal, center) <= newplanes[i].dist)
390 // polygon can't see its own center, discard and use parent portal
395 Portal_RecursiveFlow(info, p->past, firstclipplane + numclipplanes, newpoints);
397 Portal_RecursiveFlow(info, p->past, firstclipplane, numclipplanes);
402 CLEARPVSBIT(info->visitingleafpvs, leafindex);
405 static void Portal_RecursiveFindLeafForFlow(portalrecursioninfo_t *info, mnode_t *node)
409 float f = DotProduct(info->eye, node->plane->normal) - node->plane->dist;
411 Portal_RecursiveFindLeafForFlow(info, node->children[0]);
413 Portal_RecursiveFindLeafForFlow(info, node->children[1]);
417 mleaf_t *leaf = (mleaf_t *)node;
418 if (leaf->clusterindex >= 0)
419 Portal_RecursiveFlow(info, leaf, 0, info->numfrustumplanes);
423 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)
426 portalrecursioninfo_t info;
428 // if there is no model, it can not block visibility
431 Con_Print("Portal_Visibility: NULL model\n");
435 if (!model->brush.data_nodes)
437 Con_Print("Portal_Visibility: not a brush model\n");
441 // put frustum planes (if any) into tinyplane format at start of buffer
442 for (i = 0;i < numfrustumplanes;i++)
444 VectorCopy(frustumplanes[i].normal, portalplanes[i].normal);
445 portalplanes[i].dist = frustumplanes[i].dist;
448 ranoutofportalplanes = false;
449 ranoutofportals = false;
451 VectorCopy(boxmins, info.boxmins);
452 VectorCopy(boxmaxs, info.boxmaxs);
454 info.numsurfaces = 0;
455 info.surfacelist = surfacelist;
456 info.surfacepvs = surfacepvs;
458 info.visitingleafpvs = visitingleafpvs;
459 info.leaflist = leaflist;
460 info.leafpvs = leafpvs;
462 VectorCopy(eye, info.eye);
463 info.numfrustumplanes = numfrustumplanes;
464 info.updateleafsmins = updateleafsmins;
465 info.updateleafsmaxs = updateleafsmaxs;
466 info.shadowtrispvs = shadowtrispvs;
467 info.lighttrispvs = lighttrispvs;
469 Portal_RecursiveFindLeafForFlow(&info, model->brush.data_nodes);
471 if (ranoutofportalplanes)
472 Con_Printf("Portal_RecursiveFlow: ran out of %d plane stack when recursing through portals\n", MAXRECURSIVEPORTALPLANES);
474 Con_Printf("Portal_RecursiveFlow: ran out of %d portal stack when recursing through portals\n", MAXRECURSIVEPORTALS);
475 if (numsurfacespointer)
476 *numsurfacespointer = info.numsurfaces;
478 *numleafspointer = info.numleafs;