]> de.git.xonotic.org Git - xonotic/darkplaces.git/blobdiff - portals.c
fixed GL_Scissor call in rtlight code (apparently I need to feed it a top to bottom...
[xonotic/darkplaces.git] / portals.c
index 742f6d36d04ff88355d49d59c5fceaa3bd74aaac..bf6f27448bda6d683bb6ef017a987d9ef0abdcb3 100644 (file)
--- a/portals.c
+++ b/portals.c
@@ -5,16 +5,14 @@
 #define MAXRECURSIVEPORTALS 256
 
 static tinyplane_t portalplanes[MAXRECURSIVEPORTALPLANES];
-static int portalplanecount;
 static int ranoutofportalplanes;
 static int ranoutofportals;
 static float portaltemppoints[2][256][3];
 static float portaltemppoints2[256][3];
 static int portal_markid = 0;
-//float viewportalpoints[16*3];
 static float boxpoints[4*3];
 
-int R_ClipPolygonToPlane(float *in, float *out, int inpoints, int maxoutpoints, tinyplane_t *p)
+int Portal_ClipPolygonToPlane(float *in, float *out, int inpoints, int maxoutpoints, tinyplane_t *p)
 {
        int i, outpoints, prevside, side;
        float *prevpoint, prevdist, dist, dot;
@@ -66,20 +64,8 @@ begin:
        return outpoints;
 }
 
-/*
-void R_TriangleToPlane(vec3_t point1, vec3_t point2, vec3_t point3, tinyplane_t *p)
-{
-       vec3_t v1, v2;
-       VectorSubtract(point1, point2, v1);
-       VectorSubtract(point3, point2, v2);
-       CrossProduct(v1, v2, p->normal);
-//     VectorNormalize(p->normal);
-       VectorNormalizeFast(p->normal);
-       p->dist = DotProduct(point1, p->normal);
-}
-*/
 
-int R_PortalThroughPortalPlanes(tinyplane_t *clipplanes, int clipnumplanes, float *targpoints, int targnumpoints, float *out, int maxpoints)
+int Portal_PortalThroughPortalPlanes(tinyplane_t *clipplanes, int clipnumplanes, float *targpoints, int targnumpoints, float *out, int maxpoints)
 {
        int numpoints, i;
        if (targnumpoints < 3)
@@ -90,14 +76,14 @@ int R_PortalThroughPortalPlanes(tinyplane_t *clipplanes, int clipnumplanes, floa
        memcpy(&portaltemppoints[0][0][0], targpoints, numpoints * 3 * sizeof(float));
        for (i = 0;i < clipnumplanes;i++)
        {
-               numpoints = R_ClipPolygonToPlane(&portaltemppoints[0][0][0], &portaltemppoints[1][0][0], numpoints, 256, clipplanes + 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[1][0][0], numpoints * 3 * sizeof(float));
+       memcpy(out, &portaltemppoints[0][0][0], numpoints * 3 * sizeof(float));
        return numpoints;
 }
 
@@ -117,7 +103,7 @@ int Portal_RecursiveFlowSearch (mleaf_t *leaf, vec3_t eye, int firstclipplane, i
                // only flow through portals facing away from the viewer
                if (PlaneDiff(eye, (&p->plane)) < 0)
                {
-                       newpoints = R_PortalThroughPortalPlanes(&portalplanes[firstclipplane], numclipplanes, (float *) p->points, p->numpoints, &portaltemppoints2[0][0], 256);
+                       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)
@@ -134,7 +120,6 @@ int Portal_RecursiveFlowSearch (mleaf_t *leaf, vec3_t eye, int firstclipplane, i
                                newplanes = &portalplanes[firstclipplane + numclipplanes];
                                for (prev = newpoints - 1, i = 0;i < newpoints;prev = i, i++)
                                {
-//                                     R_TriangleToPlane(eye, portaltemppoints2[i], portaltemppoints2[prev], newplanes + i);
                                        VectorSubtract(eye, portaltemppoints2[i], v1);
                                        VectorSubtract(portaltemppoints2[prev], portaltemppoints2[i], v2);
                                        CrossProduct(v1, v2, newplanes[i].normal);
@@ -150,12 +135,12 @@ int Portal_RecursiveFlowSearch (mleaf_t *leaf, vec3_t eye, int firstclipplane, i
                                {
                                        if (Portal_RecursiveFlowSearch(p->past, eye, firstclipplane + numclipplanes, newpoints))
                                                return true;
-                       }
+                               }
                                else
                                {
                                        if (Portal_RecursiveFlowSearch(p->past, eye, firstclipplane, numclipplanes))
                                                return true;
-                       }
+                               }
                        }
                }
        }
@@ -201,11 +186,16 @@ int Portal_CheckPolygon(model_t *model, vec3_t eye, float *polypoints, int numpo
        mleaf_t *eyeleaf;
        vec3_t center, v1, v2;
 
+       // if there is no model, it can not block visibility
+       if (model == NULL)
+               return true;
+
        portal_markid++;
 
-       Portal_PolygonRecursiveMarkLeafs(model->nodes, polypoints, numpoints);
+       Mod_CheckLoaded(model);
+       Portal_PolygonRecursiveMarkLeafs(model->brushq1.nodes, polypoints, numpoints);
 
-       eyeleaf = Mod_PointInLeaf(eye, model);
+       eyeleaf = model->brushq1.PointInLeaf(model, eye);
 
        // find the center by averaging
        VectorClear(center);
@@ -217,7 +207,6 @@ int Portal_CheckPolygon(model_t *model, vec3_t eye, float *polypoints, int numpo
        // calculate the planes, and make sure the polygon can see it's own center
        for (prev = numpoints - 1, i = 0;i < numpoints;prev = i, i++)
        {
-//             R_TriangleToPlane(eye, portaltemppoints2[i], portaltemppoints2[prev], newplanes + i);
                VectorSubtract(eye, (&polypoints[i * 3]), v1);
                VectorSubtract((&polypoints[prev * 3]), (&polypoints[i * 3]), v2);
                CrossProduct(v1, v2, portalplanes[i].normal);
@@ -230,7 +219,6 @@ int Portal_CheckPolygon(model_t *model, vec3_t eye, float *polypoints, int numpo
                }
        }
 
-       portalplanecount = 0;
        ranoutofportalplanes = false;
        ranoutofportals = false;
 
@@ -328,3 +316,211 @@ 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;
+       vec3_t eye;
+       float *updateleafsmins;
+       float *updateleafsmaxs;
+}
+portalrecursioninfo_t;
+
+void Portal_RecursiveFlow_ExactMarkSurfaces(portalrecursioninfo_t *info, int *mark, int nummarksurfaces, int firstclipplane, int numclipplanes)
+{
+       int i, j, *elements;
+       vec3_t trimins, trimaxs;
+       msurface_t *surf;
+       for (i = 0;i < nummarksurfaces;i++, mark++)
+       {
+               if (!info->surfacemark[*mark])
+               {
+                       surf = info->model->brushq1.surfaces + *mark;
+                       if (surf->poly_numverts)
+                       {
+                               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->poly_verts, surf->poly_numverts, &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)
+{
+       mportal_t *p;
+       int newpoints, i, prev;
+       float dist;
+       vec3_t center, v1, v2;
+       tinyplane_t *newplanes;
+
+       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->leafmark)
+               info->leafmark[leaf - info->model->brushq1.data_leafs] = true;
+
+       // mark surfaces in leaf that can be seen through portal
+       if (leaf->nummarksurfaces && info->surfacemark)
+       {
+               if (info->exact)
+                       Portal_RecursiveFlow_ExactMarkSurfaces(info, leaf->firstmarksurface, leaf->nummarksurfaces, firstclipplane, numclipplanes);
+               else
+                       for (i = 0;i < leaf->nummarksurfaces;i++)
+                               info->surfacemark[leaf->firstmarksurface[i]] = true;
+       }
+
+       // 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 it's 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);
+                                       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
+                                               break;
+                                       }
+                               }
+                               if (i == newpoints)
+                                       Portal_RecursiveFlow(info, p->past, firstclipplane + numclipplanes, newpoints);
+                               else
+                                       Portal_RecursiveFlow(info, p->past, firstclipplane, numclipplanes);
+                       }
+               }
+       }
+}
+
+void Portal_RecursiveFindLeafForFlow(portalrecursioninfo_t *info, mnode_t *node)
+{
+       if (node->contents)
+       {
+               if (node->contents != CONTENTS_SKY && node->contents != CONTENTS_SOLID)
+                       Portal_RecursiveFlow(info, (mleaf_t *)node, 0, info->numfrustumplanes);
+       }
+       else
+       {
+               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]);
+       }
+}
+
+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)
+{
+       int i;
+       portalrecursioninfo_t info;
+
+       // if there is no model, it can not block visibility
+       if (model == NULL)
+       {
+               Con_Printf("Portal_Visibility: NULL model\n");
+               return;
+       }
+
+       Mod_CheckLoaded(model);
+
+       if (!model->brushq1.numportals)
+       {
+               Con_Printf("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.surfacemark = surfacemark;
+       info.leafmark = leafmark;
+       info.model = model;
+       VectorCopy(eye, info.eye);
+       info.numfrustumplanes = numfrustumplanes;
+       info.updateleafsmins = updateleafsmins;
+       info.updateleafsmaxs = updateleafsmaxs;
+
+       Portal_RecursiveFindLeafForFlow(&info, model->brushq1.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);
+}
+