]> de.git.xonotic.org Git - xonotic/darkplaces.git/blobdiff - portals.c
Fix a typo in DrawQ_RotPic which broke it badly.
[xonotic/darkplaces.git] / portals.c
index 96a3f346fca4c0d18e211461023ac3e77f1eda85..2872a8ca89c1a3faaa1fe22ef11cc8be242a1a94 100644 (file)
--- a/portals.c
+++ b/portals.c
@@ -1,5 +1,7 @@
 
 #include "quakedef.h"
+#include "polygon.h"
+#include "portals.h"
 
 #define MAXRECURSIVEPORTALPLANES 1024
 #define MAXRECURSIVEPORTALS 256
@@ -12,82 +14,28 @@ static float portaltemppoints2[256][3];
 static int portal_markid = 0;
 static float boxpoints[4*3];
 
-int Portal_ClipPolygonToPlane(float *in, float *out, int inpoints, int maxoutpoints, tinyplane_t *p)
+static int Portal_PortalThroughPortalPlanes(tinyplane_t *clipplanes, int clipnumplanes, float *targpoints, int targnumpoints, float *out, int maxpoints)
 {
-       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++)
+       int numpoints = targnumpoints, i, w;
+       if (numpoints < 1)
+               return numpoints;
+       if (maxpoints > 256)
+               maxpoints = 256;
+       w = 0;
+       memcpy(&portaltemppoints[w][0][0], targpoints, numpoints * 3 * sizeof(float));
+       for (i = 0;i < clipnumplanes && numpoints > 0;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++;
+               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);
+               w = 1-w;
+               numpoints = min(numpoints, 256);
        }
-
-       return outpoints;
-}
-
-
-int Portal_PortalThroughPortalPlanes(tinyplane_t *clipplanes, int clipnumplanes, float *targpoints, int targnumpoints, float *out, int maxpoints)
-{
-       int numpoints, i;
-       if (targnumpoints < 3)
-               return targnumpoints;
-       if (maxpoints < 3)
-               return -1;
-       numpoints = targnumpoints;
-       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);
-               if (numpoints < 3)
-                       return numpoints;
-               memcpy(&portaltemppoints[0][0][0], &portaltemppoints[1][0][0], numpoints * 3 * sizeof(float));
-       }
-       if (numpoints > maxpoints)
-               return -1;
-       memcpy(out, &portaltemppoints[0][0][0], numpoints * 3 * sizeof(float));
+       numpoints = min(numpoints, maxpoints);
+       if (numpoints > 0)
+               memcpy(out, &portaltemppoints[w][0][0], numpoints * 3 * sizeof(float));
        return numpoints;
 }
 
