mirror of
https://github.com/ZDoom/ZDRay.git
synced 2025-02-13 15:11:11 +00:00
- remove some code duplication
This commit is contained in:
parent
d03ded7f94
commit
28d7bb586f
2 changed files with 70 additions and 88 deletions
|
@ -54,6 +54,50 @@ LightmapBuilder::~LightmapBuilder()
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void LightmapBuilder::CreateLightmaps(FLevel &doomMap, int sampleDistance, int textureSize)
|
||||||
|
{
|
||||||
|
map = &doomMap;
|
||||||
|
samples = sampleDistance;
|
||||||
|
textureWidth = textureSize;
|
||||||
|
textureHeight = textureSize;
|
||||||
|
|
||||||
|
mesh = std::make_unique<LevelMesh>(doomMap);
|
||||||
|
|
||||||
|
CreateSurfaceLights();
|
||||||
|
CreateTraceTasks();
|
||||||
|
|
||||||
|
SetupLightCellGrid();
|
||||||
|
|
||||||
|
SetupTaskProcessed("Tracing cells", grid.blocks.size());
|
||||||
|
Worker::RunJob(grid.blocks.size(), [=](int id) {
|
||||||
|
LightBlock(id);
|
||||||
|
PrintTaskProcessed();
|
||||||
|
});
|
||||||
|
printf("Cells traced: %i \n\n", tracedTexels);
|
||||||
|
|
||||||
|
SetupTaskProcessed("Tracing surfaces", traceTasks.size());
|
||||||
|
Worker::RunJob(traceTasks.size(), [=](int id) {
|
||||||
|
LightSurface(id);
|
||||||
|
PrintTaskProcessed();
|
||||||
|
});
|
||||||
|
printf("Texels traced: %i \n\n", tracedTexels);
|
||||||
|
|
||||||
|
if (LightBounce > 0)
|
||||||
|
{
|
||||||
|
SetupTaskProcessed("Tracing indirect", traceTasks.size());
|
||||||
|
Worker::RunJob(traceTasks.size(), [=](int id) {
|
||||||
|
LightIndirect(id);
|
||||||
|
PrintTaskProcessed();
|
||||||
|
});
|
||||||
|
printf("Texels traced: %i \n\n", tracedTexels);
|
||||||
|
}
|
||||||
|
|
||||||
|
for (auto &surf : mesh->surfaces)
|
||||||
|
{
|
||||||
|
FinishSurface(surf.get());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
BBox LightmapBuilder::GetBoundsFromSurface(const Surface *surface)
|
BBox LightmapBuilder::GetBoundsFromSurface(const Surface *surface)
|
||||||
{
|
{
|
||||||
Vec3 low(M_INFINITY, M_INFINITY, M_INFINITY);
|
Vec3 low(M_INFINITY, M_INFINITY, M_INFINITY);
|
||||||
|
@ -597,34 +641,12 @@ void LightmapBuilder::LightSurface(const int taskid)
|
||||||
{
|
{
|
||||||
const TraceTask &task = traceTasks[taskid];
|
const TraceTask &task = traceTasks[taskid];
|
||||||
TraceSurface(mesh->surfaces[task.surface].get(), task.offset);
|
TraceSurface(mesh->surfaces[task.surface].get(), task.offset);
|
||||||
|
|
||||||
std::unique_lock<std::mutex> lock(mutex);
|
|
||||||
|
|
||||||
int numtasks = traceTasks.size();
|
|
||||||
int lastproc = processed * 100 / numtasks;
|
|
||||||
processed++;
|
|
||||||
int curproc = processed * 100 / numtasks;
|
|
||||||
if (lastproc != curproc || processed == 1)
|
|
||||||
{
|
|
||||||
float remaining = (float)processed / (float)numtasks;
|
|
||||||
printf("%i%c done\r", (int)(remaining * 100.0f), '%');
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void LightmapBuilder::LightIndirect(const int taskid)
|
void LightmapBuilder::LightIndirect(const int taskid)
|
||||||
{
|
{
|
||||||
const TraceTask &task = traceTasks[taskid];
|
const TraceTask &task = traceTasks[taskid];
|
||||||
TraceIndirectLight(mesh->surfaces[task.surface].get(), task.offset);
|
TraceIndirectLight(mesh->surfaces[task.surface].get(), task.offset);
|
||||||
|
|
||||||
int numtasks = traceTasks.size();
|
|
||||||
int lastproc = processed * 100 / numtasks;
|
|
||||||
processed++;
|
|
||||||
int curproc = processed * 100 / numtasks;
|
|
||||||
if (lastproc != curproc || processed == 1)
|
|
||||||
{
|
|
||||||
float remaining = (float)processed / (float)numtasks;
|
|
||||||
printf("%i%c done\r", (int)(remaining * 100.0f), '%');
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void LightmapBuilder::CreateSurfaceLights()
|
void LightmapBuilder::CreateSurfaceLights()
|
||||||
|
@ -667,59 +689,6 @@ void LightmapBuilder::CreateSurfaceLights()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void LightmapBuilder::CreateLightmaps(FLevel &doomMap, int sampleDistance, int textureSize)
|
|
||||||
{
|
|
||||||
map = &doomMap;
|
|
||||||
samples = sampleDistance;
|
|
||||||
textureWidth = textureSize;
|
|
||||||
textureHeight = textureSize;
|
|
||||||
|
|
||||||
mesh = std::make_unique<LevelMesh>(doomMap);
|
|
||||||
|
|
||||||
CreateSurfaceLights();
|
|
||||||
CreateTraceTasks();
|
|
||||||
|
|
||||||
printf("-------------- Tracing cells ---------------\n");
|
|
||||||
|
|
||||||
SetupLightCellGrid();
|
|
||||||
|
|
||||||
processed = 0;
|
|
||||||
tracedTexels = 0;
|
|
||||||
Worker::RunJob(grid.blocks.size(), [=](int id) {
|
|
||||||
LightBlock(id);
|
|
||||||
});
|
|
||||||
|
|
||||||
printf("Cells traced: %i \n\n", tracedTexels);
|
|
||||||
|
|
||||||
printf("------------- Tracing surfaces -------------\n");
|
|
||||||
|
|
||||||
tracedTexels = 0;
|
|
||||||
processed = 0;
|
|
||||||
Worker::RunJob(traceTasks.size(), [=](int id) {
|
|
||||||
LightSurface(id);
|
|
||||||
});
|
|
||||||
|
|
||||||
printf("Texels traced: %i \n\n", tracedTexels);
|
|
||||||
|
|
||||||
if (LightBounce > 0)
|
|
||||||
{
|
|
||||||
printf("------------- Tracing indirect -------------\n");
|
|
||||||
|
|
||||||
tracedTexels = 0;
|
|
||||||
processed = 0;
|
|
||||||
Worker::RunJob(traceTasks.size(), [=](int id) {
|
|
||||||
LightIndirect(id);
|
|
||||||
});
|
|
||||||
|
|
||||||
printf("Texels traced: %i \n\n", tracedTexels);
|
|
||||||
}
|
|
||||||
|
|
||||||
for (auto &surf : mesh->surfaces)
|
|
||||||
{
|
|
||||||
FinishSurface(surf.get());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void LightmapBuilder::SetupLightCellGrid()
|
void LightmapBuilder::SetupLightCellGrid()
|
||||||
{
|
{
|
||||||
BBox worldBBox = mesh->CollisionMesh->get_bbox();
|
BBox worldBBox = mesh->CollisionMesh->get_bbox();
|
||||||
|
@ -819,18 +788,6 @@ void LightmapBuilder::LightBlock(int id)
|
||||||
block.z = 0;
|
block.z = 0;
|
||||||
block.layers = 0;
|
block.layers = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::unique_lock<std::mutex> lock(mutex);
|
|
||||||
|
|
||||||
int numblocks = grid.blocks.size();
|
|
||||||
int lastproc = processed * 100 / numblocks;
|
|
||||||
processed++;
|
|
||||||
int curproc = processed * 100 / numblocks;
|
|
||||||
if (lastproc != curproc || processed == 1)
|
|
||||||
{
|
|
||||||
float remaining = (float)processed / (float)numblocks;
|
|
||||||
printf("%i%c cells done\r", (int)(remaining * 100.0f), '%');
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void LightmapBuilder::AddLightmapLump(FWadWriter &wadFile)
|
void LightmapBuilder::AddLightmapLump(FWadWriter &wadFile)
|
||||||
|
@ -969,6 +926,28 @@ void LightmapBuilder::AddLightmapLump(FWadWriter &wadFile)
|
||||||
zout.Write(buffer.data(), lumpFile.BufferAt() - lumpFile.Buffer());
|
zout.Write(buffer.data(), lumpFile.BufferAt() - lumpFile.Buffer());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void LightmapBuilder::SetupTaskProcessed(const char *name, int total)
|
||||||
|
{
|
||||||
|
printf("-------------- %s ---------------\n", name);
|
||||||
|
|
||||||
|
processed = 0;
|
||||||
|
progresstotal = total;
|
||||||
|
}
|
||||||
|
|
||||||
|
void LightmapBuilder::PrintTaskProcessed()
|
||||||
|
{
|
||||||
|
std::unique_lock<std::mutex> lock(mutex);
|
||||||
|
|
||||||
|
int lastproc = processed * 100 / progresstotal;
|
||||||
|
processed++;
|
||||||
|
int curproc = processed * 100 / progresstotal;
|
||||||
|
if (lastproc != curproc || processed == 1)
|
||||||
|
{
|
||||||
|
float remaining = (float)processed / (float)progresstotal;
|
||||||
|
printf("%i%c done\r", (int)(remaining * 100.0f), '%');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/////////////////////////////////////////////////////////////////////////////
|
/////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
LightmapTexture::LightmapTexture(int width, int height) : textureWidth(width), textureHeight(height)
|
LightmapTexture::LightmapTexture(int width, int height) : textureWidth(width), textureHeight(height)
|
||||||
|
|
|
@ -107,6 +107,9 @@ private:
|
||||||
|
|
||||||
void CreateSurfaceLights();
|
void CreateSurfaceLights();
|
||||||
|
|
||||||
|
void SetupTaskProcessed(const char *name, int total);
|
||||||
|
void PrintTaskProcessed();
|
||||||
|
|
||||||
uint16_t *AllocTextureRoom(Surface *surface, int *x, int *y);
|
uint16_t *AllocTextureRoom(Surface *surface, int *x, int *y);
|
||||||
|
|
||||||
FLevel *map;
|
FLevel *map;
|
||||||
|
@ -118,11 +121,11 @@ private:
|
||||||
std::vector<std::unique_ptr<SurfaceLight>> surfaceLights;
|
std::vector<std::unique_ptr<SurfaceLight>> surfaceLights;
|
||||||
std::vector<std::unique_ptr<LightmapTexture>> textures;
|
std::vector<std::unique_ptr<LightmapTexture>> textures;
|
||||||
std::vector<TraceTask> traceTasks;
|
std::vector<TraceTask> traceTasks;
|
||||||
int extraSamples = 2;
|
|
||||||
int tracedTexels = 0;
|
int tracedTexels = 0;
|
||||||
|
|
||||||
LightCellGrid grid;
|
LightCellGrid grid;
|
||||||
|
|
||||||
std::mutex mutex;
|
std::mutex mutex;
|
||||||
int processed = 0;
|
int processed = 0;
|
||||||
|
int progresstotal = 0;
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in a new issue