From dec222c9fe869a0fec227964c2a6e227ac4d628b Mon Sep 17 00:00:00 2001 From: RaveYard <29225776+MrRaveYard@users.noreply.github.com> Date: Thu, 14 Sep 2023 20:41:34 +0200 Subject: [PATCH] Use UVs from the lightmap --- .../rendering/hwrenderer/data/hw_levelmesh.h | 1 - src/maploader/maploader.cpp | 28 +++++++++- src/rendering/hwrenderer/doom_levelmesh.cpp | 51 +++++++++---------- 3 files changed, 50 insertions(+), 30 deletions(-) diff --git a/src/common/rendering/hwrenderer/data/hw_levelmesh.h b/src/common/rendering/hwrenderer/data/hw_levelmesh.h index 0a01f4e29f..7824fae23f 100644 --- a/src/common/rendering/hwrenderer/data/hw_levelmesh.h +++ b/src/common/rendering/hwrenderer/data/hw_levelmesh.h @@ -87,7 +87,6 @@ struct LevelMeshSurface // VkLightmap extra stuff that I dislike: // TArray verts; - TArray uvs; // Touching light sources std::vector LightList; diff --git a/src/maploader/maploader.cpp b/src/maploader/maploader.cpp index 8371285718..938de75bba 100644 --- a/src/maploader/maploader.cpp +++ b/src/maploader/maploader.cpp @@ -3157,7 +3157,7 @@ void MapLoader::LoadLightmap(MapData* map) continue; } - if (developer >= 4) + if (developer >= 5) { Printf("Mapping lightmap surface pixels[%u] (count: %u) -> ((x:%d, y:%d), (x2:%d, y2:%d), page:%d) area: %u\n", srcPixelOffset, surface.width * surface.height * 3, @@ -3188,6 +3188,32 @@ void MapLoader::LoadLightmap(MapData* map) } } + // Use UVs from the lightmap + for (auto& surface : zdraySurfaces) + { + auto& realSurface = Level->levelMesh->Surfaces[findSurfaceIndex(surface.type, surface.typeIndex, getControlSector(surface.controlSector))]; + + auto* UVs = &Level->levelMesh->LightmapUvs[realSurface.startUvIndex]; + auto* newUVs = &zdrayUvs[surface.uvOffset]; + + for (uint32_t i = 0; i < surface.uvCount; ++i) + { + if (developer >= 5) + { + Printf("Old UV: %.6f %.6f (w:%d, h:%d) (x:%d, y:%d), Lump UVs %.3f %.3f\n", UVs[i].X, UVs[i].Y, realSurface.texWidth, realSurface.texHeight, realSurface.atlasX, realSurface.atlasY, newUVs[i].X, newUVs[i].Y); + } + + // Finish surface + UVs[i].X = (newUVs[i].X + realSurface.atlasX) / textureSize; + UVs[i].Y = (newUVs[i].Y + realSurface.atlasY) / textureSize; + + if (developer >= 5) + { + Printf("New UV: %.6f %.6f\n", UVs[i].X, UVs[i].Y); + } + } + } + if (developer >= 3) { int loadedSurfaces = 0; diff --git a/src/rendering/hwrenderer/doom_levelmesh.cpp b/src/rendering/hwrenderer/doom_levelmesh.cpp index aca3a1a539..f1a7a667fb 100644 --- a/src/rendering/hwrenderer/doom_levelmesh.cpp +++ b/src/rendering/hwrenderer/doom_levelmesh.cpp @@ -97,7 +97,7 @@ CCMD(surfaceinfo) } auto posXYZ = FVector3(pov->Pos()); - posXYZ.Z = players[consoleplayer].viewz; + posXYZ.Z = float(players[consoleplayer].viewz); auto angle = pov->Angles.Yaw; auto pitch = pov->Angles.Pitch; @@ -401,6 +401,28 @@ void DoomLevelMesh::UpdateLightLists() void DoomLevelMesh::BindLightmapSurfacesToGeometry(FLevelLocals& doomMap) { + // You have no idea how long this took me to figure out... + + // Reorder vertices into renderer format + for (LevelMeshSurface& surface : Surfaces) + { + if (surface.Type == ST_FLOOR) + { + // reverse vertices on floor + for (int j = surface.startUvIndex + surface.numVerts - 1, k = surface.startUvIndex; j > k; j--, k++) + { + std::swap(LightmapUvs[k], LightmapUvs[j]); + } + } + else if (surface.Type != ST_CEILING) // walls + { + // from 0 1 2 3 + // to 0 2 1 3 + std::swap(LightmapUvs[surface.startUvIndex + 1], LightmapUvs[surface.startUvIndex + 2]); + std::swap(LightmapUvs[surface.startUvIndex + 2], LightmapUvs[surface.startUvIndex + 3]); + } + } + // Allocate room for all surfaces unsigned int allSurfaces = 0; @@ -1064,11 +1086,6 @@ void DoomLevelMesh::SetupLightmapUvs() { surface.verts.Push(MeshVertices[surface.startVertIndex + i]); } - - for (int i = 0; i < surface.numVerts; ++i) - { - surface.uvs.Push(LightmapUvs[surface.startUvIndex + i]); - } } BuildSmoothingGroups(); @@ -1082,28 +1099,6 @@ void DoomLevelMesh::SetupLightmapUvs() FinishSurface(LMTextureSize, LMTextureSize, packer, *surf); } - // You have no idea how long this took me to figure out... - - // Reorder vertices into renderer format - for (LevelMeshSurface& surface : Surfaces) - { - if (surface.Type == ST_FLOOR) - { - // reverse vertices on floor - for (int j = surface.startUvIndex + surface.numVerts - 1, k = surface.startUvIndex; j > k; j--, k++) - { - std::swap(LightmapUvs[k], LightmapUvs[j]); - } - } - else if (surface.Type != ST_CEILING) // walls - { - // from 0 1 2 3 - // to 0 2 1 3 - std::swap(LightmapUvs[surface.startUvIndex + 1], LightmapUvs[surface.startUvIndex + 2]); - std::swap(LightmapUvs[surface.startUvIndex + 2], LightmapUvs[surface.startUvIndex + 3]); - } - } - LMTextureCount = (int)packer.getNumPages(); }