点光源双抛物面VSM延迟渲染

时间:2018-05-23 07:40:10

标签: opengl glsl shadow opengl-3 deferred-shading

我一直在关注this tutorial为延迟渲染中的点光源实现方差阴影贴图功能。

我正在使用GLSL 3.3,左手坐标系。这是我一直在做的事情:

我将场景渲染为双抛物面地图,存储深度和深度*深度。

结果:enter image description here

上图包含正面和背面地图。点光源位于场景的中心,您可以看到它最黄的位置。

然后我设置了全屏着色器通道。 我这样做是通过将教程代码从FX转换为GLSL。

作者的.fx代码:

    float4 TexturePS(float3 normalW : TEXCOORD0, float2 tex0 : TEXCOORD1, float3 pos : TEXCOORD2) : COLOR
    {
        float4 texColor = tex2D(TexS, tex0 * TexScale);

        pos = mul(float4(pos, 1.0f), LightView);

        float L = length(pos);
        float3 P0 = pos / L;

        float alpha = .5f + pos.z / LightAttenuation;

        P0.z = P0.z + 1;
        P0.x = P0.x / P0.z;
        P0.y = P0.y / P0.z;
        P0.z = L / LightAttenuation;

        P0.x = .5f * P0.x + .5f;
        P0.y = -.5f * P0.y + .5f;

        float3 P1 = pos / L;

        P1.z = 1 - P1.z;
        P1.x = P1.x / P1.z;
        P1.y = P1.y / P1.z;
        P1.z = L / LightAttenuation;

        P1.x = .5f * P1.x + .5f;
        P1.y = -.5f * P1.y + .5f;

        float depth;
        float mydepth;
        float2 moments;
        if(alpha >= 0.5f)
        {
            moments = tex2D(ShadowFrontS, P0.xy).xy;
            depth = moments.x;
            mydepth = P0.z;
        }
        else
        {
            moments = tex2D(ShadowBackS, P1.xy).xy;
            depth = moments.x;
            mydepth = P1.z;
        }

        float lit_factor = (mydepth <= moments[0]);

        float E_x2 = moments.y;
        float Ex_2 = moments.x * moments.x;
        float variance = min(max(E_x2 - Ex_2, 0.0) + SHADOW_EPSILON, 1.0);
        float m_d = (moments.x - mydepth);
        float p = variance / (variance + m_d * m_d); //Chebychev's inequality

        texColor.xyz *= max(lit_factor, p + .2f);

        return texColor;
    }

我翻译的GLSL代码:

    void main() {
        vec3 in_vertex = texture(scenePosTexture, texCoord).xyz; // get 3D vertex from 2D screen coordinate
        vec4 vert = lightViewMat * vec4(in_vertex, 1); // project vertex to point light space (view from light position, look target is -Z)

        float L = length(vert.xyz);

        float distance = length(lightPos - in_vertex);
        float denom = distance / lightRad + 1;
        float attenuation = 1.0 / (denom * denom);

        // to determine which paraboloid map to use
        float alpha = vert.z / attenuation + 0.5f;

        vec3 P0 = vert.xyz / L;

        P0.z = P0.z + 1;
        P0.x = P0.x / P0.z;
        P0.y = P0.y / P0.z;
        P0.z = L / attenuation;

        P0.x = .5f * P0.x + .5f;
        P0.y = -.5f * P0.y + .5f;

        vec3 P1 = vert.xyz / L;

        P1.z = 1 - P1.z;
        P1.x = P1.x / P1.z;
        P1.y = P1.y / P1.z;
        P1.z = L / attenuation;

        P1.x = .5f * P1.x + .5f;
        P1.y = -.5f * P1.y + .5f;

        // Variance shadow mapping
        float depth;
        float mydepth;
        vec2 moments;

        if(alpha >= 0.5f)
        {
            moments = texture(shadowMapFrontTexture, P0.xy).xy;
            depth = moments.x;
            mydepth = P0.z;
        }
        else
        {
            moments = texture(shadowMapBackTexture, P1.xy).xy;
            depth = moments.x;
            mydepth = P1.z;
        }

        // Original .fx code is: float lit_factor = (mydepth <= moments[0]);
        // I'm not sure my translated code belew is correct

        float lit_factor = 0;
        if (mydepth <= moments.x)
            lit_factor = mydepth;
        else
            lit_factor = moments.x;

        float E_x2 = moments.y;
        float Ex_2 = moments.x * moments.x;
        float variance = min(max(E_x2 - Ex_2, 0.0) + SHADOW_EPSILON, 1.0);
        float m_d = (moments.x - mydepth);
        float p = variance / (variance + m_d * m_d); //Chebychev's inequality

        fragColor = texture(sceneTexture, texCoord).rgb; // sample original color
        fragColor.rgb *= max(lit_factor, p + .2f);
    }

渲染结果 enter image description here enter image description here

现在我对于要触摸正确渲染阴影的地方一无所知。有人能为我指出它吗?

2 个答案:

答案 0 :(得分:1)

我的一些朋友指出Y组件被翻转,这就是为什么阴影看起来像上下。在减去P0和P1的Y之后,它开始显示非常合理的阴影: enter image description here

但另一个问题是阴影的位置是错误的。 enter image description here

答案 1 :(得分:0)

为什么要复制抛物面投影计算?
你在&#39; vert&#39;上计算它,然后&#39; P0&#39;和&#39; P1&#39;,不应该只在&#39; P0&#39;和&#39; P1&#39; ? (原始代码不会在&#39; pos&#39;)上做这件事。

编辑:

你的lit_factor错了,它应该是0.0或1.0 你可以用这种方式使用step()GLSL内在:
float lit_factor = step(mydepth, moments[0]);