]> de.git.xonotic.org Git - xonotic/darkplaces.git/blobdiff - portals.c
Cleaned up alot more memory leaks. (still get 720 leaks just running demo1.dem)
[xonotic/darkplaces.git] / portals.c
index ba7de15d6b59d475aadd7187f7ff33a34e266b77..fdc4ecb98d114c23d2cdaf4592b4936be5ca7211 100644 (file)
--- a/portals.c
+++ b/portals.c
@@ -323,7 +323,8 @@ typedef struct portalrecursioninfo_s
 {
        int exact;
        int numfrustumplanes;
-       float nradius;
+       vec3_t boxmins;
+       vec3_t boxmaxs;
        qbyte *surfacemark;
        qbyte *leafmark;
        model_t *model;
@@ -336,8 +337,8 @@ 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;
-       surfmesh_t *surfmesh;
        for (i = 0;i < nummarksurfaces;i++, mark++)
        {
                if (!info->surfacemark[*mark])
@@ -360,21 +361,25 @@ void Portal_RecursiveFlow_ExactMarkSurfaces(portalrecursioninfo_t *info, int *ma
                        }
                        else
                        {
-                               for (surfmesh = surf->mesh;surfmesh;surfmesh = surfmesh->chain)
+                               for (j = 0, elements = surf->mesh.data_element3i;j < surf->mesh.num_triangles;j++, elements += 3)
                                {
-                                       for (j = 0, elements = surfmesh->element3i;j < surfmesh->numtriangles;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]))
                                        {
-                                               VectorCopy((surfmesh->vertex3f + elements[0] * 3), trianglepoints[0]);
-                                               VectorCopy((surfmesh->vertex3f + elements[1] * 3), trianglepoints[1]);
-                                               VectorCopy((surfmesh->vertex3f + elements[2] * 3), trianglepoints[2]);
-                                               if (PointInfrontOfTriangle(info->eye, trianglepoints[0], trianglepoints[1], trianglepoints[2])
-                                                && Portal_PortalThroughPortalPlanes(&portalplanes[firstclipplane], numclipplanes, trianglepoints[0], 3, &portaltemppoints2[0][0], 256) >= 3)
-                                                       break;
+                                               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 < surfmesh->numtriangles)
-                                               break;
                                }
-                               if (surfmesh == NULL)
+                               if (j == surf->mesh.num_triangles)
                                        continue;
                        }
                        info->surfacemark[*mark] = true;
@@ -397,7 +402,7 @@ void Portal_RecursiveFlow (portalrecursioninfo_t *info, mleaf_t *leaf, int first
        }
 
        if (info->leafmark)
-               info->leafmark[leaf - info->model->brushq1.leafs] = true;
+               info->leafmark[leaf - info->model->brushq1.data_leafs] = true;
 
        // mark surfaces in leaf that can be seen through portal
        if (leaf->nummarksurfaces && info->surfacemark)
@@ -412,9 +417,9 @@ void Portal_RecursiveFlow (portalrecursioninfo_t *info, mleaf_t *leaf, int first
        // follow portals into other leafs
        for (p = leaf->portals;p;p = p->next)
        {
-               // only flow through portals facing away from the viewer
+               // only flow through portals facing the viewer
                dist = PlaneDiff(info->eye, (&p->plane));
-               if (dist < 0 && dist >= info->nradius)
+               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)
@@ -433,8 +438,8 @@ void Portal_RecursiveFlow (portalrecursioninfo_t *info, mleaf_t *leaf, int first
                                newplanes = &portalplanes[firstclipplane + numclipplanes];
                                for (prev = newpoints - 1, i = 0;i < newpoints;prev = i, i++)
                                {
-                                       VectorSubtract(info->eye, portaltemppoints2[i], v1);
-                                       VectorSubtract(portaltemppoints2[prev], portaltemppoints2[i], v2);
+                                       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);
@@ -470,7 +475,7 @@ void Portal_RecursiveFindLeafForFlow(portalrecursioninfo_t *info, mnode_t *node)
        }
 }
 
-void Portal_Visibility(model_t *model, const vec3_t eye, qbyte *leafmark, qbyte *surfacemark, const mplane_t *frustumplanes, int numfrustumplanes, int exact, float radius, float *updateleafsmins, float *updateleafsmaxs)
+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;
@@ -478,7 +483,7 @@ void Portal_Visibility(model_t *model, const vec3_t eye, qbyte *leafmark, qbyte
        // if there is no model, it can not block visibility
        if (model == NULL)
        {
-               Con_Printf("Portal_Visibility: NULL model\n");
+               Con_Print("Portal_Visibility: NULL model\n");
                return;
        }
 
@@ -486,7 +491,7 @@ void Portal_Visibility(model_t *model, const vec3_t eye, qbyte *leafmark, qbyte
 
        if (!model->brushq1.numportals)
        {
-               Con_Printf("Portal_Visibility: not a brush model\n");
+               Con_Print("Portal_Visibility: not a brush model\n");
                return;
        }
 
@@ -500,7 +505,8 @@ void Portal_Visibility(model_t *model, const vec3_t eye, qbyte *leafmark, qbyte
        ranoutofportalplanes = false;
        ranoutofportals = false;
 
-       info.nradius = -radius;
+       VectorCopy(boxmins, info.boxmins);
+       VectorCopy(boxmaxs, info.boxmaxs);
        info.exact = exact;
        info.surfacemark = surfacemark;
        info.leafmark = leafmark;