从深度纹理采样总是得到0

时间:2017-04-13 09:53:44

标签: c++ opengl glsl shadow-mapping

我正在尝试使用opengl实现阴影贴图,使用定向光源。

我已经确认正确渲染了深度纹理,如下所示: depth texture

为了进行视觉检查,我还输出了世界空间坐标,转换为光透视:

shadow coords

再次,似乎没问题。

然而,当我使用:

对深度纹理进行采样时
vec3 coord = vec3(shadowCoords.xy,shadowCoords.z/shadowCoords.w);
float depth = texture( shadowMap, coord);

我发现比较总是失败。如果我将uniform sampler2DShadow shadowMap;更改为uniform sampler2D shadowMap;,并使用texture( shadowMap, coord.xy).r直接对纹理进行采样,我会通过将此结果输出到屏幕来发现采样值始终为零。

我像这样创建了深度纹理:

glGenTextures(1, &m_DepthTexture);
glBindTexture(GL_TEXTURE_2D, m_DepthTexture);
glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT16, width, height, 0, GL_DEPTH_COMPONENT, GL_FLOAT, NULL);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_FUNC, GL_LEQUAL);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE, GL_COMPARE_REF_TO_TEXTURE);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, m_DepthTexture, 0);

我的着色器代码如下(顶点):

#version 430
// Some drivers require the following
precision highp float;
layout (location = 0)in vec3 MSVertex;
layout (location = 4)in vec2 MSTexCoord;

out xferBlock
{
    vec3 VSVertex;
    vec2 VSTexCoord;
} outdata;

void main()
{
    outdata.VSVertex = MSVertex;
    outdata.VSTexCoord = MSTexCoord;
    gl_Position = vec4(MSVertex,1.0);
}

和片段着色器:

#version 430 core
// Some drivers require the following
precision highp float;

layout (location = 0) uniform sampler2D positionMap;
layout (location = 1) uniform sampler2D normalMap;
layout (location = 2) uniform sampler2D colourMap;
layout (location = 3) uniform sampler2D specularMap;
layout (location = 4) uniform sampler2DShadow shadowMap;

struct DirLightData
{
    vec4 colour;
    float intensity;
    vec4 direction;
};
uniform mat4 ShadowTransform;
uniform DirLightData dirLight;
out vec4 colour;
uniform vec3 WSCamPos;
in xferBlock
{
    vec3 VSVertex;
    vec2 VSTexCoord;
} indata;

vec3 computeLight(vec3 Ldirection, vec3 Vdirection, vec3 Lcolour, vec3 normal, float Lintensity, float specular)
{
    vec3 diffCol = Lcolour * max(0.0,dot(normalize(normal),-Ldirection));
    vec3 reflectVec = normalize(reflect(Ldirection,normal));
    float specFactor = max(dot(reflectVec,Vdirection),0);
    float specPow = pow(specFactor,specular*255.0);
    vec3 specCol = Lcolour * specPow;
    return (diffCol+specCol)*Lintensity;;
}
float computeOcclusion(vec4 shadowCoords)
{
    float vis = 1.0;
    vec3 coord = vec3(shadowCoords.xyz/shadowCoords.w);
    float depth = texture( shadowMap, vec3(coord.xy,coord.z+0.05));
    if (  depth < coord.z);
    {
        vis = 0.5;
    }
    return vis;
}
void main()
{
    vec4 pcolour = texture(positionMap, indata.VSTexCoord).rgba;
    vec4 ncolour = texture(normalMap, indata.VSTexCoord).rgba;
    vec4 dcolour = texture(colourMap, indata.VSTexCoord).rgba;
    vec4 scolour = texture(specularMap, indata.VSTexCoord).rgba;

    vec4 shadowCoord = ShadowTransform * pcolour;
    float visibility = computeOcclusion( shadowCoord );
    //float depth = texture(shadowMap, shadowCoord.xy).z;
    vec3 vToEye = WSCamPos - pcolour.xyz;
    vToEye = normalize(vToEye);
    vec3 outColour = vec3(0.0,0.0,0.0);

    outColour = computeLight(dirLight.direction.xyz,vToEye,dirLight.colour.rgb,ncolour.rgb,dirLight.intensity,scolour.r);
    colour = vec4(visibility*(dcolour.rgb*outColour),1.0);

}

有人能发现这出错的地方吗?

2 个答案:

答案 0 :(得分:3)

事实证明我的采样返回了正确的值。

真正的问题是在渲染深度贴图时剔除几何体的背面,以及通过错误地采样深度纹理处理返回值而导致的细粒度阴影痤疮。

我将采样功能更改为:

float computeOcclusion(vec4 shadowCoords)
{
    vec3 coord = vec3(shadowCoords.xyz/shadowCoords.w);
    float depth = texture( shadowMap, vec3(coord.xy,coord.z+0.001));
    return depth;
}

并将我的深度渲染gl客户端代码更改为包括:

glCullFace(GL_FRONT); 

在将几何体渲染到shadowMap之前。

一旦完成,我得到以下结果: final render

还有一些彼此平移问题,但这是我可以单独处理的事情。

答案 1 :(得分:1)

这样的问题很难被发现所以这里有一些你需要看的东西:

  1. 您使用的是samplerShadow,这是一种特殊的方法,您需要设置COMPARE_R_TO_TEXTURE,并且不需要if语句。或者,您可以使用sampler2D,但是您需要在着色器中执行if语句,类似于您所做的操作,但是coord必须是vec2而不是vec3

  2. 考虑将textureProjvec4纹理坐标一起使用。因为使用矩阵乘法在光空间中转换世界位置要容易得多。它与用于首先渲染阴影贴图的矩阵相同。在你的情况下,它是这个值vec4 shadowCoord = ShadowTransform * pcolour;;直接将shadowCoordtextureProj一起使用。