Skip to content

Commit

Permalink
Uplodate lightmap resize functions
Browse files Browse the repository at this point in the history
  • Loading branch information
Karaulov committed Dec 15, 2022
1 parent 302d4af commit 66ca684
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 29 deletions.
63 changes: 35 additions & 28 deletions src/bsp/Bsp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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++)
{
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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++)
{
Expand Down Expand Up @@ -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)
Expand Down
5 changes: 4 additions & 1 deletion src/bsp/Bsp.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ class Bsp
Bsp* parentMap = NULL;
void selectModelEnt();

int clipnodetype; // 0 - default; 1 -

int planeCount;
int texinfoCount;
int leafCount;
Expand Down Expand Up @@ -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);

Expand Down

0 comments on commit 66ca684

Please sign in to comment.