我正在将 csm 添加到我的项目中,它一整天都运行良好,直到我将 sponza 添加到我的项目中。 当我往下看时,它工作正常。
以下是我生成视图和项目矩阵的代码:
glm::mat4 lightViewMat = glm::lookAt(frustumCenter - lightDir , frustumCenter, glm::vec3(0.0, 1.0f, 0.0f));
float minX = std::numeric_limits<float>::max();
float maxX = std::numeric_limits<float>::lowest();
float minY = std::numeric_limits<float>::max();
float maxY = std::numeric_limits<float>::lowest();
float minZ = std::numeric_limits<float>::max();
float maxZ = std::numeric_limits<float>::lowest();
for (const auto& v : frustumCorners) {
const auto trf = lightViewMat * glm::vec4(v, 1.0f);
minX = std::min(minX, trf.x);
maxX = std::max(maxX, trf.x);
minY = std::min(minY, trf.y);
maxY = std::max(maxY, trf.y);
minZ = std::min(minZ, trf.z);
maxZ = std::max(maxZ, trf.z);
}
float zMult = 50.f;
if (minZ >= 0) {
minZ /= zMult;
} else {
minZ *= zMult;
}
if (maxZ < 0) {
maxZ /= zMult;
} else {
maxZ *= zMult;
}
glm::mat4 lightOrthoMat = glm::ortho(minX, maxX, minY, maxY, minZ, maxZ );
这是我在着色器中选择索引的方法:
uint cascadeIndex = 0;
vec3 viewPos = (uboScene.view * vec4(worldPos, 1.0f)).xyz;
for (uint i = 0; i < CSM_COUNT - 1; i++){
if (viewPos.z < csm.cascadeSplits[i]) {
cascadeIndex = i + 1;
}
}
我已经启用了PCF。 有人可以告诉我哪里出了问题吗:-(?这困扰了我很长一段时间。
您可以尝试将视锥体中心更改为 x 和 z 轴上的 -5.0 之类的值,然后通过平均视锥体角的坐标来获得视锥体中心,并将其用于光视图矩阵
frustumCenter = glm::vec3(-5.0f, 0.0f, -5.0f);
for (const auto& v : corners)
{
frustumCenter += glm::vec3(v);
}
frustumCenter /= corners.size();
glm::mat4 lightViewMat = glm::lookAt(frustumCenter - lightDir , frustumCenter, glm::vec3(0.0, 1.0f, 0.0f));
当我尝试解决同样的问题时,这对我有用