@@ -167,7 +167,7 @@ void PipelineShadow::CalculateCascade(
167167 }
168168 frustumCenter /= 8 .0f ;
169169
170- float radius = 0 .0f ;
170+ /* float radius = 0.0f;
171171 for (uint32_t i = 0; i < 8; i++)
172172 {
173173 float distance = glm::length(frustumCorners[i] - frustumCenter);
@@ -184,7 +184,47 @@ void PipelineShadow::CalculateCascade(
184184 -radius,
185185 radius,
186186 0.0f,
187- radius * 2 .0f );
187+ radius * 2.0f);*/
188+
189+ glm::mat4 lightViewMatrix = glm::lookAt (frustumCenter + normLightPos, frustumCenter, glm::vec3 (0 .0f , 1 .0f , 0 .0f ));
190+
191+ float minX = std::numeric_limits<float >::max ();
192+ float maxX = std::numeric_limits<float >::lowest ();
193+ float minY = std::numeric_limits<float >::max ();
194+ float maxY = std::numeric_limits<float >::lowest ();
195+ float minZ = std::numeric_limits<float >::max ();
196+ float maxZ = std::numeric_limits<float >::lowest ();
197+ for (const auto & v : frustumCorners)
198+ {
199+ glm::vec4 trf = lightViewMatrix * glm::vec4 (v, 1 .0f );
200+ minX = std::min (minX, trf.x );
201+ maxX = std::max (maxX, trf.x );
202+ minY = std::min (minY, trf.y );
203+ maxY = std::max (maxY, trf.y );
204+ minZ = std::min (minZ, trf.z );
205+ maxZ = std::max (maxZ, trf.z );
206+ }
207+
208+ // Tune this parameter according to the scene
209+ if (minZ < 0 )
210+ {
211+ minZ *= zMult;
212+ }
213+ else
214+ {
215+ minZ /= zMult;
216+ }
217+
218+ if (maxZ < 0 )
219+ {
220+ maxZ /= zMult;
221+ }
222+ else
223+ {
224+ maxZ *= zMult;
225+ }
226+
227+ const glm::mat4 lightOrthoMatrix = glm::ortho (minX, maxX, minY, maxY, minZ, maxZ);
188228
189229 // Store split distance and matrix in cascade
190230 ubo->lightSpaceMatrices [a] = lightOrthoMatrix * lightViewMatrix;
@@ -290,7 +330,7 @@ glm::mat4 PipelineShadow::GetLightSpaceMatrix(const Camera* camera, float near,
290330 float maxZ = std::numeric_limits<float >::lowest ();
291331 for (const auto & v : corners)
292332 {
293- const auto trf = lightView * v;
333+ glm::vec4 trf = lightView * v;
294334 minX = std::min (minX, trf.x );
295335 maxX = std::max (maxX, trf.x );
296336 minY = std::min (minY, trf.y );
0 commit comments