Move some code

This commit is contained in:
Magnus Norddahl 2023-10-25 18:11:05 +02:00
commit 4f4d859fd9
5 changed files with 150 additions and 174 deletions

View file

@ -37,6 +37,7 @@ void DoomLevelSubmesh::CreateStatic(FLevelLocals& doomMap)
}
CreateSubsectorSurfaces(doomMap);
LinkSurfaces(doomMap);
CreateIndexes();
SetupLightmapUvs(doomMap);
@ -79,13 +80,13 @@ void DoomLevelSubmesh::UpdateDynamic(FLevelLocals& doomMap, int lightmapStartInd
}
}
LinkSurfaces(doomMap);
CreateIndexes();
SetupLightmapUvs(doomMap);
BuildTileSurfaceLists();
UpdateCollision();
PackLightmapAtlas(lightmapStartIndex);
BindLightmapSurfacesToGeometry(doomMap);
}
void DoomLevelSubmesh::CreateIndexes()
@ -97,6 +98,7 @@ void DoomLevelSubmesh::CreateIndexes()
unsigned int pos = s.startVertIndex;
FFlatVertex* verts = &MeshVertices[pos];
s.Vertices = verts;
s.startElementIndex = MeshElements.Size();
s.numElements = 0;
@ -309,17 +311,12 @@ void DoomLevelSubmesh::CreatePortals()
}
}
void DoomLevelSubmesh::BindLightmapSurfacesToGeometry(FLevelLocals& doomMap)
void DoomLevelSubmesh::LinkSurfaces(FLevelLocals& doomMap)
{
// Link surfaces
for (auto& surface : Surfaces)
{
surface.Vertices = &MeshVertices[surface.startVertIndex];
if (surface.Type == ST_FLOOR || surface.Type == ST_CEILING)
{
if (surface.Subsector->firstline && surface.Subsector->firstline->sidedef)
surface.Subsector->firstline->sidedef->sector->HasLightmaps = true;
SetSubsectorLightmap(&surface);
}
else
@ -331,6 +328,9 @@ void DoomLevelSubmesh::BindLightmapSurfacesToGeometry(FLevelLocals& doomMap)
void DoomLevelSubmesh::SetSubsectorLightmap(DoomLevelMeshSurface* surface)
{
if (surface->Subsector->firstline && surface->Subsector->firstline->sidedef)
surface->Subsector->firstline->sidedef->sector->HasLightmaps = true;
if (!surface->ControlSector)
{
int index = surface->Type == ST_CEILING ? 1 : 0;
@ -1148,118 +1148,9 @@ bool DoomLevelSubmesh::IsDegenerate(const FVector3 &v0, const FVector3 &v1, cons
return crosslengthsqr <= 1.e-6f;
}
void DoomLevelSubmesh::DumpMesh(const FString& objFilename, const FString& mtlFilename) const
{
auto f = fopen(objFilename.GetChars(), "w");
fprintf(f, "# DoomLevelMesh debug export\n");
fprintf(f, "# MeshVertices: %u, MeshElements: %u, Surfaces: %u\n", MeshVertices.Size(), MeshElements.Size(), Surfaces.Size());
fprintf(f, "mtllib %s\n", mtlFilename.GetChars());
double scale = 1 / 10.0;
for (const auto& v : MeshVertices)
{
fprintf(f, "v %f %f %f\n", v.x * scale, v.y * scale, v.z * scale);
}
for (const auto& v : MeshVertices)
{
fprintf(f, "vt %f %f\n", v.lu, v.lv);
}
auto name = [](DoomLevelMeshSurfaceType type) -> const char* {
switch (type)
{
case ST_CEILING:
return "ceiling";
case ST_FLOOR:
return "floor";
case ST_LOWERSIDE:
return "lowerside";
case ST_UPPERSIDE:
return "upperside";
case ST_MIDDLESIDE:
return "middleside";
case ST_UNKNOWN:
return "unknown";
default:
break;
}
return "error";
};
uint32_t lastSurfaceIndex = -1;
bool useErrorMaterial = false;
int highestUsedAtlasPage = -1;
for (unsigned i = 0, count = MeshElements.Size(); i + 2 < count; i += 3)
{
auto index = MeshSurfaceIndexes[i / 3];
if(index != lastSurfaceIndex)
{
lastSurfaceIndex = index;
if (unsigned(index) >= Surfaces.Size())
{
fprintf(f, "o Surface[%d] (bad index)\n", index);
fprintf(f, "usemtl error\n");
useErrorMaterial = true;
}
else
{
const auto& surface = Surfaces[index];
fprintf(f, "o Surface[%d] %s %d%s\n", index, name(surface.Type), surface.TypeIndex, surface.bSky ? " sky" : "");
fprintf(f, "usemtl lightmap%d\n", surface.AtlasTile.ArrayIndex);
if (surface.AtlasTile.ArrayIndex > highestUsedAtlasPage)
{
highestUsedAtlasPage = surface.AtlasTile.ArrayIndex;
}
}
}
// fprintf(f, "f %d %d %d\n", MeshElements[i] + 1, MeshElements[i + 1] + 1, MeshElements[i + 2] + 1);
fprintf(f, "f %d/%d %d/%d %d/%d\n",
MeshElements[i + 0] + 1, MeshElements[i + 0] + 1,
MeshElements[i + 1] + 1, MeshElements[i + 1] + 1,
MeshElements[i + 2] + 1, MeshElements[i + 2] + 1);
}
fclose(f);
// material
f = fopen(mtlFilename.GetChars(), "w");
fprintf(f, "# DoomLevelMesh debug export\n");
if (useErrorMaterial)
{
fprintf(f, "# Surface indices that are referenced, but do not exists in the 'Surface' array\n");
fprintf(f, "newmtl error\nKa 1 0 0\nKd 1 0 0\nKs 1 0 0\n");
}
for (int page = 0; page <= highestUsedAtlasPage; ++page)
{
fprintf(f, "newmtl lightmap%d\n", page);
fprintf(f, "Ka 1 1 1\nKd 1 1 1\nKs 0 0 0\n");
fprintf(f, "map_Ka lightmap%d.png\n", page);
fprintf(f, "map_Kd lightmap%d.png\n", page);
}
fclose(f);
}
void DoomLevelSubmesh::SetupLightmapUvs(FLevelLocals& doomMap)
{
LMTextureSize = 1024; // TODO cvar
LMTextureSize = 1024;
for (auto& surface : Surfaces)
{