Skip to content

Commit ac2638b

Browse files
committed
wip
1 parent e32622d commit ac2638b

File tree

3 files changed

+46
-6
lines changed

3 files changed

+46
-6
lines changed

HelloVulkan/Source/Apps/AppPBRShadow.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ void AppPBRShadow::UpdateUBOs()
161161
shadowUBO_.lightSpaceMatrices[0] = lightSpaceMatrix;
162162
shadowUBO_.lightPosition = light.position_;
163163
*/
164-
shadowPtr_->CalculateCascade2(vulkanContext_, camera_.get(), & (resLight_->lights_[0]), &shadowUBO_);
164+
shadowPtr_->CalculateCascade(vulkanContext_, camera_.get(), & (resLight_->lights_[0]), &shadowUBO_);
165165
pbrPtr_->SetShadowMapConfigUBO(vulkanContext_, shadowUBO_);
166166
}
167167

HelloVulkan/Source/Pipelines/PipelineShadow.cpp

Lines changed: 43 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -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);

Shaders/ShadowMapping/Scene.frag

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ void main()
144144

145145
fragColor = vec4(color, 1.0);
146146

147-
switch (cascadeIndex)
147+
/*switch (cascadeIndex)
148148
{
149149
case 0:
150150
fragColor.rgb *= vec3(1.0f, 0.25f, 0.25f);
@@ -158,5 +158,5 @@ void main()
158158
case 3:
159159
fragColor.rgb *= vec3(1.0f, 0.25f, 0.25f);
160160
break;
161-
}
161+
}*/
162162
}

0 commit comments

Comments
 (0)