From 6acea159f7c4dbe2bc9515f9b8634631beaf9253 Mon Sep 17 00:00:00 2001 From: Magnus Norddahl Date: Mon, 29 Oct 2018 18:32:50 +0100 Subject: [PATCH] - change the light math and some defaults --- src/level/level.cpp | 2 +- src/lightmap/kexlib/math/bounds.cpp | 8 ++ src/lightmap/kexlib/math/mathlib.h | 46 ++++++++-- src/lightmap/kexlib/math/vector.cpp | 133 ++++++++++++++++++++++++++-- src/lightmap/lightmap.cpp | 22 ++--- src/lightmap/lightsurface.cpp | 17 ++-- src/lightmap/mapdata.cpp | 4 +- 7 files changed, 190 insertions(+), 42 deletions(-) diff --git a/src/level/level.cpp b/src/level/level.cpp index 3c810dc..af1df8f 100644 --- a/src/level/level.cpp +++ b/src/level/level.cpp @@ -597,7 +597,7 @@ void FProcessor::BuildNodes() void FProcessor::BuildLightmaps(const char *configFile) { LMBuilder.ambience = 0.0f; - LMBuilder.samples = 64; + LMBuilder.samples = 8; LMBuilder.textureWidth = LIGHTMAP_MAX_SIZE; LMBuilder.textureHeight = LIGHTMAP_MAX_SIZE; diff --git a/src/lightmap/kexlib/math/bounds.cpp b/src/lightmap/kexlib/math/bounds.cpp index 4eb18e7..fd7f6c2 100644 --- a/src/lightmap/kexlib/math/bounds.cpp +++ b/src/lightmap/kexlib/math/bounds.cpp @@ -98,6 +98,14 @@ kexVec3 kexBBox::Center() const (max.z + min.z) * 0.5f); } +kexVec3 kexBBox::Extents() const +{ + return kexVec3( + (max.x - min.x) * 0.5f, + (max.y - min.y) * 0.5f, + (max.z - min.z) * 0.5f); +} + // // kexBBox::Radius // diff --git a/src/lightmap/kexlib/math/mathlib.h b/src/lightmap/kexlib/math/mathlib.h index dae41bd..3847fde 100644 --- a/src/lightmap/kexlib/math/mathlib.h +++ b/src/lightmap/kexlib/math/mathlib.h @@ -215,12 +215,15 @@ public: float Dot(const kexVec3 &vec) const; static float Dot(const kexVec3 &vec1, const kexVec3 &vec2); kexVec3 Cross(const kexVec3 &vec) const; - kexVec3 &Cross(const kexVec3 &vec1, const kexVec3 &vec2); + static kexVec3 Cross(const kexVec3 &vec1, const kexVec3 &vec2); float UnitSq() const; float Unit() const; - float DistanceSq(const kexVec3 &vec) const; + float LengthSq() const { return UnitSq(); } + float Length() const { return Unit(); } + float DistanceSq(const kexVec3 &vec) const; float Distance(const kexVec3 &vec) const; kexVec3 &Normalize(); + static kexVec3 Normalize(kexVec3 a); kexAngle PointAt(kexVec3 &location) const; kexVec3 Lerp(const kexVec3 &next, float movement) const; kexVec3 &Lerp(const kexVec3 &start, const kexVec3 &next, float movement); @@ -238,8 +241,10 @@ public: kexVec3 operator+(const kexVec3 &vec); kexVec3 operator+(const kexVec3 &vec) const; kexVec3 operator+(kexVec3 &vec); - kexVec3 operator-() const; - kexVec3 operator-(const kexVec3 &vec) const; + kexVec3 operator+(const float val) const; + kexVec3 operator-() const; + kexVec3 operator-(const float val) const; + kexVec3 operator-(const kexVec3 &vec) const; kexVec3 operator*(const kexVec3 &vec); kexVec3 operator*(const float val); kexVec3 operator*(const float val) const; @@ -251,8 +256,10 @@ public: kexVec3 &operator=(const kexVec3 &vec); kexVec3 &operator=(const float *vecs); kexVec3 &operator+=(const kexVec3 &vec); - kexVec3 &operator-=(const kexVec3 &vec); - kexVec3 &operator*=(const kexVec3 &vec); + kexVec3 &operator+=(const float val); + kexVec3 &operator-=(const kexVec3 &vec); + kexVec3 &operator-=(const float val); + kexVec3 &operator*=(const kexVec3 &vec); kexVec3 &operator*=(const float val); kexVec3 &operator/=(const kexVec3 &vec); kexVec3 &operator/=(const float val); @@ -277,16 +284,35 @@ class kexVec4 public: kexVec4(); explicit kexVec4(const float x, const float y, const float z, const float w); + explicit kexVec4(const kexVec3 &v, const float w); void Set(const float x, const float y, const float z, const float w); void Clear(); float *ToFloatPtr(); + static float Dot(const kexVec4 &a, const kexVec4 &b) { return a.x * b.x + a.y * b.y + a.z * b.z + a.w * b.w; } + const kexVec3 &ToVec3() const; kexVec3 &ToVec3(); kexVec4 operator|(const kexMatrix &mtx); kexVec4 &operator|=(const kexMatrix &mtx); - float operator[](int index) const; + kexVec4 operator+(const kexVec4 &vec) const; + kexVec4 operator+(const float val) const; + kexVec4 operator-(const kexVec4 &vec) const; + kexVec4 operator-(const float val) const; + kexVec4 operator*(const kexVec4 &vec) const; + kexVec4 operator*(const float val) const; + kexVec4 operator/(const kexVec4 &vec) const; + kexVec4 operator/(const float val) const; + kexVec4 &operator+=(const kexVec4 &vec); + kexVec4 &operator+=(const float val); + kexVec4 &operator-=(const kexVec4 &vec); + kexVec4 &operator-=(const float val); + kexVec4 &operator*=(const kexVec4 &vec); + kexVec4 &operator*=(const float val); + kexVec4 &operator/=(const kexVec4 &vec); + kexVec4 &operator/=(const float val); + float operator[](int index) const; float &operator[](int index); float x; @@ -332,6 +358,9 @@ public: kexMatrix &operator=(const float *m); kexMatrix operator|(const kexMatrix &matrix); + float operator[](size_t i) const { return vectors[i >> 2][i & 3]; } + float &operator[](size_t i) { return vectors[i >> 2][i & 3]; } + kexVec4 vectors[4]; }; @@ -436,7 +465,8 @@ public: void Clear(); kexVec3 Center() const; - float Radius() const; + kexVec3 Extents() const; + float Radius() const; void AddPoint(const kexVec3 &vec); bool PointInside(const kexVec3 &vec) const; bool IntersectingBox(const kexBBox &box) const; diff --git a/src/lightmap/kexlib/math/vector.cpp b/src/lightmap/kexlib/math/vector.cpp index 1f38be4..b18a476 100644 --- a/src/lightmap/kexlib/math/vector.cpp +++ b/src/lightmap/kexlib/math/vector.cpp @@ -631,13 +631,9 @@ kexVec3 kexVec3::Cross(const kexVec3 &vec) const // kexVec3::Cross // -kexVec3 &kexVec3::Cross(const kexVec3 &vec1, const kexVec3 &vec2) +kexVec3 kexVec3::Cross(const kexVec3 &vec1, const kexVec3 &vec2) { - x = vec2.z * vec1.y - vec1.z * vec2.y; - y = vec2.x * vec1.z - vec1.x * vec2.z; - z = vec1.x * vec2.y - vec2.x * vec1.y; - - return *this; + return vec1.Cross(vec2); } // @@ -690,6 +686,12 @@ kexVec3 &kexVec3::Normalize() return *this; } +kexVec3 kexVec3::Normalize(kexVec3 a) +{ + a.Normalize(); + return a; +} + // // kexVec3::PointAt // @@ -860,6 +862,11 @@ kexVec3 kexVec3::operator+(const kexVec3 &vec) const return kexVec3(x + vec.x, y + vec.y, z + vec.z); } +kexVec3 kexVec3::operator+(const float val) const +{ + return kexVec3(x + val, y + val, z + val); +} + // // kexVec3::operator+ // @@ -881,6 +888,14 @@ kexVec3 &kexVec3::operator+=(const kexVec3 &vec) return *this; } +kexVec3 &kexVec3::operator+=(const float val) +{ + x += val; + y += val; + z += val; + return *this; +} + // // kexVec3::operator- // @@ -890,6 +905,11 @@ kexVec3 kexVec3::operator-(const kexVec3 &vec) const return kexVec3(x - vec.x, y - vec.y, z - vec.z); } +kexVec3 kexVec3::operator-(const float val) const +{ + return kexVec3(x - val, y - val, z - val); +} + // // kexVec3::operator- // @@ -911,6 +931,14 @@ kexVec3 &kexVec3::operator-=(const kexVec3 &vec) return *this; } +kexVec3 &kexVec3::operator-=(const float val) +{ + x -= val; + y -= val; + z -= val; + return *this; +} + // // kexVec3::operator* // @@ -1168,6 +1196,11 @@ kexVec4::kexVec4(const float x, const float y, const float z, const float w) Set(x, y, z, w); } +kexVec4::kexVec4(const kexVec3 &v, const float w) +{ + Set(v.x, v.y, v.z, w); +} + // // kexVec4::Set // @@ -1216,6 +1249,94 @@ float *kexVec4::ToFloatPtr() return reinterpret_cast(&x); } +kexVec4 kexVec4::operator+(const kexVec4 &vec) const +{ + return kexVec4(x + vec.x, y + vec.y, z + vec.z, w + vec.w); +} + +kexVec4 kexVec4::operator+(const float val) const +{ + return kexVec4(x + val, y + val, z + val, w + val); +} + +kexVec4 kexVec4::operator-(const kexVec4 &vec) const +{ + return kexVec4(x - vec.x, y - vec.y, z - vec.z, w - vec.w); +} + +kexVec4 kexVec4::operator-(const float val) const +{ + return kexVec4(x - val, y - val, z - val, w - val); +} + +kexVec4 kexVec4::operator*(const kexVec4 &vec) const +{ + return kexVec4(x * vec.x, y * vec.y, z * vec.z, w * vec.w); +} + +kexVec4 kexVec4::operator*(const float val) const +{ + return kexVec4(x * val, y * val, z * val, w * val); +} + +kexVec4 kexVec4::operator/(const kexVec4 &vec) const +{ + return kexVec4(x / vec.x, y / vec.y, z / vec.z, w / vec.w); +} + +kexVec4 kexVec4::operator/(const float val) const +{ + return kexVec4(x / val, y / val, z / val, w / val); +} + +kexVec4 &kexVec4::operator+=(const kexVec4 &vec) +{ + x += vec.x; y += vec.y; z += vec.z; w += vec.w; + return *this; +} + +kexVec4 &kexVec4::operator+=(const float val) +{ + x += val; y += val; z += val; w += val; + return *this; +} + +kexVec4 &kexVec4::operator-=(const kexVec4 &vec) +{ + x -= vec.x; y -= vec.y; z -= vec.z; w -= vec.w; + return *this; +} + +kexVec4 &kexVec4::operator-=(const float val) +{ + x -= val; y -= val; z -= val; w -= val; + return *this; +} + +kexVec4 &kexVec4::operator*=(const kexVec4 &vec) +{ + x *= vec.x; y *= vec.y; z *= vec.z; w *= vec.w; + return *this; +} + +kexVec4 &kexVec4::operator*=(const float val) +{ + x *= val; y *= val; z *= val; w *= val; + return *this; +} + +kexVec4 &kexVec4::operator/=(const kexVec4 &vec) +{ + x /= vec.x; y /= vec.y; z /= vec.z; w /= vec.w; + return *this; +} + +kexVec4 &kexVec4::operator/=(const float val) +{ + x /= val; y /= val; z /= val; w /= val; + return *this; +} + // // kexVec4::operator| // diff --git a/src/lightmap/lightmap.cpp b/src/lightmap/lightmap.cpp index 2411875..2c0b670 100644 --- a/src/lightmap/lightmap.cpp +++ b/src/lightmap/lightmap.cpp @@ -328,8 +328,7 @@ kexVec3 kexLightmapBuilder::LightTexelSample(kexTrace &trace, const kexVec3 &ori } // accumulate results - color = color.Lerp(tl->rgb, colorAdd); - kexMath::Clamp(color, 0, 1); + color += tl->rgb * colorAdd; tracedTexels++; } @@ -342,8 +341,7 @@ kexVec3 kexLightmapBuilder::LightTexelSample(kexTrace &trace, const kexVec3 &ori dist = (dist * 4); kexMath::Clamp(dist, 0, 1); - color = color.Lerp(map->GetSunColor(), dist); - kexMath::Clamp(color, 0, 1); + color += map->GetSunColor() * dist; tracedTexels++; } @@ -362,11 +360,7 @@ kexVec3 kexLightmapBuilder::LightTexelSample(kexTrace &trace, const kexVec3 &ori if(surfaceLight->TraceSurface(map, trace, surface, origin, &dist)) { - dist = (dist * surfaceLight->Intensity()); - kexMath::Clamp(dist, 0, 1); - - color = color.Lerp(surfaceLight->GetRGB(), kexMath::Pow(dist, surfaceLight->FallOff())); - kexMath::Clamp(color, 0, 1); + color += surfaceLight->GetRGB() * kexMath::Pow(dist * surfaceLight->Intensity(), surfaceLight->FallOff()); tracedTexels++; } @@ -503,7 +497,7 @@ void kexLightmapBuilder::TraceSurface(surface_t *surface) bool bShouldLookupTexture = false; trace.Init(*map); - memset(colorSamples, 0, sizeof(colorSamples)); + //memset(colorSamples, 0, sizeof(colorSamples)); sampleWidth = surface->lightmapDims[0]; sampleHeight = surface->lightmapDims[1]; @@ -535,16 +529,18 @@ void kexLightmapBuilder::TraceSurface(surface_t *surface) #endif // accumulate color samples - colorSamples[i][j] += LightTexelSample(trace, pos, surface); + kexVec3 c = LightTexelSample(trace, pos, surface); // if nothing at all was traced and color is completely black // then this surface will not go through the extra rendering // step in rendering the lightmap - if(colorSamples[i][j].UnitSq() != 0) + if (c.x > 0.0f || c.y > 0.0f || c.z > 0.0f) { bShouldLookupTexture = true; } - } + + colorSamples[i][j] = c; + } } #ifdef EXPORT_TEXELS_OBJ diff --git a/src/lightmap/lightsurface.cpp b/src/lightmap/lightsurface.cpp index 1f8da9f..3e43af3 100644 --- a/src/lightmap/lightsurface.cpp +++ b/src/lightmap/lightsurface.cpp @@ -288,8 +288,7 @@ void kexLightSurface::Subdivide(const float divide) // kexLightSurface::TraceSurface // -bool kexLightSurface::TraceSurface(FLevel *doomMap, kexTrace &trace, const surface_t *surf, - const kexVec3 &origin, float *dist) +bool kexLightSurface::TraceSurface(FLevel *doomMap, kexTrace &trace, const surface_t *surf, const kexVec3 &origin, float *dist) { kexVec3 normal; kexVec3 lnormal; @@ -415,17 +414,10 @@ bool kexLightSurface::TraceSurface(FLevel *doomMap, kexTrace &trace, const surfa float d = origin.Distance(center); - if(d <= 0) - { - // this origin point must be right on the light surface so just mark it as - // full bright if that's the case - curDist = 1; - } - else - { - curDist = distance / d; - } + curDist = 1.0f - d / distance; + if (curDist < 0.0f) curDist = 0.0f; + /* if(curDist >= 1) { curDist = 1; @@ -453,6 +445,7 @@ bool kexLightSurface::TraceSurface(FLevel *doomMap, kexTrace &trace, const surfa curDist *= angle; } } + */ if(curDist > *dist) { diff --git a/src/lightmap/mapdata.cpp b/src/lightmap/mapdata.cpp index 377f62b..f6af4a8 100644 --- a/src/lightmap/mapdata.cpp +++ b/src/lightmap/mapdata.cpp @@ -567,8 +567,8 @@ void FLevel::ParseConfigFile(const char *file) surfaceLight.outerCone = 1.0f; surfaceLight.innerCone = 0; surfaceLight.falloff = 1.0f; - surfaceLight.intensity = 10.0f; - surfaceLight.distance = 150.0f; + surfaceLight.intensity = 1.0f; + surfaceLight.distance = 1000.0f; surfaceLight.bIgnoreCeiling = false; surfaceLight.bIgnoreFloor = false; surfaceLight.bNoCenterPoint = false;