From d309aa02b0acdd389b0f39be9115afa7deda0f7d Mon Sep 17 00:00:00 2001 From: Eric Wasylishen Date: Fri, 22 Apr 2016 19:15:49 -0600 Subject: [PATCH] light: refactor CalcPvs to calculate the face pvs in a cleaner way --- include/light/light.h | 1 + light/ltface.c | 64 +++++++++++++++++++++++++++---------------- 2 files changed, 41 insertions(+), 24 deletions(-) diff --git a/include/light/light.h b/include/light/light.h index 07c39e0a..d6be2905 100644 --- a/include/light/light.h +++ b/include/light/light.h @@ -185,6 +185,7 @@ typedef struct { the pvs at each of the sample points */ byte *pvs; + bool skyvisible; /* for sphere culling */ vec3_t origin; diff --git a/light/ltface.c b/light/ltface.c index c6010ab4..3faab85d 100644 --- a/light/ltface.c +++ b/light/ltface.c @@ -716,38 +716,54 @@ static void CalcPvs(const bsp2_t *bsp, lightsurf_t *lightsurf) { const int pvssize = DecompressedVisSize(bsp); - const vec_t *surfpoint; - const bsp2_dleaf_t *lastleaf = NULL; + // set defaults + lightsurf->pvs = NULL; + lightsurf->skyvisible = true; + if (!bsp->visdatasize) return; - byte *pointpvs = calloc(pvssize, 1); - lightsurf->pvs = calloc(pvssize, 1); + // gather the leafs this face is in + int numfaceleafs = 0; + const bsp2_dleaf_t *faceleafs[64]; - surfpoint = lightsurf->points[0]; - for (int i = 0; i < lightsurf->numpoints; i++, surfpoint += 3) { - const bsp2_dleaf_t *leaf = Light_PointInLeaf (bsp, surfpoint); + for (int i = 0; i < bsp->numleafs; i++) { + const bsp2_dleaf_t *leaf = &bsp->dleafs[i]; - if (leaf == NULL) - continue; - - /* most/all of the surface points are probably in the same leaf */ - if (leaf == lastleaf) - continue; - - lastleaf = leaf; - - /* copy the pvs for this leaf into pointpvs */ - Mod_LeafPvs(bsp, leaf, pointpvs); - - /* merge the pvs for this sample point into lightsurf->pvs */ - for (int j=0; jpvs[j] |= pointpvs[j]; + for (int k = 0; k < leaf->nummarksurfaces; k++) { + const bsp2_dface_t *surf = &bsp->dfaces[bsp->dmarksurfaces[leaf->firstmarksurface + k]]; + + if (surf == lightsurf->face) { + // store it + if (numfaceleafs < 64) { + faceleafs[numfaceleafs++] = leaf; + } else { + logprint("CalcPvs: warning: numfaceleafs == 64\n"); + } + } } } - free(pointpvs); + // set lightsurf->pvs + byte *leafpvs = calloc(pvssize, 1); + lightsurf->pvs = calloc(pvssize, 1); + + for (int i = 0; i < numfaceleafs; i++) { + const bsp2_dleaf_t *leaf = faceleafs[i]; + + /* copy the pvs for this leaf into leafpvs */ + Mod_LeafPvs(bsp, leaf, leafpvs); + + /* merge the pvs for this leaf into lightsurf->pvs */ + for (int j=0; jpvs[j] |= leafpvs[j]; + } + } + + free(leafpvs); + + // set lightsurf->skyvisible } __attribute__((noinline))