From a211a900151bf6fec0c8cbf226d21bf1b48e1e25 Mon Sep 17 00:00:00 2001 From: Antoine Pilote Date: Sat, 19 Jun 2021 15:46:42 -0400 Subject: [PATCH] Light CSM work --- Nuake/src/Scene/Components/LightComponent.h | 41 +++++++++++++-------- 1 file changed, 25 insertions(+), 16 deletions(-) diff --git a/Nuake/src/Scene/Components/LightComponent.h b/Nuake/src/Scene/Components/LightComponent.h index fa276875..a5e25861 100644 --- a/Nuake/src/Scene/Components/LightComponent.h +++ b/Nuake/src/Scene/Components/LightComponent.h @@ -7,6 +7,7 @@ #include "BaseComponent.h" #include "../Resource/Serializable.h" +#include enum LightType { Directional, Point, Spot }; @@ -29,6 +30,9 @@ public: float LinearAttenuation = 0.0f; float QuadraticAttenuation = 0.0f; + std::vector mViewProjections; + std::vector mCascadeSplitDepth; + LightComponent(); glm::mat4 GetProjection(); @@ -49,6 +53,8 @@ public: void SetType(LightType type); std::vector mCascadeSplits; + + void CalculateViewProjection(glm::mat4& view, const glm::mat4& projection, const glm::vec3& normalizedDirection) { glm::mat4 viewProjection = projection * view; @@ -59,6 +65,9 @@ public: const float farClip = 1000.0f; const float clipRange = farClip - nearClip; + const float mCascadeNearPlaneOffset = 0.0; + const float mCascadeFarPlaneOffset = 0.0; + // Calculate the optimal cascade distances const float minZ = nearClip; const float maxZ = nearClip + clipRange; @@ -69,13 +78,13 @@ public: const float p = (i + 1) / static_cast(4); const float log = minZ * glm::pow(ratio, p); const float uniform = minZ + range * p; - //const float d = 0.91f * (log - uniform) + uniform; - //mCascadeSplits[i] = (d - nearClip) / clipRange; + const float d = 0.91f * (log - uniform) + uniform; + mCascadeSplits[i] = (d - nearClip) / clipRange; } - //mCascadeSplits[0] = 0.2f; - //mCascadeSplits[1] = 0.45f; - //mCascadeSplits[2] = 1.0f; + mCascadeSplits[0] = 0.2f; + mCascadeSplits[1] = 0.45f; + mCascadeSplits[2] = 1.0f; float lastSplitDist = 0.0f; // Calculate Orthographic Projection matrix for each cascade @@ -130,22 +139,22 @@ public: // Calculate the view and projection matrix glm::vec3 lightDir = -normalizedDirection; glm::mat4 lightViewMatrix = glm::lookAt(frustumCenter - lightDir * -minExtents.z, frustumCenter, glm::vec3(0.0f, 0.0f, 1.0f)); - //glm::mat4 lightProjectionMatrix = glm::ortho(minExtents.x, maxExtents.x, minExtents.y, maxExtents.y, 0.0f + mCascadeNearPlaneOffset, maxExtents.z - minExtents.z + mCascadeFarPlaneOffset); + glm::mat4 lightProjectionMatrix = glm::ortho(minExtents.x, maxExtents.x, minExtents.y, maxExtents.y, 0.0f + mCascadeNearPlaneOffset, maxExtents.z - minExtents.z + mCascadeFarPlaneOffset); // Offset to texel space to avoid shimmering ->(https://stackoverflow.com/questions/33499053/cascaded-shadow-map-shimmering) - //glm::mat4 shadowMatrix = lightProjectionMatrix * lightViewMatrix; + glm::mat4 shadowMatrix = lightProjectionMatrix * lightViewMatrix; const float ShadowMapResolution = 4096; - //glm::vec4 shadowOrigin = (shadowMatrix * glm::vec4(0.0f, 0.0f, 0.0f, 1.0f)) * ShadowMapResolution / 2.0f; - //glm::vec4 roundedOrigin = glm::round(shadowOrigin); - //glm::vec4 roundOffset = roundedOrigin - shadowOrigin; - //roundOffset = roundOffset * 2.0f / ShadowMapResolution; - //roundOffset.z = 0.0f; - //roundOffset.w = 0.0f; - //lightProjectionMatrix[3] += roundOffset; + glm::vec4 shadowOrigin = (shadowMatrix * glm::vec4(0.0f, 0.0f, 0.0f, 1.0f)) * ShadowMapResolution / 2.0f; + glm::vec4 roundedOrigin = glm::round(shadowOrigin); + glm::vec4 roundOffset = roundedOrigin - shadowOrigin; + roundOffset = roundOffset * 2.0f / ShadowMapResolution; + roundOffset.z = 0.0f; + roundOffset.w = 0.0f; + lightProjectionMatrix[3] += roundOffset; // Store SplitDistance and ViewProjection-Matrix - //mCascadeSplitDepth[cascade] = (nearClip + splitDist * clipRange) * 1.0f; - //mViewProjections[cascade] = lightProjectionMatrix * lightViewMatrix; + mCascadeSplitDepth[cascade] = (nearClip + splitDist * clipRange) * 1.0f; + mViewProjections[cascade] = lightProjectionMatrix * lightViewMatrix; lastSplitDist = mCascadeSplits[cascade]; // -----------------------Debug only-----------------------