mirror of
https://github.com/ZDoom/ZDRay.git
synced 2025-02-18 01:11:25 +00:00
Add automatically generating a grid of light probes
This commit is contained in:
parent
2f7b97854a
commit
4f3005de9e
5 changed files with 93 additions and 31 deletions
|
@ -234,7 +234,7 @@ void FBlockmapBuilder::BuildBlockmap ()
|
||||||
{
|
{
|
||||||
if (bx > bx2)
|
if (bx > bx2)
|
||||||
{
|
{
|
||||||
swap (block, endblock);
|
std::swap (block, endblock);
|
||||||
}
|
}
|
||||||
do
|
do
|
||||||
{
|
{
|
||||||
|
@ -246,7 +246,7 @@ void FBlockmapBuilder::BuildBlockmap ()
|
||||||
{
|
{
|
||||||
if (by > by2)
|
if (by > by2)
|
||||||
{
|
{
|
||||||
swap (block, endblock);
|
std::swap (block, endblock);
|
||||||
}
|
}
|
||||||
do
|
do
|
||||||
{
|
{
|
||||||
|
|
|
@ -179,17 +179,3 @@ T clamp (const T in, const T min, const T max)
|
||||||
{
|
{
|
||||||
return in <= min ? min : in >= max ? max : in;
|
return in <= min ? min : in >= max ? max : in;
|
||||||
}
|
}
|
||||||
|
|
||||||
//==========================================================================
|
|
||||||
//
|
|
||||||
// swap
|
|
||||||
//
|
|
||||||
// Swaps the values of a and b.
|
|
||||||
//==========================================================================
|
|
||||||
|
|
||||||
template<class T>
|
|
||||||
inline
|
|
||||||
void swap (T &a, T &b)
|
|
||||||
{
|
|
||||||
T temp = a; a = b; b = temp;
|
|
||||||
}
|
|
||||||
|
|
|
@ -66,15 +66,14 @@ void LightmapBuilder::CreateLightmaps(FLevel &doomMap, int sampleDistance, int t
|
||||||
|
|
||||||
CreateSurfaceLights();
|
CreateSurfaceLights();
|
||||||
CreateTraceTasks();
|
CreateTraceTasks();
|
||||||
|
CreateLightProbes();
|
||||||
lightProbes.resize(map->ThingLightProbes.Size(), Vec3(0.0f, 0.0f, 0.0f));
|
|
||||||
|
|
||||||
SetupTaskProcessed("Tracing light probes", lightProbes.size());
|
SetupTaskProcessed("Tracing light probes", lightProbes.size());
|
||||||
Worker::RunJob(lightProbes.size(), [=](int id) {
|
Worker::RunJob(lightProbes.size(), [=](int id) {
|
||||||
LightProbe(id);
|
LightProbe(id);
|
||||||
PrintTaskProcessed();
|
PrintTaskProcessed();
|
||||||
});
|
});
|
||||||
printf("Probes traced: %i \n\n", tracedTexels);
|
printf("Probes traced: %i \n\n", (int)lightProbes.size());
|
||||||
|
|
||||||
SetupTaskProcessed("Tracing surfaces", traceTasks.size());
|
SetupTaskProcessed("Tracing surfaces", traceTasks.size());
|
||||||
Worker::RunJob(traceTasks.size(), [=](int id) {
|
Worker::RunJob(traceTasks.size(), [=](int id) {
|
||||||
|
@ -638,6 +637,69 @@ void LightmapBuilder::CreateTraceTasks()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void LightmapBuilder::CreateLightProbes()
|
||||||
|
{
|
||||||
|
float minX = std::floor(map->MinX / 65536.0f);
|
||||||
|
float minY = std::floor(map->MinY / 65536.0f);
|
||||||
|
float maxX = std::floor(map->MaxX / 65536.0f) + 1.0f;
|
||||||
|
float maxY = std::floor(map->MaxY / 65536.0f) + 1.0f;
|
||||||
|
|
||||||
|
float gridSize = 32.0f;
|
||||||
|
float halfGridSize = gridSize * 0.5f;
|
||||||
|
|
||||||
|
std::vector<std::vector<LightProbeSample>> probes; // order probes by subsector
|
||||||
|
probes.resize(map->NumGLSubsectors);
|
||||||
|
size_t totalProbes = 0;
|
||||||
|
|
||||||
|
for (float y = minY; y < maxY; y += gridSize)
|
||||||
|
{
|
||||||
|
for (float x = minX; x < maxX; x += gridSize)
|
||||||
|
{
|
||||||
|
MapSubsectorEx* ssec = map->PointInSubSector((int)x, (int)y);
|
||||||
|
IntSector* sec = ssec ? map->GetSectorFromSubSector(ssec) : nullptr;
|
||||||
|
if (sec)
|
||||||
|
{
|
||||||
|
float z0 = sec->floorplane.zAt(x, y);
|
||||||
|
float z1 = sec->ceilingplane.zAt(x, y);
|
||||||
|
float startZ = (z1 - z0 < halfGridSize) ? (z0 + z1) * 0.5f : z0 + halfGridSize;
|
||||||
|
for (float z = startZ; z < z1; z += gridSize)
|
||||||
|
{
|
||||||
|
LightProbeSample probe;
|
||||||
|
probe.Position.x = x;
|
||||||
|
probe.Position.y = y;
|
||||||
|
probe.Position.z = z;
|
||||||
|
|
||||||
|
size_t index = (ptrdiff_t)(ssec - map->GLSubsectors);
|
||||||
|
probes[index].push_back(probe);
|
||||||
|
totalProbes++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for (unsigned int i = 0; i < map->ThingLightProbes.Size(); i++)
|
||||||
|
{
|
||||||
|
Vec3 pos = map->GetLightProbePosition(i);
|
||||||
|
MapSubsectorEx* ssec = map->PointInSubSector((int)pos.x, (int)pos.y);
|
||||||
|
if (ssec)
|
||||||
|
{
|
||||||
|
LightProbeSample probe;
|
||||||
|
probe.Position = pos;
|
||||||
|
|
||||||
|
size_t index = (ptrdiff_t)(ssec - map->GLSubsectors);
|
||||||
|
probes[index].push_back(probe);
|
||||||
|
totalProbes++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
lightProbes.reserve(totalProbes);
|
||||||
|
for (const std::vector<LightProbeSample>& ssprobes : probes)
|
||||||
|
{
|
||||||
|
lightProbes.insert(lightProbes.end(), ssprobes.begin(), ssprobes.cend());
|
||||||
|
lightProbeSubsectorCounts.push_back((int)ssprobes.size());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void LightmapBuilder::LightSurface(const int taskid)
|
void LightmapBuilder::LightSurface(const int taskid)
|
||||||
{
|
{
|
||||||
const TraceTask &task = traceTasks[taskid];
|
const TraceTask &task = traceTasks[taskid];
|
||||||
|
@ -692,7 +754,7 @@ void LightmapBuilder::CreateSurfaceLights()
|
||||||
|
|
||||||
void LightmapBuilder::LightProbe(int id)
|
void LightmapBuilder::LightProbe(int id)
|
||||||
{
|
{
|
||||||
lightProbes[id] = LightTexelSample(map->GetLightProbePosition(id), nullptr);
|
lightProbes[id].Color = LightTexelSample(lightProbes[id].Position, nullptr);
|
||||||
}
|
}
|
||||||
|
|
||||||
void LightmapBuilder::AddLightmapLump(FWadWriter &wadFile)
|
void LightmapBuilder::AddLightmapLump(FWadWriter &wadFile)
|
||||||
|
@ -712,7 +774,7 @@ void LightmapBuilder::AddLightmapLump(FWadWriter &wadFile)
|
||||||
}
|
}
|
||||||
|
|
||||||
int version = 0;
|
int version = 0;
|
||||||
int headerSize = 4 * sizeof(uint32_t) + 2 * sizeof(uint16_t);
|
int headerSize = 5 * sizeof(uint32_t) + 2 * sizeof(uint16_t);
|
||||||
int surfacesSize = surfaces.size() * 5 * sizeof(uint32_t);
|
int surfacesSize = surfaces.size() * 5 * sizeof(uint32_t);
|
||||||
int texCoordsSize = numTexCoords * 2 * sizeof(float);
|
int texCoordsSize = numTexCoords * 2 * sizeof(float);
|
||||||
int texDataSize = textures.size() * textureWidth * textureHeight * 3 * 2;
|
int texDataSize = textures.size() * textureWidth * textureHeight * 3 * 2;
|
||||||
|
@ -731,17 +793,22 @@ void LightmapBuilder::AddLightmapLump(FWadWriter &wadFile)
|
||||||
lumpFile.Write32(numSurfaces);
|
lumpFile.Write32(numSurfaces);
|
||||||
lumpFile.Write32(numTexCoords);
|
lumpFile.Write32(numTexCoords);
|
||||||
lumpFile.Write32(lightProbes.size());
|
lumpFile.Write32(lightProbes.size());
|
||||||
|
lumpFile.Write32(lightProbeSubsectorCounts.size());
|
||||||
|
|
||||||
// Write light probes
|
// Write light probes
|
||||||
for (size_t i = 0; i < lightProbes.size(); i++)
|
for (const LightProbeSample& probe : lightProbes)
|
||||||
{
|
{
|
||||||
Vec3 pos = map->GetLightProbePosition(i);
|
lumpFile.WriteFloat(probe.Position.x);
|
||||||
lumpFile.WriteFloat(pos.x);
|
lumpFile.WriteFloat(probe.Position.y);
|
||||||
lumpFile.WriteFloat(pos.y);
|
lumpFile.WriteFloat(probe.Position.z);
|
||||||
lumpFile.WriteFloat(pos.z);
|
lumpFile.WriteFloat(probe.Color.x);
|
||||||
lumpFile.WriteFloat(lightProbes[i].x);
|
lumpFile.WriteFloat(probe.Color.y);
|
||||||
lumpFile.WriteFloat(lightProbes[i].y);
|
lumpFile.WriteFloat(probe.Color.z);
|
||||||
lumpFile.WriteFloat(lightProbes[i].z);
|
}
|
||||||
|
|
||||||
|
for (int count : lightProbeSubsectorCounts)
|
||||||
|
{
|
||||||
|
lumpFile.Write32(count);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Write surfaces
|
// Write surfaces
|
||||||
|
|
|
@ -67,6 +67,13 @@ public:
|
||||||
static const int tasksize = 64;
|
static const int tasksize = 64;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
class LightProbeSample
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
Vec3 Position = Vec3(0.0f, 0.0f, 0.0f);
|
||||||
|
Vec3 Color = Vec3(0.0f, 0.0f, 0.0f);
|
||||||
|
};
|
||||||
|
|
||||||
class LightmapBuilder
|
class LightmapBuilder
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
@ -92,6 +99,7 @@ private:
|
||||||
void LightIndirect(const int taskid);
|
void LightIndirect(const int taskid);
|
||||||
|
|
||||||
void CreateSurfaceLights();
|
void CreateSurfaceLights();
|
||||||
|
void CreateLightProbes();
|
||||||
|
|
||||||
void SetupTaskProcessed(const char *name, int total);
|
void SetupTaskProcessed(const char *name, int total);
|
||||||
void PrintTaskProcessed();
|
void PrintTaskProcessed();
|
||||||
|
@ -107,7 +115,8 @@ 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;
|
||||||
std::vector<Vec3> lightProbes;
|
std::vector<LightProbeSample> lightProbes;
|
||||||
|
std::vector<int> lightProbeSubsectorCounts;
|
||||||
int tracedTexels = 0;
|
int tracedTexels = 0;
|
||||||
|
|
||||||
std::mutex mutex;
|
std::mutex mutex;
|
||||||
|
|
|
@ -366,7 +366,7 @@ bool FNodeBuilder::CheckSubsectorOverlappingSegs (uint32_t set, node_t &node, ui
|
||||||
{
|
{
|
||||||
if (Segs[seg2].linedef == -1)
|
if (Segs[seg2].linedef == -1)
|
||||||
{ // Do not put minisegs into a new subsector.
|
{ // Do not put minisegs into a new subsector.
|
||||||
swap (seg1, seg2);
|
std::swap (seg1, seg2);
|
||||||
}
|
}
|
||||||
D(Printf("Need to synthesize a splitter for set %d on seg %d (ov)\n", set, seg2));
|
D(Printf("Need to synthesize a splitter for set %d on seg %d (ov)\n", set, seg2));
|
||||||
splitseg = DWORD_MAX;
|
splitseg = DWORD_MAX;
|
||||||
|
|
Loading…
Reference in a new issue