Flip surface sort order for better lightmap packing and add some border between the surfaces

This commit is contained in:
Magnus Norddahl 2021-11-14 19:54:44 +01:00
parent 346f71e60a
commit e61f10af24
3 changed files with 17 additions and 16 deletions

View file

@ -890,7 +890,7 @@ void FProcessor::WriteUDMF(FWadWriter &out)
if (LightmapMesh) if (LightmapMesh)
{ {
LightmapMesh->AddLightmapLump(out); LightmapMesh->AddLightmapLump(out);
// LightmapMesh->ExportMesh("level.obj"); //LightmapMesh->Export("level.obj");
} }
out.CreateLabel("ENDMAP"); out.CreateLabel("ENDMAP");

View file

@ -171,17 +171,17 @@ void LevelMesh::BuildSurfaceParams(Surface* surface)
} }
// clamp width // clamp width
if (width > textureWidth) if (width > textureWidth - 2)
{ {
tCoords[0] *= ((float)textureWidth / (float)width); tCoords[0] *= ((float)(textureWidth - 2) / (float)width);
width = textureWidth; width = (textureWidth - 2);
} }
// clamp height // clamp height
if (height > textureHeight) if (height > textureHeight - 2)
{ {
tCoords[1] *= ((float)textureHeight / (float)height); tCoords[1] *= ((float)(textureHeight - 2) / (float)height);
height = textureHeight; height = (textureHeight - 2);
} }
surface->lightmapCoords.resize(surface->numVerts * 2); surface->lightmapCoords.resize(surface->numVerts * 2);
@ -250,7 +250,7 @@ void LevelMesh::CreateTextures()
sortedSurfaces.reserve(surfaces.size()); sortedSurfaces.reserve(surfaces.size());
for (auto& surf : surfaces) for (auto& surf : surfaces)
sortedSurfaces.push_back(surf.get()); sortedSurfaces.push_back(surf.get());
std::sort(sortedSurfaces.begin(), sortedSurfaces.end(), [](Surface* a, Surface* b) { return a->lightmapDims[1] > b->lightmapDims[1]; }); std::sort(sortedSurfaces.begin(), sortedSurfaces.end(), [](Surface* a, Surface* b) { return a->lightmapDims[1] < b->lightmapDims[1]; });
for (Surface* surf : sortedSurfaces) for (Surface* surf : sortedSurfaces)
{ {
@ -299,7 +299,11 @@ void LevelMesh::FinishSurface(Surface* surface)
else else
{ {
int x = 0, y = 0; int x = 0, y = 0;
uint16_t* currentTexture = AllocTextureRoom(surface, &x, &y); surface->lightmapNum = AllocTextureRoom(sampleWidth + 2, sampleHeight + 2, &x, &y);
x++;
y++;
uint16_t* currentTexture = textures[surface->lightmapNum]->Pixels();
// calculate texture coordinates // calculate texture coordinates
for (int i = 0; i < surface->numVerts; i++) for (int i = 0; i < surface->numVerts; i++)
@ -361,10 +365,8 @@ void LevelMesh::FinishSurface(Surface* surface)
} }
} }
uint16_t* LevelMesh::AllocTextureRoom(Surface* surface, int* x, int* y) int LevelMesh::AllocTextureRoom(int width, int height, int* x, int* y)
{ {
int width = surface->lightmapDims[0];
int height = surface->lightmapDims[1];
int numTextures = textures.size(); int numTextures = textures.size();
int k; int k;
@ -385,8 +387,7 @@ uint16_t* LevelMesh::AllocTextureRoom(Surface* surface, int* x, int* y)
} }
} }
surface->lightmapNum = k; return k;
return textures[surface->lightmapNum]->Pixels();
} }
void LevelMesh::CreateLightProbes(FLevel& map) void LevelMesh::CreateLightProbes(FLevel& map)

View file

@ -85,7 +85,7 @@ public:
#else #else
mPixels.resize(width * height * 3, 0); mPixels.resize(width * height * 3, 0);
#endif #endif
allocBlocks.resize(height); allocBlocks.resize(width);
} }
bool MakeRoomForBlock(const int width, const int height, int* x, int* y) bool MakeRoomForBlock(const int width, const int height, int* x, int* y)
@ -168,7 +168,7 @@ private:
void BuildSurfaceParams(Surface* surface); void BuildSurfaceParams(Surface* surface);
BBox GetBoundsFromSurface(const Surface* surface); BBox GetBoundsFromSurface(const Surface* surface);
void FinishSurface(Surface* surface); void FinishSurface(Surface* surface);
uint16_t* AllocTextureRoom(Surface* surface, int* x, int* y); int AllocTextureRoom(int width, int height, int* x, int* y);
static bool IsDegenerate(const vec3 &v0, const vec3 &v1, const vec3 &v2); static bool IsDegenerate(const vec3 &v0, const vec3 &v1, const vec3 &v2);
}; };