@@ -203,142 +203,6 @@ void PipelineShadow::CalculateCascade(
203203 shadowMapUBOBuffers_[frameIndex].UploadBufferData (ctx, ubo, sizeof (ShadowMapUBO));
204204}
205205
206- void PipelineShadow::CalculateCascade2 (VulkanContext& ctx,
207- const Camera* camera,
208- const LightData* light,
209- ShadowMapUBO* ubo)
210- {
211- float far = CameraConfig::Far;
212- const glm::vec3 normLightPos = normalize (glm::vec3 (light->position_ .x , light->position_ .y , light->position_ .z ));
213- std::vector<float > shadowCascadeLevels{ far / 50 .0f , far / 25 .0f , far / 10 .0f , far / 2 .0f };
214-
215- for (uint32_t i = 0 ; i < ShadowConfig::CascadeCount; i++)
216- {
217- glm::mat4 lightspaceMatrix (1 .0f );
218- if (i == 0 )
219- {
220- lightspaceMatrix = GetLightSpaceMatrix (camera, CameraConfig::Near, shadowCascadeLevels[i], normLightPos);
221- }
222- else if (i < shadowCascadeLevels.size ())
223- {
224- lightspaceMatrix = GetLightSpaceMatrix (camera, shadowCascadeLevels[i - 1 ], shadowCascadeLevels[i], normLightPos);
225- }
226- else
227- {
228- lightspaceMatrix = GetLightSpaceMatrix (camera, shadowCascadeLevels[i - 1 ], CameraConfig::Far, normLightPos);
229- }
230-
231- // Store split distance and matrix in cascade
232- ubo->lightSpaceMatrices [i] = lightspaceMatrix;
233- ubo->orthoMatrices [i] = glm::mat4 (1 .0f );
234- ubo->viewMatrices [i] = glm::mat4 (1 .0f );
235- ubo->splitValues [i] = -1 .0f * shadowCascadeLevels[i];
236- }
237-
238- ubo->lightPosition = light->position_ ;
239-
240- // Copy UBO to the buffer
241- const uint32_t frameIndex = ctx.GetFrameIndex ();
242- shadowMapUBOBuffers_[frameIndex].UploadBufferData (ctx, ubo, sizeof (ShadowMapUBO));
243- }
244-
245- std::vector<glm::vec4> PipelineShadow::GetFrustumCornersWorldSpace (glm::mat4& proj, glm::mat4& view)
246- {
247- const auto inv = glm::inverse (proj * view);
248-
249- std::vector<glm::vec4> frustumCorners;
250- for (unsigned int x = 0 ; x < 2 ; ++x)
251- {
252- for (unsigned int y = 0 ; y < 2 ; ++y)
253- {
254- for (unsigned int z = 0 ; z < 2 ; ++z)
255- {
256- const glm::vec4 pt = inv * glm::vec4 (2 .0f * x - 1 .0f , 2 .0f * y - 1 .0f , 2 .0f * z - 1 .0f , 1 .0f );
257- frustumCorners.push_back (pt / pt.w );
258- }
259- }
260- }
261-
262- return frustumCorners;
263- }
264-
265- glm::mat4 PipelineShadow::GetLightSpaceMatrix (const Camera* camera, float near, float far, glm::vec3 normLightPos)
266- {
267- glm::mat4 proj = glm::perspective (glm::radians (camera->zoom_ ), 1 .0f , near, far);
268- proj[1 ][1 ] *= -1 ;
269- glm::mat4 view = camera->GetViewMatrix ();
270-
271- const auto corners = GetFrustumCornersWorldSpace (proj, view);
272-
273- glm::vec3 center = glm::vec3 (0 , 0 , 0 );
274- for (const auto & v : corners)
275- {
276- center += glm::vec3 (v);
277- }
278- center /= static_cast <float >(corners.size ());
279-
280- const auto lightView = glm::lookAt (center + normLightPos, center, glm::vec3 (0 .0f , 1 .0f , 0 .0f ));
281-
282- float minX = std::numeric_limits<float >::max ();
283- float maxX = std::numeric_limits<float >::lowest ();
284- float minY = std::numeric_limits<float >::max ();
285- float maxY = std::numeric_limits<float >::lowest ();
286- float minZ = std::numeric_limits<float >::max ();
287- float maxZ = std::numeric_limits<float >::lowest ();
288- for (const auto & v : corners)
289- {
290- glm::vec4 trf = lightView * v;
291- minX = std::min (minX, trf.x );
292- maxX = std::max (maxX, trf.x );
293- minY = std::min (minY, trf.y );
294- maxY = std::max (maxY, trf.y );
295- minZ = std::min (minZ, trf.z );
296- maxZ = std::max (maxZ, trf.z );
297- }
298-
299- // Tune this parameter according to the scene
300- if (minZ < 0 )
301- {
302- minZ *= zMult;
303- }
304- else
305- {
306- minZ /= zMult;
307- }
308-
309- if (maxZ < 0 )
310- {
311- maxZ /= zMult;
312- }
313- else
314- {
315- maxZ *= zMult;
316- }
317-
318- const glm::mat4 lightProjection = glm::ortho (minX, maxX, minY, maxY, minZ, maxZ);
319-
320- /* float radius = 0.0f;
321- for (uint32_t i = 0; i < 8; i++)
322- {
323- float distance = glm::length(glm::vec3(corners[i]) - center);
324- radius = glm::max(radius, distance);
325- }
326- radius = std::ceil(radius * 16.0f) / 16.0f;
327-
328- glm::vec3 eye = center + normLightPos * radius;
329- glm::vec3 target = center;
330- glm::mat4 lightView = glm::lookAt(eye, target, glm::vec3(0.0f, 1.0f, 0.0f));
331- glm::mat4 lightProjection = glm::ortho(
332- -radius,
333- radius,
334- -radius,
335- radius,
336- 0.0f,
337- radius * 2.0f);*/
338-
339- return lightProjection * lightView;
340- }
341-
342206void PipelineShadow::FillCommandBuffer (VulkanContext& ctx, VkCommandBuffer commandBuffer)
343207{
344208 uint32_t frameIndex = ctx.GetFrameIndex ();
0 commit comments