diff --git a/include/light/light.h b/include/light/light.h index d6be2905..002f5f17 100644 --- a/include/light/light.h +++ b/include/light/light.h @@ -299,15 +299,20 @@ Face_TextureName(const bsp2_t *bsp, const bsp2_dface_t *face); void Face_MakeInwardFacingEdgePlanes(const bsp2_t *bsp, const bsp2_dface_t *face, plane_t *out); + +/* vis testing */ +const bsp2_dleaf_t *Light_PointInLeaf( const bsp2_t *bsp, const vec3_t point ); +int Light_PointContents( const bsp2_t *bsp, const vec3_t point ); +void Mod_LeafPvs(const bsp2_t *bsp, const bsp2_dleaf_t *leaf, byte *out); +int DecompressedVisSize(const bsp2_t *bsp); +bool Pvs_LeafVisible(const bsp2_t *bsp, const byte *pvs, const bsp2_dleaf_t *leaf); + +/* PVS index (light.cc) */ +bool Leaf_HasSky(const bsp2_t *bsp, const bsp2_dleaf_t *leaf); +const bsp2_dleaf_t **Face_CopyLeafList(const bsp2_t *bsp, const bsp2_dface_t *face); #ifdef __cplusplus } #endif -/* vis testing */ -const bsp2_dleaf_t *Light_PointInLeaf( const bsp2_t *bsp, const vec3_t point ); -int PointInLeafnum (const bsp2_t *bsp, const vec3_t point); -void PvsForOrigin (const bsp2_t *bsp, const vec3_t org, byte *pvs); -int Light_PointContents( const bsp2_t *bsp, const vec3_t point ); - #endif /* __LIGHT_LIGHT_H__ */ diff --git a/light/light.cc b/light/light.cc index ea256507..8ac59cd1 100644 --- a/light/light.cc +++ b/light/light.cc @@ -28,6 +28,8 @@ #include #include +using namespace std; + float scaledist = 1.0; float rangescale = 0.5; float global_anglescale = 0.5; @@ -754,6 +756,57 @@ ExportObj(const char *filename, const bsp2_t *bsp) //obj +vector> faceleafs; +vector leafhassky; + +// index some stuff from the bsp +void BuildPvsIndex(const bsp2_t *bsp) +{ + // build leafsForFace + faceleafs.resize(bsp->numfaces); + for (int i = 0; i < bsp->numleafs; i++) { + const bsp2_dleaf_t *leaf = &bsp->dleafs[i]; + for (int k = 0; k < leaf->nummarksurfaces; k++) { + const int facenum = bsp->dmarksurfaces[leaf->firstmarksurface + k]; + faceleafs.at(facenum).push_back(leaf); + } + } + + // build leafhassky + leafhassky.resize(bsp->numleafs, false); + for (int i = 0; i < bsp->numleafs; i++) { + const bsp2_dleaf_t *leaf = &bsp->dleafs[i]; + + // search for sky faces in it + for (int k = 0; k < leaf->nummarksurfaces; k++) { + const bsp2_dface_t *surf = &bsp->dfaces[bsp->dmarksurfaces[leaf->firstmarksurface + k]]; + const char *texname = Face_TextureName(bsp, surf); + if (!strncmp("sky", texname, 3)) { + leafhassky.at(i) = true; + break; + } + } + } +} + +bool Leaf_HasSky(const bsp2_t *bsp, const bsp2_dleaf_t *leaf) +{ + const int leafnum = leaf - bsp->dleafs; + return leafhassky.at(leafnum); +} + +const bsp2_dleaf_t **Face_CopyLeafList(const bsp2_t *bsp, const bsp2_dface_t *face) +{ + const int facenum = face - bsp->dfaces; + auto &leafs = faceleafs.at(facenum); + + const bsp2_dleaf_t **result = (const bsp2_dleaf_t **) calloc(leafs.size() + 1, sizeof(const bsp2_dleaf_t *)); + for (int i = 0; idmodels[0].visleafs + 7) / 8; @@ -671,7 +671,7 @@ static void Mod_Q1BSP_DecompressVis(const unsigned char *in, const unsigned char } } -static void +void Mod_LeafPvs(const bsp2_t *bsp, const bsp2_dleaf_t *leaf, byte *out) { const int num_pvsclusterbytes = DecompressedVisSize(bsp); @@ -701,7 +701,7 @@ Mod_LeafPvs(const bsp2_t *bsp, const bsp2_dleaf_t *leaf, byte *out) } // returns true if pvs can see leaf -static bool +bool Pvs_LeafVisible(const bsp2_t *bsp, const byte *pvs, const bsp2_dleaf_t *leaf) { const int leafnum = (leaf - bsp->dleafs); @@ -712,7 +712,7 @@ Pvs_LeafVisible(const bsp2_t *bsp, const byte *pvs, const bsp2_dleaf_t *leaf) return !!(pvs[visleaf>>3] & (1<<(visleaf&7))); } -void +static void CalcPvs(const bsp2_t *bsp, lightsurf_t *lightsurf) { const int pvssize = DecompressedVisSize(bsp); @@ -724,39 +724,22 @@ CalcPvs(const bsp2_t *bsp, lightsurf_t *lightsurf) if (!bsp->visdatasize) return; // gather the leafs this face is in - int numfaceleafs = 0; - const bsp2_dleaf_t *faceleafs[64]; - - for (int i = 0; i < bsp->numleafs; i++) { - const bsp2_dleaf_t *leaf = &bsp->dleafs[i]; - - 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"); - } - } - } - } + const bsp2_dleaf_t **faceleafs = Face_CopyLeafList(bsp, lightsurf->face); // set lightsurf->pvs byte *leafpvs = calloc(pvssize, 1); lightsurf->pvs = calloc(pvssize, 1); - for (int i = 0; i < numfaceleafs; i++) { + for (int i = 0; ; i++) { const bsp2_dleaf_t *leaf = faceleafs[i]; + if (!leaf) + break; /* 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]; } } @@ -769,18 +752,11 @@ CalcPvs(const bsp2_t *bsp, lightsurf_t *lightsurf) const bsp2_dleaf_t *leaf = &bsp->dleafs[i]; if (Pvs_LeafVisible(bsp, lightsurf->pvs, leaf)) { // we can see this leaf, search for sky faces in it - for (int k = 0; k < leaf->nummarksurfaces; k++) { - const bsp2_dface_t *surf = &bsp->dfaces[bsp->dmarksurfaces[leaf->firstmarksurface + k]]; - const char *texname = Face_TextureName(bsp, surf); - if (!strncmp("sky", texname, 3)) { - lightsurf->skyvisible = true; - break; - } + if (Leaf_HasSky(bsp, leaf)) { + lightsurf->skyvisible = true; + break; } } - - if (lightsurf->skyvisible) - break; // we are done } }