Skip to content

Commit

Permalink
修复直接光照下阴影显示不全
Browse files Browse the repository at this point in the history
  • Loading branch information
luxiaodong authored and kwbm committed Oct 21, 2022
1 parent a2a01cc commit 3183f4b
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 3 deletions.
43 changes: 42 additions & 1 deletion engine/source/runtime/core/math/math.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ namespace Piccolo
float C = -(right + left) * inv_width;
float D = -(top + bottom) * inv_height;
float q = -2 * inv_distance;
float qn = qn = -(zfar + znear) * inv_distance;
float qn = -(zfar + znear) * inv_distance;

// NB: This creates 'uniform' orthographic projection matrix,
// which depth range [-1,1], right-handed rules
Expand Down Expand Up @@ -192,4 +192,45 @@ namespace Piccolo
return proj_matrix;
}

Matrix4x4
Math::makeOrthographicProjectionMatrix01(float left, float right, float bottom, float top, float znear, float zfar)
{
float inv_width = 1.0f / (right - left);
float inv_height = 1.0f / (top - bottom);
float inv_distance = 1.0f / (zfar - znear);

float A = 2 * inv_width;
float B = 2 * inv_height;
float C = -(right + left) * inv_width;
float D = -(top + bottom) * inv_height;
float q = -1 * inv_distance;
float qn = -znear * inv_distance;

// NB: This creates 'uniform' orthographic projection matrix,
// which depth range [-1,1], right-handed rules
//
// [ A 0 0 C ]
// [ 0 B 0 D ]
// [ 0 0 q qn ]
// [ 0 0 0 1 ]
//
// A = 2 * / (right - left)
// B = 2 * / (top - bottom)
// C = - (right + left) / (right - left)
// D = - (top + bottom) / (top - bottom)
// q = - 1 / (far - near)
// qn = - near / (far - near)

Matrix4x4 proj_matrix = Matrix4x4::ZERO;
proj_matrix[0][0] = A;
proj_matrix[0][3] = C;
proj_matrix[1][1] = B;
proj_matrix[1][3] = D;
proj_matrix[2][2] = q;
proj_matrix[2][3] = qn;
proj_matrix[3][3] = 1;

return proj_matrix;
}

} // namespace Piccolo
3 changes: 3 additions & 0 deletions engine/source/runtime/core/math/math.h
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,9 @@ namespace Piccolo

static Matrix4x4
makeOrthographicProjectionMatrix(float left, float right, float bottom, float top, float znear, float zfar);

static Matrix4x4
makeOrthographicProjectionMatrix01(float left, float right, float bottom, float top, float znear, float zfar);
};

// these functions could not be defined within the class definition of class
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -527,7 +527,7 @@ namespace Piccolo
}

// Mesh
if (m_vulkan_rhi->isPointLightShadowEnabled())
// if (m_vulkan_rhi->isPointLightShadowEnabled())
{
if (m_vulkan_rhi->isDebugLabelEnabled())
{
Expand Down
2 changes: 1 addition & 1 deletion engine/source/runtime/function/render/render_helper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,7 @@ namespace Piccolo

BoundingBox frustum_bounding_box_light_view = BoundingBoxTransform(frustum_bounding_box, light_view);
BoundingBox scene_bounding_box_light_view = BoundingBoxTransform(scene_bounding_box, light_view);
light_proj = Math::makeOrthographicProjectionMatrix(
light_proj = Math::makeOrthographicProjectionMatrix01(
std::max(frustum_bounding_box_light_view.min_bound.x, scene_bounding_box_light_view.min_bound.x),
std::min(frustum_bounding_box_light_view.max_bound.x, scene_bounding_box_light_view.max_bound.x),
std::max(frustum_bounding_box_light_view.min_bound.y, scene_bounding_box_light_view.min_bound.y),
Expand Down

0 comments on commit 3183f4b

Please sign in to comment.