From dff71edfe97ac526f4abc000ff711626bf005e92 Mon Sep 17 00:00:00 2001 From: Eric Wasylishen Date: Tue, 3 Oct 2017 00:33:14 -0600 Subject: [PATCH] light: very early q2 support. half of the faces seem to be saved without lightmaps for some reason --- common/bspfile.cc | 2 ++ include/common/bspfile.hh | 1 + include/light/ltface.hh | 2 +- light/light.cc | 17 +++++++++-------- light/ltface.cc | 39 +++++++++++++++++++++++++++------------ 5 files changed, 40 insertions(+), 21 deletions(-) diff --git a/common/bspfile.cc b/common/bspfile.cc index 8669988d..2d4b19f9 100644 --- a/common/bspfile.cc +++ b/common/bspfile.cc @@ -2285,6 +2285,8 @@ LoadBSPFile(char *filename, bspdata_t *bspdata) lumps = q1header->lumps; } + bspdata->loadversion = version; + /* check the file version */ logprint("BSP is version %s\n", BSPVersionString(version)); if (!BSPVersionSupported(version)) diff --git a/include/common/bspfile.hh b/include/common/bspfile.hh index 5a44259e..85ad3649 100644 --- a/include/common/bspfile.hh +++ b/include/common/bspfile.hh @@ -742,6 +742,7 @@ typedef struct { typedef struct { int32_t version; + int32_t loadversion; int hullcount; struct { diff --git a/include/light/ltface.hh b/include/light/ltface.hh index 2fa8c49c..9b0770fa 100644 --- a/include/light/ltface.hh +++ b/include/light/ltface.hh @@ -78,6 +78,6 @@ vec_t GetLightValue(const globalconfig_t &cfg, const light_t *entity, vec_t dist std::map GetDirectLighting(const globalconfig_t &cfg, raystream_t *rs, const vec3_t origin, const vec3_t normal); void SetupDirt(globalconfig_t &cfg); float DirtAtPoint(const globalconfig_t &cfg, raystream_t *rs, const vec3_t point, const vec3_t normal, const modelinfo_t *selfshadow); -void LightFace(const mbsp_t *bsp, bsp2_dface_t *face, facesup_t *facesup, const globalconfig_t &cfg); +void LightFace(bspdata_t *bspdata, bsp2_dface_t *face, facesup_t *facesup, const globalconfig_t &cfg); #endif /* __LIGHT_LTFACE_H__ */ diff --git a/light/light.cc b/light/light.cc index 8339192d..e59854a1 100644 --- a/light/light.cc +++ b/light/light.cc @@ -223,8 +223,9 @@ const modelinfo_t *ModelInfoForFace(const mbsp_t *bsp, int facenum) static void * LightThread(void *arg) { - const mbsp_t *bsp = (const mbsp_t *)arg; - + bspdata_t *bspdata = (bspdata_t *)arg; + mbsp_t *bsp = &bspdata->data.mbsp; + #ifdef HAVE_EMBREE _MM_SET_FLUSH_ZERO_MODE(_MM_FLUSH_ZERO_ON); // _MM_SET_DENORMALS_ZERO_MODE(_MM_DENORMALS_ZERO_ON); @@ -246,24 +247,24 @@ LightThread(void *arg) } if (!faces_sup) - LightFace(bsp, f, nullptr, cfg_static); + LightFace(bspdata, f, nullptr, cfg_static); else if (scaledonly) { f->lightofs = -1; f->styles[0] = 255; - LightFace(bsp, f, faces_sup + facenum, cfg_static); + LightFace(bspdata, f, faces_sup + facenum, cfg_static); } else if (faces_sup[facenum].lmscale == face_modelinfo->lightmapscale) { - LightFace(bsp, f, nullptr, cfg_static); + LightFace(bspdata, f, nullptr, cfg_static); faces_sup[facenum].lightofs = f->lightofs; for (int i = 0; i < MAXLIGHTMAPS; i++) faces_sup[facenum].styles[i] = f->styles[i]; } else { - LightFace(bsp, f, nullptr, cfg_static); - LightFace(bsp, f, faces_sup + facenum, cfg_static); + LightFace(bspdata, f, nullptr, cfg_static); + LightFace(bspdata, f, faces_sup + facenum, cfg_static); } } @@ -425,7 +426,7 @@ LightWorld(bspdata_t *bspdata, qboolean forcedscale) info.bsp = bsp; RunThreadsOn(0, info.all_batches.size(), LightBatchThread, &info); #else - RunThreadsOn(0, bsp->numfaces, LightThread, bsp); + RunThreadsOn(0, bsp->numfaces, LightThread, bspdata); #endif logprint("Lighting Completed.\n\n"); diff --git a/light/ltface.cc b/light/ltface.cc index f0c02142..b071c643 100644 --- a/light/ltface.cc +++ b/light/ltface.cc @@ -2585,9 +2585,11 @@ BoxBlurImage(const std::vector &input, int w, int h, int radius) } static void -WriteLightmaps(const mbsp_t *bsp, bsp2_dface_t *face, facesup_t *facesup, const lightsurf_t *lightsurf, +WriteLightmaps(bspdata_t *bspdata, bsp2_dface_t *face, facesup_t *facesup, const lightsurf_t *lightsurf, const lightmapdict_t *lightmaps) { + mbsp_t *bsp = &bspdata->data.mbsp; + // intermediate collection for sorting lightmaps std::vector> sortable; @@ -2654,6 +2656,11 @@ WriteLightmaps(const mbsp_t *bsp, bsp2_dface_t *face, facesup_t *facesup, const return; int size = (lightsurf->texsize[0] + 1) * (lightsurf->texsize[1] + 1); + + // q2 support + if (bspdata->loadversion == Q2_BSPVERSION) + size *= 3; + byte *out, *lit, *lux; GetFileSpace(&out, &lit, &lux, size * numstyles); if (facesup) { @@ -2702,15 +2709,21 @@ WriteLightmaps(const mbsp_t *bsp, bsp2_dface_t *face, facesup_t *facesup, const *lit++ = color[1]; *lit++ = color[2]; - /* Average the color to get the value to write to the - .bsp lightmap. this avoids issues with some engines - that require the lit and internal lightmap to have the same - intensity. (MarkV, some QW engines) - */ - vec_t light = LightSample_Brightness(color); - if (light < 0) light = 0; - if (light > 255) light = 255; - *out++ = light; + if (bspdata->loadversion == Q2_BSPVERSION) { + *out++ = color[0]; + *out++ = color[1]; + *out++ = color[2]; + } else { + /* Average the color to get the value to write to the + .bsp lightmap. this avoids issues with some engines + that require the lit and internal lightmap to have the same + intensity. (MarkV, some QW engines) + */ + vec_t light = LightSample_Brightness(color); + if (light < 0) light = 0; + if (light > 255) light = 255; + *out++ = light; + } if (lux) { vec3_t temp; @@ -2756,8 +2769,10 @@ static void LightFaceShutdown(lightsurf_t *lightsurf) * ============ */ void -LightFace(const mbsp_t *bsp, bsp2_dface_t *face, facesup_t *facesup, const globalconfig_t &cfg) +LightFace(bspdata_t *bspdata, bsp2_dface_t *face, facesup_t *facesup, const globalconfig_t &cfg) { + mbsp_t *bsp = &bspdata->data.mbsp; + /* Find the correct model offset */ const modelinfo_t *modelinfo = ModelInfoForFace(bsp, Face_GetNum(bsp, face)); if (modelinfo == nullptr) { @@ -2889,7 +2904,7 @@ LightFace(const mbsp_t *bsp, bsp2_dface_t *face, facesup_t *facesup, const globa /* Apply gamma, rangescale, and clamp */ LightFace_ScaleAndClamp(lightsurf, lightmaps); - WriteLightmaps(bsp, face, facesup, lightsurf, lightmaps); + WriteLightmaps(bspdata, face, facesup, lightsurf, lightmaps); LightFaceShutdown(lightsurf); }