-int Portal_RecursiveFlowSearch (mleaf_t *leaf, vec3_t eye, int firstclipplane, int numclipplanes)
+static int Portal_RecursiveFlowSearch (mleaf_t *leaf, vec3_t eye, int firstclipplane, int numclipplanes)
 {
        mportal_t *p;
        int newpoints, i, prev;
@@ -123,7 +71,7 @@ int Portal_RecursiveFlowSearch (mleaf_t *leaf, vec3_t eye, int firstclipplane, i
                                        VectorSubtract(eye, portaltemppoints2[i], v1);
                                        VectorSubtract(portaltemppoints2[prev], portaltemppoints2[i], v2);
                                        CrossProduct(v1, v2, newplanes[i].normal);
-                                       VectorNormalizeFast(newplanes[i].normal);
+                                       VectorNormalize(newplanes[i].normal);
                                        newplanes[i].dist = DotProduct(eye, newplanes[i].normal);
                                        if (DotProduct(newplanes[i].normal, center) <= newplanes[i].dist)
                                        {
@@ -148,7 +96,7 @@ int Portal_RecursiveFlowSearch (mleaf_t *leaf, vec3_t eye, int firstclipplane, i
        return false;
 }
 
-void Portal_PolygonRecursiveMarkLeafs(mnode_t *node, float *polypoints, int numpoints)
+static void Portal_PolygonRecursiveMarkLeafs(mnode_t *node, float *polypoints, int numpoints)
 {
        int i, front;
        float *p;
@@ -180,22 +128,21 @@ loc0:
        goto loc0;
 }
 
-int Portal_CheckPolygon(model_t *model, vec3_t eye, float *polypoints, int numpoints)
+int Portal_CheckPolygon(dp_model_t *model, vec3_t eye, float *polypoints, int numpoints)
 {
        int i, prev, returnvalue;
        mleaf_t *eyeleaf;
        vec3_t center, v1, v2;
 
        // if there is no model, it can not block visibility
-       if (model == NULL)
+       if (model == NULL || !model->brush.PointInLeaf)
                return true;
 
        portal_markid++;
 
-       Mod_CheckLoaded(model);
        Portal_PolygonRecursiveMarkLeafs(model->brush.data_nodes, polypoints, numpoints);
 
-       eyeleaf = model->brushq1.PointInLeaf(model, eye);
+       eyeleaf = model->brush.PointInLeaf(model, eye);
 
        // find the center by averaging
        VectorClear(center);
@@ -210,7 +157,7 @@ int Portal_CheckPolygon(model_t *model, vec3_t eye, float *polypoints, int numpo
                VectorSubtract(eye, (&polypoints[i * 3]), v1);
                VectorSubtract((&polypoints[prev * 3]), (&polypoints[i * 3]), v2);
                CrossProduct(v1, v2, portalplanes[i].normal);
-               VectorNormalizeFast(portalplanes[i].normal);
+               VectorNormalize(portalplanes[i].normal);
                portalplanes[i].dist = DotProduct(eye, portalplanes[i].normal);
                if (DotProduct(portalplanes[i].normal, center) <= portalplanes[i].dist)
                {
@@ -258,7 +205,7 @@ int Portal_CheckPolygon(model_t *model, vec3_t eye, float *polypoints, int numpo
        }\
 }
 
-int Portal_CheckBox(model_t *model, vec3_t eye, vec3_t a, vec3_t b)
+int Portal_CheckBox(dp_model_t *model, vec3_t eye, vec3_t a, vec3_t b)
 {
        if (eye[0] >= (a[0] - 1.0f) && eye[0] < (b[0] + 1.0f)
         && eye[1] >= (a[1] - 1.0f) && eye[1] < (b[1] + 1.0f)
@@ -317,84 +264,41 @@ int Portal_CheckBox(model_t *model, vec3_t eye, vec3_t a, vec3_t b)
        return false;
 }
 
-vec3_t trianglepoints[3];
-
 typedef struct portalrecursioninfo_s
 {
        int exact;
        int numfrustumplanes;
        vec3_t boxmins;
        vec3_t boxmaxs;
-       qbyte *surfacemark;
-       qbyte *leafmark;
-       model_t *model;
+       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;
 
-void Portal_RecursiveFlow_ExactLeafFaces(portalrecursioninfo_t *info, int *mark, int numleaffaces, int firstclipplane, int numclipplanes)
-{
-       int i, j, *elements;
-       vec3_t trimins, trimaxs;
-       msurface_t *surf;
-       for (i = 0;i < numleaffaces;i++, mark++)
-       {
-               if (!info->surfacemark[*mark])
-               {
-                       // FIXME?  this assumes q1bsp polygon surfaces
-                       surf = info->model->brushq1.surfaces + *mark;
-                       if (surf->mesh.num_vertices)
-                       {
-                               if (surf->flags & SURF_PLANEBACK)
-                               {
-                                       if (DotProduct(info->eye, surf->plane->normal) > surf->plane->dist)
-                                               continue;
-                               }
-                               else
-                               {
-                                       if (DotProduct(info->eye, surf->plane->normal) < surf->plane->dist)
-                                               continue;
-                               }
-                               if (Portal_PortalThroughPortalPlanes(&portalplanes[firstclipplane], numclipplanes, surf->mesh.data_vertex3f, surf->mesh.num_vertices, &portaltemppoints2[0][0], 256) < 3)
-                                       continue;
-                       }
-                       else
-                       {
-                               for (j = 0, elements = surf->mesh.data_element3i;j < surf->mesh.num_triangles;j++, elements += 3)
-                               {
-                                       VectorCopy((surf->mesh.data_vertex3f + elements[0] * 3), trianglepoints[0]);
-                                       VectorCopy((surf->mesh.data_vertex3f + elements[1] * 3), trianglepoints[1]);
-                                       VectorCopy((surf->mesh.data_vertex3f + elements[2] * 3), trianglepoints[2]);
-                                       if (PointInfrontOfTriangle(info->eye, trianglepoints[0], trianglepoints[1], trianglepoints[2]))
-                                       {
-                                               trimins[0] = min(trianglepoints[0][0], min(trianglepoints[1][0], trianglepoints[2][0]));
-                                               trimaxs[0] = max(trianglepoints[0][0], max(trianglepoints[1][0], trianglepoints[2][0]));
-                                               trimins[1] = min(trianglepoints[0][1], min(trianglepoints[1][1], trianglepoints[2][1]));
-                                               trimaxs[1] = max(trianglepoints[0][1], max(trianglepoints[1][1], trianglepoints[2][1]));
-                                               trimins[2] = min(trianglepoints[0][2], min(trianglepoints[1][2], trianglepoints[2][2]));
-                                               trimaxs[2] = max(trianglepoints[0][2], max(trianglepoints[1][2], trianglepoints[2][2]));
-                                               if (BoxesOverlap(trimins, trimaxs, info->boxmins, info->boxmaxs))
-                                                       if (Portal_PortalThroughPortalPlanes(&portalplanes[firstclipplane], numclipplanes, trianglepoints[0], 3, &portaltemppoints2[0][0], 256) >= 3)
-                                                               break;
-                                       }
-                               }
-                               if (j == surf->mesh.num_triangles)
-                                       continue;
-                       }
-                       info->surfacemark[*mark] = true;
-               }
-       }
-}
-
-void Portal_RecursiveFlow (portalrecursioninfo_t *info, mleaf_t *leaf, int firstclipplane, int numclipplanes)
+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, v1, v2;
+       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++)
        {
@@ -402,17 +306,56 @@ void Portal_RecursiveFlow (portalrecursioninfo_t *info, mleaf_t *leaf, int first
                if (info->updateleafsmaxs && info->updateleafsmaxs[i] < leaf->maxs[i]) info->updateleafsmaxs[i] = leaf->maxs[i];
        }
 
-       if (info->leafmark)
-               info->leafmark[leaf - info->model->brush.data_leafs] = true;
+
+       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->numleaffaces && info->surfacemark)
+       if (leaf->numleafsurfaces && info->surfacepvs)
        {
-               if (info->exact)
-                       Portal_RecursiveFlow_ExactLeafFaces(info, leaf->firstleafface, leaf->numleaffaces, firstclipplane, numclipplanes);
-               else
-                       for (i = 0;i < leaf->numleaffaces;i++)
-                               info->surfacemark[leaf->firstleafface[i]] = true;
+               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 || TriangleBBoxOverlapsBox(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
@@ -435,18 +378,16 @@ void Portal_RecursiveFlow (portalrecursioninfo_t *info, mleaf_t *leaf, int first
                                        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 it's own 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++)
                                {
-                                       VectorSubtract(portaltemppoints2[prev], portaltemppoints2[i], v1);
-                                       VectorSubtract(info->eye, portaltemppoints2[i], v2);
-                                       CrossProduct(v1, v2, newplanes[i].normal);
-                                       VectorNormalizeFast(newplanes[i].normal);
+                                       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 it's own center, discard and use parent portal
+                                               // polygon can't see its own center, discard and use parent portal
                                                break;
                                        }
                                }
@@ -457,9 +398,11 @@ void Portal_RecursiveFlow (portalrecursioninfo_t *info, mleaf_t *leaf, int first
                        }
                }
        }
+
+       CLEARPVSBIT(info->visitingleafpvs, leafindex);
 }
 
-void Portal_RecursiveFindLeafForFlow(portalrecursioninfo_t *info, mnode_t *node)
+static void Portal_RecursiveFindLeafForFlow(portalrecursioninfo_t *info, mnode_t *node)
 {
        if (node->plane)
        {
@@ -472,12 +415,12 @@ void Portal_RecursiveFindLeafForFlow(portalrecursioninfo_t *info, mnode_t *node)
        else
        {
                mleaf_t *leaf = (mleaf_t *)node;
-               if (leaf->portals)
+               if (leaf->clusterindex >= 0)
                        Portal_RecursiveFlow(info, leaf, 0, info->numfrustumplanes);
        }
 }
 
-void Portal_Visibility(model_t *model, const vec3_t eye, qbyte *leafmark, qbyte *surfacemark, const mplane_t *frustumplanes, int numfrustumplanes, int exact, const float *boxmins, const float *boxmaxs, float *updateleafsmins, float *updateleafsmaxs)
+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;
@@ -489,9 +432,7 @@ void Portal_Visibility(model_t *model, const vec3_t eye, qbyte *leafmark, qbyte
                return;
        }
 
-       Mod_CheckLoaded(model);
-
-       if (!model->brush.num_portals)
+       if (!model->brush.data_nodes)
        {
                Con_Print("Portal_Visibility: not a brush model\n");
                return;
@@ -510,13 +451,20 @@ void Portal_Visibility(model_t *model, const vec3_t eye, qbyte *leafmark, qbyte
        VectorCopy(boxmins, info.boxmins);
        VectorCopy(boxmaxs, info.boxmaxs);
        info.exact = exact;
-       info.surfacemark = surfacemark;
-       info.leafmark = leafmark;
+       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);
 
@@ -524,5 +472,9 @@ void Portal_Visibility(model_t *model, const vec3_t eye, qbyte *leafmark, qbyte
                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;
 }