diff --git a/include/light/light.h b/include/light/light.h index 5aa4f7b8..dfba8aa4 100644 --- a/include/light/light.h +++ b/include/light/light.h @@ -223,6 +223,9 @@ extern lightsample_t minlight; extern sun_t *suns; +extern int dump_facenum; +extern bool dump_face; + /* command-line options */ typedef struct { @@ -294,6 +297,8 @@ Face_MakeInwardFacingEdgePlanes(const bsp2_t *bsp, const bsp2_dface_t *face, pla plane_t Face_Plane(const bsp2_t *bsp, const bsp2_dface_t *f); void Face_Normal(const bsp2_t *bsp, const bsp2_dface_t *f, vec3_t norm); +void FaceCentroid(const bsp2_dface_t *face, const bsp2_t *bsp, vec3_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 ); diff --git a/light/light.cc b/light/light.cc index c89e128f..112060e8 100644 --- a/light/light.cc +++ b/light/light.cc @@ -104,6 +104,10 @@ char mapfilename[1024]; struct ltface_ctx *ltface_ctxs; +int dump_facenum = -1; +bool dump_face; +vec3_t dump_face_point = {0,0,0}; + void GetFileSpace(byte **lightdata, byte **colordata, byte **deluxdata, int size) { @@ -1185,6 +1189,47 @@ CheckNoDebugModeSet() } } +// returns the face with a centroid nearest the given point. +static const bsp2_dface_t * +Face_NearestCentroid(const bsp2_t *bsp, const vec3_t point) +{ + const bsp2_dface_t *nearest_face = NULL; + vec_t nearest_dist = VECT_MAX; + + for (int i=0; inumfaces; i++) { + const bsp2_dface_t *f = &bsp->dfaces[i]; + + vec3_t fc; + FaceCentroid(f, bsp, fc); + + vec3_t distvec; + VectorSubtract(fc, point, distvec); + vec_t dist = VectorLength(distvec); + + if (dist < nearest_dist) { + nearest_dist = dist; + nearest_face = f; + } + } + + return nearest_face; +} + +void FindDebugFace(const bsp2_t *bsp) +{ + if (!dump_face) + return; + + const bsp2_dface_t *f = Face_NearestCentroid(bsp, dump_face_point); + if (f == NULL) + Error("FindDebugFace: f == NULL\n"); + + const int facenum = f - bsp->dfaces; + + logprint("FindDebugFace: dumping face %d\n", facenum); + dump_facenum = facenum; +} + /* * ================== * main @@ -1394,6 +1439,20 @@ main(int argc, const char **argv) } else if ( !strcmp( argv[ i ], "-novis" ) ) { novis = true; logprint( "Skipping use of vis data to optimize lighting\n" ); + } else if ( !strcmp( argv[ i ], "-debugface" ) ) { + + vec3_t point; + if ((i + 3) < argc) { + point[0] = atof( argv[ ++i ] ); + point[1] = atof( argv[ ++i ] ); + point[2] = atof( argv[ ++i ] ); + } else { + Error("-debugface requires x y z coordinates\n"); + } + + VectorCopy(point, dump_face_point); + dump_face = true; + } else if (argv[i][0] == '-') Error("Unknown option \"%s\"", argv[i]); else @@ -1514,6 +1573,8 @@ main(int argc, const char **argv) LoadExtendedTexinfoFlags(source, bsp); LoadEntities(bsp); + FindDebugFace(bsp); + modelinfo = (modelinfo_t *)malloc(bsp->nummodels * sizeof(*modelinfo)); FindModelInfo(bsp, lmscaleoverride); SetupLights(bsp); diff --git a/light/ltface.c b/light/ltface.c index 080d516f..5f737816 100644 --- a/light/ltface.c +++ b/light/ltface.c @@ -156,7 +156,7 @@ TriArea(const dvertex_t *v0, const dvertex_t *v1, const dvertex_t *v2) return VectorLength(cross) * 0.5; } -static void +void FaceCentroid(const bsp2_dface_t *face, const bsp2_t *bsp, vec3_t out) { int i, edgenum; @@ -619,6 +619,34 @@ CheckObstructed(const lightsurf_t *surf, const vec3_t offset, const vec_t us, co return false; } +// Dump points to a .map file +void +CalcPoints_Debug(const lightsurf_t *surf, const bsp2_t *bsp) +{ + const int facenum = surf->face - bsp->dfaces; + FILE *f = fopen("calcpoints.map", "w"); + + for (int t = 0; t < surf->height; t++) { + for (int s = 0; s < surf->width; s++) { + const int i = t*surf->width + s; + const vec_t *point = surf->points[i]; + + fprintf(f, "{\n"); + fprintf(f, "\"classname\" \"light\"\n"); + fprintf(f, "\"origin\" \"%f %f %f\"\n", point[0], point[1], point[2]); + fprintf(f, "\"face\" \"%d\"\n", facenum); + fprintf(f, "\"s\" \"%d\"\n", s); + fprintf(f, "\"t\" \"%d\"\n", t); + fprintf(f, "}\n"); + } + } + + fclose(f); + + logprint("wrote face %d's sample points (%dx%d) to calcpoints.map\n", + facenum, surf->width, surf->height); +} + /* * ================= * CalcPoints @@ -675,6 +703,11 @@ CalcPoints(const modelinfo_t *modelinfo, const vec3_t offset, lightsurf_t *surf, CheckObstructed(surf, offset, us, ut, point); } } + + const int facenum = (face - bsp->dfaces); + if (dump_facenum == facenum) { + CalcPoints_Debug(surf, bsp); + } } int