From 66ca6846eb99e325de1a09e5efda034f5bf50f6c Mon Sep 17 00:00:00 2001 From: Karaulov Date: Thu, 15 Dec 2022 18:05:46 +0300 Subject: [PATCH] Uplodate lightmap resize functions --- src/bsp/Bsp.cpp | 63 +++++++++++++++++++++++++++---------------------- src/bsp/Bsp.h | 5 +++- 2 files changed, 39 insertions(+), 29 deletions(-) diff --git a/src/bsp/Bsp.cpp b/src/bsp/Bsp.cpp index 4576ce4b..312860c6 100644 --- a/src/bsp/Bsp.cpp +++ b/src/bsp/Bsp.cpp @@ -838,29 +838,7 @@ bool Bsp::move(vec3 offset, int modelIdx, bool onlyModel, bool forceMove, bool l memset(oldLightmaps, 0, sizeof(LIGHTMAP) * faceCount); memset(newLightmaps, 0, sizeof(LIGHTMAP) * faceCount); - for (int i = 0; i < faceCount; i++) - { - int size[2]; - GetFaceLightmapSize(this, i, size); - - int lightmapSz = size[0] * size[1]; - int lightmapCount = lightmap_count(i); - oldLightmaps[i].layers = lightmapCount; - lightmapSz *= lightmapCount; - - oldLightmaps[i].width = size[0]; - oldLightmaps[i].height = size[1]; - - bool skipResize = i < target.iFirstFace || i >= target.iFirstFace + target.nFaces; - - if (!skipResize) - { - oldLightmaps[i].luxelFlags = new unsigned char[size[0] * size[1]]; - qrad_get_lightmap_flags(this, i, oldLightmaps[i].luxelFlags); - } - if (logged) - g_progress.tick(); - } + get_lightmaps(oldLightmaps, &target, logged); } if (logged) @@ -1011,7 +989,9 @@ bool Bsp::move(vec3 offset, int modelIdx, bool onlyModel, bool forceMove, bool l if (hasLighting && oldLightmaps && newLightmaps) { - resize_lightmaps(oldLightmaps, newLightmaps); + int newLightmapsSize = 0; + resize_lightmaps(oldLightmaps, newLightmaps, newLightmapsSize); + replace_lump(LUMP_LIGHTING, newLightmaps, newLightmapsSize); for (int i = 0; i < faceCount; i++) { @@ -1072,10 +1052,39 @@ void Bsp::move_texinfo(int idx, vec3 offset) } } -void Bsp::resize_lightmaps(LIGHTMAP* oldLightmaps, LIGHTMAP* newLightmaps) +void Bsp::get_lightmaps(LIGHTMAP* outLightmaps, BSPMODEL* target, bool logged) +{ + for (int i = 0; i < faceCount; i++) + { + int size[2]; + GetFaceLightmapSize(this, i, size); + + int lightmapSz = size[0] * size[1]; + int lightmapCount = lightmap_count(i); + outLightmaps[i].layers = lightmapCount; + lightmapSz *= lightmapCount; + + outLightmaps[i].width = size[0]; + outLightmaps[i].height = size[1]; + + bool skipResize = target ? (i < target->iFirstFace || i >= target->iFirstFace + target->nFaces) : false; + + if (!skipResize) + { + outLightmaps[i].luxelFlags = new unsigned char[size[0] * size[1]]; + qrad_get_lightmap_flags(this, i, outLightmaps[i].luxelFlags); + } + if (logged) + g_progress.tick(); + } +} + +void Bsp::resize_lightmaps(LIGHTMAP* oldLightmaps, LIGHTMAP* newLightmaps, int& newLightmapSize) { g_progress.update("Recalculate lightmaps", faceCount); + int lightmapOffset = 0; + // calculate new lightmap sizes int newLightDataSz = 0; int totalLightmaps = 0; @@ -1114,7 +1123,6 @@ void Bsp::resize_lightmaps(LIGHTMAP* oldLightmaps, LIGHTMAP* newLightmaps) int newColorCount = newLightDataSz / sizeof(COLOR3); COLOR3* newLightData = new COLOR3[newColorCount]; memset(newLightData, 255, newColorCount * sizeof(COLOR3)); - int lightmapOffset = 0; for (int i = 0; i < faceCount; i++) { @@ -1190,9 +1198,8 @@ void Bsp::resize_lightmaps(LIGHTMAP* oldLightmaps, LIGHTMAP* newLightmaps) face.nLightmapOffset = lightmapOffset; lightmapOffset += newSz; } - - replace_lump(LUMP_LIGHTING, newLightData, lightmapOffset); } + newLightmapSize = lightmapOffset; } void Bsp::split_shared_model_structures(int modelIdx) diff --git a/src/bsp/Bsp.h b/src/bsp/Bsp.h index 8eb5298a..f4d03a62 100644 --- a/src/bsp/Bsp.h +++ b/src/bsp/Bsp.h @@ -56,6 +56,8 @@ class Bsp Bsp* parentMap = NULL; void selectModelEnt(); + int clipnodetype; // 0 - default; 1 - + int planeCount; int texinfoCount; int leafCount; @@ -254,7 +256,8 @@ class Bsp unsigned int remove_unused_textures(bool* usedTextures, int* remappedIndexes); unsigned int remove_unused_structs(int lumpIdx, bool* usedStructs, int* remappedIndexes); - void resize_lightmaps(LIGHTMAP* oldLightmaps, LIGHTMAP* newLightmaps); + void get_lightmaps(LIGHTMAP* outLightmaps, BSPMODEL* target, bool logged = false); + void resize_lightmaps(LIGHTMAP* oldLightmaps, LIGHTMAP* newLightmaps, int & newLightmapSize); bool load_lumps(std::string fname);