]> de.git.xonotic.org Git - xonotic/darkplaces.git/blobdiff - portals.c
hush some texture loading warnings in dedicated servers (thanks to Biomass for report...
[xonotic/darkplaces.git] / portals.c
index 339fd80239a2a6ebb6885697499f68ade61b5689..05a188666935759c15be5928f95e763924b51abf 100644 (file)
--- a/portals.c
+++ b/portals.c
@@ -154,7 +154,7 @@ void Portal_PolygonRecursiveMarkLeafs(mnode_t *node, float *polypoints, int nump
        float *p;
 
 loc0:
-       if (node->contents < 0)
+       if (!node->plane)
        {
                ((mleaf_t *)node)->portalmarkid = portal_markid;
                return;
@@ -187,15 +187,15 @@ int Portal_CheckPolygon(model_t *model, vec3_t eye, float *polypoints, int numpo
        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->nodes, polypoints, numpoints);
+       Portal_PolygonRecursiveMarkLeafs(model->brush.data_nodes, polypoints, numpoints);
 
-       eyeleaf = Mod_PointInLeaf(eye, model);
+       eyeleaf = model->brush.PointInLeaf(model, eye);
 
        // find the center by averaging
        VectorClear(center);
@@ -323,63 +323,21 @@ typedef struct portalrecursioninfo_s
 {
        int exact;
        int numfrustumplanes;
-       float nradius;
-       qbyte *surfacemark;
-       qbyte *leafmark;
+       vec3_t boxmins;
+       vec3_t boxmaxs;
+       int numsurfaces;
+       int *surfacelist;
+       qbyte *surfacepvs;
+       int numleafs;
+       int *leaflist;
+       qbyte *leafpvs;
        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;
-       msurface_t *surf;
-       surfmesh_t *surfmesh;
-       for (i = 0;i < nummarksurfaces;i++, mark++)
-       {
-               if (!info->surfacemark[*mark])
-               {
-                       surf = info->model->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 (surfmesh = surf->mesh;surfmesh;surfmesh = surfmesh->chain)
-                               {
-                                       for (j = 0, elements = surfmesh->element3i;j < surfmesh->numtriangles;j++, elements += 3)
-                                       {
-                                               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;
-                                       }
-                                       if (j < surfmesh->numtriangles)
-                                               break;
-                               }
-                               if (surfmesh == NULL)
-                                       continue;
-                       }
-                       info->surfacemark[*mark] = true;
-               }
-       }
-}
-
 void Portal_RecursiveFlow (portalrecursioninfo_t *info, mleaf_t *leaf, int firstclipplane, int numclipplanes)
 {
        mportal_t *p;
@@ -388,25 +346,80 @@ void Portal_RecursiveFlow (portalrecursioninfo_t *info, mleaf_t *leaf, int first
        vec3_t center, v1, v2;
        tinyplane_t *newplanes;
 
-       if (info->leafmark)
-               info->leafmark[leaf - info->model->leafs] = true;
+       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->leafpvs)
+       {
+               int leafindex = leaf - info->model->brush.data_leafs;
+               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->nummarksurfaces && info->surfacemark)
+       if (leaf->numleafsurfaces && info->surfacepvs)
        {
-               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;
+               for (i = 0;i < leaf->numleafsurfaces;i++)
+               {
+                       int surfaceindex = leaf->firstleafsurface[i];
+                       if (!CHECKPVSBIT(info->surfacepvs, surfaceindex))
+                       {
+                               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;
+                                       }
+                               }
+                       }
+               }
        }
 
        // 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)
@@ -425,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);
@@ -447,12 +460,7 @@ void Portal_RecursiveFlow (portalrecursioninfo_t *info, mleaf_t *leaf, int first
 
 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
+       if (node->plane)
        {
                float f = DotProduct(info->eye, node->plane->normal) - node->plane->dist;
                if (f > -0.1)
@@ -460,21 +468,33 @@ void Portal_RecursiveFindLeafForFlow(portalrecursioninfo_t *info, mnode_t *node)
                if (f < 0.1)
                        Portal_RecursiveFindLeafForFlow(info, node->children[1]);
        }
+       else
+       {
+               mleaf_t *leaf = (mleaf_t *)node;
+               if (leaf->portals)
+                       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, float radius)
+void Portal_Visibility(model_t *model, const vec3_t eye, int *leaflist, qbyte *leafpvs, int *numleafspointer, int *surfacelist, qbyte *surfacepvs, int *numsurfacespointer, 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)
-               Host_Error("Portal_Visibility: NULL model\n");
+       {
+               Con_Print("Portal_Visibility: NULL model\n");
+               return;
+       }
 
        Mod_CheckLoaded(model);
 
-       if (model->type != mod_brush)
-               Host_Error("Portal_Visibility: not a brush model\n");
+       if (!model->brush.num_portals)
+       {
+               Con_Print("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++)
@@ -486,19 +506,30 @@ 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;
+       info.numsurfaces = 0;
+       info.surfacelist = surfacelist;
+       info.surfacepvs = surfacepvs;
+       info.numleafs = 0;
+       info.leaflist = leaflist;
+       info.leafpvs = leafpvs;
        info.model = model;
        VectorCopy(eye, info.eye);
        info.numfrustumplanes = numfrustumplanes;
+       info.updateleafsmins = updateleafsmins;
+       info.updateleafsmaxs = updateleafsmaxs;
 
-       Portal_RecursiveFindLeafForFlow(&info, model->nodes);
+       Portal_RecursiveFindLeafForFlow(&info, model->brush.data_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);
+       if (numsurfacespointer)
+               *numsurfacespointer = info.numsurfaces;
+       if (numleafspointer)
+               *numleafspointer = info.numleafs;
 }