3

我正在尝试使用 OpenGL API 实现方差阴影贴图技术。我一直在使用教程(Fabien Sanglard's Soft shadows with VSM)并遵循每一步,但我的阴影贴图看起来有点奇怪。我注意到的主要事情是,当我在光(透视)投影矩阵的剪裁平面附近改变时,它开始看起来很奇怪。

例如,这是它在 1.0f 上接近剪裁http://postimg.org/image/rupf6wqcx/的样子(这个结果被认为是好的)

这是 0.1f 值http://postimg.org/image/fox04z14z/

请注意,灯的位置保持不变。

我一直试图找出问题所在,连续 3 天没有结果。你能帮我解决这个问题吗?

这是阴影片段着色器的代码。

in vec4 v_position;
out vec4 color;

void main()
{
    float depth = v_position.z / v_position.w;
    depth = depth * 0.5 + 0.5;

    float dx = dFdx(depth);
    float dy = dFdy(depth);

    float moment1 = depth;
    float moment2 = depth * depth - 0.25 * (dx * dx + dy * dy);

    color = vec4(moment1, moment2, 0.0, 1.0);
}

以及来自实际渲染通道片段着色器的阴影映射部分

in vec4 ShadowPosition;
out vec4 outColor;
uniform sampler2D shadowMap;
vec4 sc;

float chebyshevUpperBound(float distance)
{
    float p = 0.0;

    // We retrive the two moments previously stored (depth and depth*depth)
    vec2 moments = texture2D(shadowMap, sc.xy).rg;

    // Surface is fully lit. as the current fragment is before the light occluder
    if (distance <= moments.x)
        p = 1.0;

    // The fragment is either in shadow or penumbra. We now use chebyshev's upperBound to check
    // How likely this pixel is to be lit (p_max)
    float variance = moments.y - (moments.x * moments.x);
    variance = max(variance, 0.00001);

    float d = distance - moments.x;
    float p_max = variance / (variance + d*d);

    return max(p, p_max);
}

void main()
{
    /* Shadow Mapping */
    vec3 pixColor = vec3(1.0, 1.0, 1.0);
    sc = ShadowPosition / ShadowPosition.w;
    sc = sc * 0.5 + 0.5;
    float visibility = chebyshevUpperBound(sc.z);
    outColor = vec4(visibility * pixColor, 1.0);
}

顶点着色器非常简单,使用 MVP 矩阵从光线或相机的角度计算顶点,所以我认为我不需要发布它们。

此代码用于初始化和渲染:

glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LESS);
glEnable(GL_CULL_FACE);
glCullFace(GL_BACK);

glGenFramebuffers(1, &FramebufferName);

glGenTextures(1, &light_s.shadowBO);
glBindTexture(GL_TEXTURE_2D, light_s.shadowBO);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA32F, 1024, 1024, 0, GL_RGBA, GL_FLOAT, 0);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER);
glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_BORDER_COLOR, borderColor);

glGenTextures(1, &light_s.shadowBOZ);
glBindTexture(GL_TEXTURE_2D, light_s.shadowBOZ);
glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT32, 1024, 1024, 0, GL_DEPTH_COMPONENT, GL_FLOAT, 0);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER);
glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_BORDER_COLOR, borderColor);

glBindFramebuffer(GL_FRAMEBUFFER, FramebufferName);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, light_s.shadowBO, 0);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, light_s.shadowBOZ, 0);

