#include "quakedef.h"
+#include "polygon.h"
#define MAXRECURSIVEPORTALPLANES 1024
#define MAXRECURSIVEPORTALS 256
static int portal_markid = 0;
static float boxpoints[4*3];
-int Portal_ClipPolygonToPlane(float *in, float *out, int inpoints, int maxoutpoints, tinyplane_t *p)
-{
- int i, outpoints, prevside, side;
- float *prevpoint, prevdist, dist, dot;
-
- if (inpoints < 3)
- return inpoints;
- // begin with the last point, then enter the loop with the first point as current
- prevpoint = in + 3 * (inpoints - 1);
- prevdist = DotProduct(prevpoint, p->normal) - p->dist;
- prevside = prevdist >= 0 ? SIDE_FRONT : SIDE_BACK;
- i = 0;
- outpoints = 0;
- goto begin;
- for (;i < inpoints;i++)
- {
- prevpoint = in;
- prevdist = dist;
- prevside = side;
- in += 3;
-
-begin:
- dist = DotProduct(in, p->normal) - p->dist;
- side = dist >= 0 ? SIDE_FRONT : SIDE_BACK;
-
- if (prevside == SIDE_FRONT)
- {
- if (outpoints >= maxoutpoints)
- return -1;
- VectorCopy(prevpoint, out);
- out += 3;
- outpoints++;
- if (side == SIDE_FRONT)
- continue;
- }
- else if (side == SIDE_BACK)
- continue;
-
- // generate a split point
- if (outpoints >= maxoutpoints)
- return -1;
- dot = prevdist / (prevdist - dist);
- out[0] = prevpoint[0] + dot * (in[0] - prevpoint[0]);
- out[1] = prevpoint[1] + dot * (in[1] - prevpoint[1]);
- out[2] = prevpoint[2] + dot * (in[2] - prevpoint[2]);
- out += 3;
- outpoints++;
- }
-
- return outpoints;
-}
-
-
int Portal_PortalThroughPortalPlanes(tinyplane_t *clipplanes, int clipnumplanes, float *targpoints, int targnumpoints, float *out, int maxpoints)
{
int numpoints, i;
memcpy(&portaltemppoints[0][0][0], targpoints, numpoints * 3 * sizeof(float));
for (i = 0;i < clipnumplanes;i++)
{
- numpoints = Portal_ClipPolygonToPlane(&portaltemppoints[0][0][0], &portaltemppoints[1][0][0], numpoints, 256, clipplanes + i);
+ PolygonF_Divide(numpoints, &portaltemppoints[0][0][0], clipplanes[i].normal[0], clipplanes[i].normal[1], clipplanes[i].normal[2], clipplanes[i].dist, 1.0f/32.0f, 256, &portaltemppoints[1][0][0], &numpoints, 0, NULL, NULL);
if (numpoints < 3)
return numpoints;
memcpy(&portaltemppoints[0][0][0], &portaltemppoints[1][0][0], numpoints * 3 * sizeof(float));
}
portalrecursioninfo_t;
-void Portal_RecursiveFlow_ExactLeafFaces(portalrecursioninfo_t *info, int *mark, int numleafsurfaces, int firstclipplane, int numclipplanes)
-{
- int i, j, *elements;
- float *v[3], *vertex3f;
- vec3_t trimins, trimaxs;
- msurface_t *surface;
- for (i = 0;i < numleafsurfaces;i++, mark++)
- {
- if (!CHECKPVSBIT(info->surfacepvs, *mark))
- {
- surface = info->model->brush.data_surfaces + *mark;
- if (!BoxesOverlap(surface->mins, surface->maxs, info->boxmins, info->boxmaxs))
- continue;
- vertex3f = surface->groupmesh->data_vertex3f;
- for (j = 0, elements = (surface->groupmesh->data_element3i + 3 * surface->num_firsttriangle);j < surface->num_triangles;j++, elements += 3)
- {
- v[0] = vertex3f + elements[0] * 3;
- v[1] = vertex3f + elements[1] * 3;
- v[2] = vertex3f + elements[2] * 3;
- if (PointInfrontOfTriangle(info->eye, v[0], v[1], v[2]))
- {
- trimins[0] = min(v[0][0], min(v[1][0], v[2][0]));
- trimaxs[0] = max(v[0][0], max(v[1][0], v[2][0]));
- trimins[1] = min(v[0][1], min(v[1][1], v[2][1]));
- trimaxs[1] = max(v[0][1], max(v[1][1], v[2][1]));
- trimins[2] = min(v[0][2], min(v[1][2], v[2][2]));
- trimaxs[2] = max(v[0][2], max(v[1][2], v[2][2]));
- if (BoxesOverlap(trimins, trimaxs, info->boxmins, info->boxmaxs))
- if (Portal_PortalThroughPortalPlanes(&portalplanes[firstclipplane], numclipplanes, v[0], 3, &portaltemppoints2[0][0], 256) >= 3)
- break;
- }
- }
- if (j == surface->num_triangles)
- continue;
- SETPVSBIT(info->surfacepvs, *mark);
- info->surfacelist[info->numsurfaces++] = *mark;
- }
- }
-}
-
void Portal_RecursiveFlow (portalrecursioninfo_t *info, mleaf_t *leaf, int firstclipplane, int numclipplanes)
{
mportal_t *p;
// mark surfaces in leaf that can be seen through portal
if (leaf->numleafsurfaces && info->surfacepvs)
{
- if (info->exact)
- Portal_RecursiveFlow_ExactLeafFaces(info, leaf->firstleafsurface, leaf->numleafsurfaces, firstclipplane, numclipplanes);
- else
+ for (i = 0;i < leaf->numleafsurfaces;i++)
{
- for (i = 0;i < leaf->numleafsurfaces;i++)
+ int surfaceindex = leaf->firstleafsurface[i];
+ if (!CHECKPVSBIT(info->surfacepvs, surfaceindex))
{
- int surfaceindex = leaf->firstleafsurface[i];
- if (!CHECKPVSBIT(info->surfacepvs, surfaceindex))
+ msurface_t *surface = info->model->data_surfaces + surfaceindex;
+ if (BoxesOverlap(surface->mins, surface->maxs, info->boxmins, info->boxmaxs))
{
- msurface_t *surface = info->model->brush.data_surfaces + surfaceindex;
- if (BoxesOverlap(surface->mins, surface->maxs, info->boxmins, info->boxmaxs))
+ if (info->exact)
+ {
+ int j;
+ const int *elements;
+ const float *vertex3f;
+ float v[9], trimins[3], trimaxs[3];
+ vertex3f = surface->groupmesh->data_vertex3f;
+ elements = (surface->groupmesh->data_element3i + 3 * surface->num_firsttriangle);
+ for (j = 0;j < surface->num_triangles;j++, 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))
+ {
+ trimins[0] = min(v[0], min(v[3], v[6]));
+ trimaxs[0] = max(v[0], max(v[3], v[6]));
+ trimins[1] = min(v[1], min(v[4], v[7]));
+ trimaxs[1] = max(v[1], max(v[4], v[7]));
+ trimins[2] = min(v[2], min(v[5], v[8]));
+ trimaxs[2] = max(v[2], max(v[5], v[8]));
+ if (BoxesOverlap(trimins, trimaxs, info->boxmins, info->boxmaxs) && Portal_PortalThroughPortalPlanes(&portalplanes[firstclipplane], numclipplanes, v, 3, &portaltemppoints2[0][0], 256) >= 3)
+ {
+ SETPVSBIT(info->surfacepvs, surfaceindex);
+ info->surfacelist[info->numsurfaces++] = surfaceindex;
+ break;
+ }
+ }
+ }
+ }
+ else
{
SETPVSBIT(info->surfacepvs, surfaceindex);
info->surfacelist[info->numsurfaces++] = surfaceindex;
else
{
mleaf_t *leaf = (mleaf_t *)node;
- if (leaf->portals)
+ if (leaf->clusterindex >= 0)
Portal_RecursiveFlow(info, leaf, 0, info->numfrustumplanes);
}
}
Mod_CheckLoaded(model);
- if (!model->brush.num_portals)
+ if (!model->brush.data_nodes)
{
Con_Print("Portal_Visibility: not a brush model\n");
return;