do not generate vertexposition/vertexmesh arrays in
[divverent/darkplaces.git] / portals.c
index 427a15a..3b770f9 100644 (file)
--- a/portals.c
+++ b/portals.c
@@ -15,23 +15,22 @@ static float boxpoints[4*3];
 
 static 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++)
+       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++)
        {
-               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));
+               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);
        }
-       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;
 }
 
@@ -128,7 +127,7 @@ 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;
@@ -140,7 +139,6 @@ int Portal_CheckPolygon(model_t *model, vec3_t eye, float *polypoints, int numpo
 
        portal_markid++;
 
-       Mod_CheckLoaded(model);
        Portal_PolygonRecursiveMarkLeafs(model->brush.data_nodes, polypoints, numpoints);
 
        eyeleaf = model->brush.PointInLeaf(model, eye);
@@ -206,7 +204,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)
@@ -273,11 +271,14 @@ typedef struct portalrecursioninfo_s
        vec3_t boxmaxs;
        int numsurfaces;
        int *surfacelist;
-       qbyte *surfacepvs;
+       unsigned char *surfacepvs;
        int numleafs;
+       unsigned char *visitingleafpvs; // used to prevent infinite loops
        int *leaflist;
-       qbyte *leafpvs;
-       model_t *model;
+       unsigned char *leafpvs;
+       unsigned char *shadowtrispvs;
+       unsigned char *lighttrispvs;
+       dp_model_t *model;
        vec3_t eye;
        float *updateleafsmins;
        float *updateleafsmaxs;
@@ -291,6 +292,12 @@ static void Portal_RecursiveFlow (portalrecursioninfo_t *info, mleaf_t *leaf, in
        float dist;
        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++)
        {
@@ -301,7 +308,6 @@ static void Portal_RecursiveFlow (portalrecursioninfo_t *info, mleaf_t *leaf, in
 
        if (info->leafpvs)
        {
-               int leafindex = leaf - info->model->brush.data_leafs;
                if (!CHECKPVSBIT(info->leafpvs, leafindex))
                {
                        SETPVSBIT(info->leafpvs, leafindex);
@@ -315,47 +321,38 @@ static void Portal_RecursiveFlow (portalrecursioninfo_t *info, mleaf_t *leaf, in
                for (i = 0;i < leaf->numleafsurfaces;i++)
                {
                        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->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)
                                {
-                                       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
+                                       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 || TriangleOverlapsBox(v, v + 3, v + 6, info->boxmins, info->boxmaxs))
+                                        && (!info->exact || Portal_PortalThroughPortalPlanes(&portalplanes[firstclipplane], numclipplanes, v, 3, &portaltemppoints2[0][0], 256) > 0))
                                        {
-                                               SETPVSBIT(info->surfacepvs, surfaceindex);
-                                               info->surfacelist[info->numsurfaces++] = surfaceindex;
+                                               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;
+                               }
                        }
                }
        }
@@ -400,6 +397,8 @@ static void Portal_RecursiveFlow (portalrecursioninfo_t *info, mleaf_t *leaf, in
                        }
                }
        }
+
+       CLEARPVSBIT(info->visitingleafpvs, leafindex);
 }
 
 static void Portal_RecursiveFindLeafForFlow(portalrecursioninfo_t *info, mnode_t *node)
@@ -420,7 +419,7 @@ static void Portal_RecursiveFindLeafForFlow(portalrecursioninfo_t *info, mnode_t
        }
 }
 
-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)
+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;
@@ -432,8 +431,6 @@ void Portal_Visibility(model_t *model, const vec3_t eye, int *leaflist, qbyte *l
                return;
        }
 
-       Mod_CheckLoaded(model);
-
        if (!model->brush.data_nodes)
        {
                Con_Print("Portal_Visibility: not a brush model\n");
@@ -457,6 +454,7 @@ void Portal_Visibility(model_t *model, const vec3_t eye, int *leaflist, qbyte *l
        info.surfacelist = surfacelist;
        info.surfacepvs = surfacepvs;
        info.numleafs = 0;
+       info.visitingleafpvs = visitingleafpvs;
        info.leaflist = leaflist;
        info.leafpvs = leafpvs;
        info.model = model;
@@ -464,6 +462,8 @@ void Portal_Visibility(model_t *model, const vec3_t eye, int *leaflist, qbyte *l
        info.numfrustumplanes = numfrustumplanes;
        info.updateleafsmins = updateleafsmins;
        info.updateleafsmaxs = updateleafsmaxs;
+       info.shadowtrispvs = shadowtrispvs;
+       info.lighttrispvs = lighttrispvs;
 
        Portal_RecursiveFindLeafForFlow(&info, model->brush.data_nodes);