Skip to content

Commit 2ecafa0

Browse files
committed
it works
1 parent ac2638b commit 2ecafa0

File tree

1 file changed

+6
-49
lines changed

1 file changed

+6
-49
lines changed

HelloVulkan/Source/Pipelines/PipelineShadow.cpp

Lines changed: 6 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ void PipelineShadow::CalculateCascade(
110110

111111
const glm::mat4 invCam = glm::inverse(camera->GetProjectionMatrix() * camera->GetViewMatrix());
112112
const glm::vec3 normLightPos = normalize(glm::vec3(light->position_.x, light->position_.y, light->position_.z));
113+
//const float lightDist = glm::length(glm::vec3(light->pos));
113114
const float clipNear = CameraConfig::Near;
114115
const float clipFar = CameraConfig::Far;
115116
const float clipRange = clipFar - clipNear;
@@ -167,78 +168,34 @@ void PipelineShadow::CalculateCascade(
167168
}
168169
frustumCenter /= 8.0f;
169170

170-
/*float radius = 0.0f;
171+
float radius = 0.0f;
171172
for (uint32_t i = 0; i < 8; i++)
172173
{
173174
float distance = glm::length(frustumCorners[i] - frustumCenter);
174175
radius = glm::max(radius, distance);
175176
}
176177
radius = std::ceil(radius * 16.0f) / 16.0f;
177178

178-
glm::vec3 eye = frustumCenter + normLightPos * radius;
179+
glm::vec3 eye = frustumCenter + glm::vec3(light->position_);
179180
glm::vec3 target = frustumCenter;
180181
glm::mat4 lightViewMatrix = glm::lookAt(eye, target, glm::vec3(0.0f, 1.0f, 0.0f));
181182
glm::mat4 lightOrthoMatrix = glm::ortho(
182183
-radius,
183184
radius,
184185
-radius,
185186
radius,
186-
0.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);
187+
0.f,
188+
clipFar);
228189

229190
// Store split distance and matrix in cascade
230191
ubo->lightSpaceMatrices[a] = lightOrthoMatrix * lightViewMatrix;
231192
ubo->orthoMatrices[a] = lightOrthoMatrix;
232193
ubo->viewMatrices[a] = lightViewMatrix;
233194
ubo->splitValues[a] = (clipNear + splitDist * clipRange) * -1.0f;
234-
195+
235196
lastSplitDist = cascadeSplits[a];
236197
}
237198

238-
/*glm::mat4 lightProjection = glm::perspective(glm::radians(45.f), 1.0f, ubo->shadowNearPlane, ubo->shadowFarPlane);
239-
glm::mat4 lightView = glm::lookAt(glm::vec3(light->position_), glm::vec3(0.0f), glm::vec3(0.0, 1.0, 0.0));
240-
glm::mat4 lightSpaceMatrix = lightProjection * lightView;
241-
ubo->lightSpaceMatrices[0] = lightSpaceMatrix;*/
242199
ubo->lightPosition = light->position_;
243200

244201
// Copy UBO to the buffer

0 commit comments

Comments
 (0)