while (running)
{
    if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }
    else
    {
        /* Shadow pass */
        glBindFramebuffer(GL_FRAMEBUFFER, FramebufferName);
        glViewport(0, 0, 1024, 1024);
        glDrawBuffer(GL_BACK);
        glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
        glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);

        glUseProgram(theShadowProgram);
        glEnableVertexAttribArray(svxposition);

        glUniformMatrix4fv(spmatrix, 1, GL_FALSE, light_s.mProjection);
        glUniformMatrix4fv(svmatrix, 1, GL_FALSE, light_s.mView);
        glUniformMatrix4fv(smmatrix, 1, GL_FALSE, bunny_s.mModel);

        glBindBuffer(GL_ARRAY_BUFFER, bunny_s.vertexBufferObject);
        glVertexAttribPointer(svxposition, 3, GL_FLOAT, GL_FALSE, 0, NULL);

        glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, bunny_s.elementBufferObject);
        glDrawElements(GL_TRIANGLES, bunny_s.elementBufferSize, GL_UNSIGNED_INT, NULL);

        glBindBuffer(GL_ARRAY_BUFFER, vbplaneVert);
        glVertexAttribPointer(svxposition, 3, GL_FLOAT, GL_FALSE, 0, NULL);

        glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vbplaneElem);
        glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, NULL);

        glDisableVertexAttribArray(svxposition);

        /* Rendering pass */
        glBindFramebuffer(GL_FRAMEBUFFER, 0);
        glViewport(0, 0, 1024, 768);
        glDrawBuffer(GL_BACK);
        glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
        glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);

        glUseProgram(theProgram);
        glActiveTexture(GL_TEXTURE0);
        glBindTexture(GL_TEXTURE_2D, light_s.shadowBO);

        glEnableVertexAttribArray(vxposition);
        glEnableVertexAttribArray(normals);

        glUniformMatrix4fv(dpmatrix, 1, GL_FALSE, light_s.mProjection);
        glUniformMatrix4fv(dvmatrix, 1, GL_FALSE, light_s.mView);
        glUniformMatrix4fv(dbmatrix, 1, GL_FALSE, mDepthBias);

        glUniformMatrix4fv(pmatrix, 1, GL_FALSE, camera_s.mProjection);
        glUniformMatrix4fv(vmatrix, 1, GL_FALSE, camera_s.mView);
        glUniformMatrix4fv(mmatrix, 1, GL_FALSE, bunny_s.mModel);

        glUniform3f(campos, camera_s.x, camera_s.y, camera_s.z);
        glUniform3f(lightpos, light_s.x, light_s.y, light_s.z);

        glUniform1i(frsampler, 0);

        glBindBuffer(GL_ARRAY_BUFFER, bunny_s.vertexBufferObject);
        glVertexAttribPointer(vxposition, 3, GL_FLOAT, GL_FALSE, 0, NULL);

        glBindBuffer(GL_ARRAY_BUFFER, bunny_s.normalBufferObject);
        glVertexAttribPointer(normals, 3, GL_FLOAT, GL_FALSE, 0, NULL);

        glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, bunny_s.elementBufferObject);
        glDrawElements(GL_TRIANGLES, bunny_s.elementBufferSize, GL_UNSIGNED_INT, NULL);

        glBindBuffer(GL_ARRAY_BUFFER, vbplaneVert);
        glVertexAttribPointer(vxposition, 3, GL_FLOAT, GL_FALSE, 0, NULL);

        glBindBuffer(GL_ARRAY_BUFFER, vbplaneNorm);
        glVertexAttribPointer(normals, 3, GL_FLOAT, GL_FALSE, 0, NULL);

        glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vbplaneElem);
        glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, NULL);

        glDisableVertexAttribArray(vxposition);
        glDisableVertexAttribArray(normals);
    }
}

它没有优化,但现在可以。

4

1 回答 1

3

如果使用GL_RG32F而不是GL_RGBA32F因为color.zw( BA) 是常量,则可以将阴影贴图存储和内存带宽需求减半。此外,您是否知道与透视投影越接近近平面depth * depth相比,变化非常缓慢?depth正如您在屏幕截图中看到的那样,这会导致精度问题。本文的集成部分简要讨论了这两件事。

基本上,您需要更智能地管理近平面和远平面(使其更接近最小/最大距离)......这对于任何阴影映射算法都是很好的做法,但对于 VSM 尤其重要。

我会考虑完全放弃透视深度并消除精度的非线性分布问题(算法工作不需要投影深度,如果你计算与光线的常规距离,它将正常工作)。这应该对您遇到的主要问题有很大帮助。

于 2013-12-14T00:38:09.863 回答