3

我无法让阴影映射在我的应用程序中工作。我尝试渲染一辆四轮摩托车并在其下方的地板上查看它的影子。这是我的一些代码。纹理创建:

    // Create a depth texture
    glGenTextures(1, &depth_texture);
    glBindTexture(GL_TEXTURE_2D, depth_texture);
    // Allocate storage for the texture data
    glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT32 ,1600, 900, 0, GL_DEPTH_COMPONENT, GL_FLOAT, NULL);
    // Set the default filtering modes
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    // Set up wrapping modes
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
    glBindTexture(GL_TEXTURE_2D, 0);
    // Create FBO to render depth into
    glGenFramebuffers(1, &depth_fbo);
    glBindFramebuffer(GL_FRAMEBUFFER, depth_fbo);
    // Attach the depth texture to it
    glFramebufferTexture(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT,
    depth_texture, 0);
    // Disable color rendering as there are no color attachments
    glDrawBuffer(GL_NONE);
    //check fbo status
    GLenum result = glCheckFramebufferStatus(GL_FRAMEBUFFER);
    if(result != GL_FRAMEBUFFER_COMPLETE)
        throw std::runtime_error("shadow mapping framebuffer error");
    //bind default framebuffer
    glBindFramebuffer(GL_FRAMEBUFFER, 0);

渲染到深度纹理:

progShadow.Use();
glBindFramebuffer(GL_FRAMEBUFFER, depth_fbo);

glClear(GL_DEPTH_BUFFER_BIT);

glm::mat4 shadowProjection = glm::frustum(-1.0f, 1.0f, -1.0f, 1.0f, 1.0f, 100.0f);
glm::mat4 shadowView = glm::lookAt(light.position, glm::vec3(0,0,0), glm::vec3(0,1,0));
glm::mat4 shadowModel(1);
if(g_rotate)
    shadowModel = glm::rotate((float)clock() / (float)CLOCKS_PER_SEC, glm::vec3(0,1,0));
glm::mat4 shadowMVP = shadowProjection * shadowView * shadowModel;
progShadow.SetUniform("MVPMatrix", shadowMVP);
quadBike.Draw();

我还使用渲染深度纹理的“测试”着色器程序。这是它的样子。 图片

所以我想我到现在都很好。现在我正常渲染场景。

glBindTexture(GL_TEXTURE_2D, depth_texture);
prog.Use();//main program
glBindFramebuffer(GL_FRAMEBUFFER, 0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glm::mat4 shadowBias = glm::mat4(0.5, 0.0, 0.0, 0.0,
                                 0.0, 0.5, 0.0, 0.0,
                                 0.0, 0.0, 0.5, 0.0,
                                 0.5, 0.5, 0.5, 1.0);
glm::mat4 ShadowBiasMVP = shadowBias * shadowMVP;
prog.SetUniform("ShadowBiasMVP", ShadowBiasMVP);

//draw quadBike and floor
...

我的顶点着色器的相关部分:

#version 430

...

out vec4 shadowCoord;

void main()
{
    gl_Position = ProjectionMatrix * CameraMatrix * ModelMatrix * vec4(vertex, 1.0);
    shadowCoord = ShadowBiasMVP * vec4(vertex, 1.0);
    ...
}

我的片段着色器的相关部分:

#version 430

...

uniform sampler2D shadowMap;
in vec4 shadowCoord;

void main()
{
    ...

    float visibility = 1.0;
    if ( texture(shadowMap, shadowCoord.xy).z <  shadowCoord.z)
        visibility = 0.0;

    ...
}

现在的问题是我得到了一个完全黑暗的场景,就好像它被阴影覆盖了一样。只有当灯光真正靠近四轮摩托车时,它才会正常渲染。(深度纹理在右侧可见,因为它是用不同的程序渲染的。我用它来测试) 场景

我究竟做错了什么?

4

1 回答 1

2
  1. 您应该在第一个组件处读取灰度深度纹理。

    texture(shadowMap, shadowCoord.xy).r或者

    texture(shadowMap, shadowCoord.xy).x

  2. 阴影坐标应该在插值后去均匀化(除以 w)。

    -> 在片段着色器中:shadowPos = shadowPos/shadowPos.w;

  3. 如果没有使用多边形偏移等其他技术,则需要从阴影深度值中减去偏差以防止自阴影。

这是一个在片段着色器中计算阴影的示例函数。注意:它是延迟渲染器的一部分,这就是在片段着色器中进行矩阵乘法的原因。

float calculateShadow(vec3 position){
    vec4 shadowPos = depthBiasMV * vec4(position,1);
    shadowPos = shadowPos/shadowPos.w;


     float bias =  0.0012;
     float visibility = 1.0f;
     if ((shadowPos.x < 0 || shadowPos.x > 1 || shadowPos.y < 0 || shadowPos.y > 1 || shadowPos.z < 0 || shadowPos.z > 1)){
       visibility = 1.0f;
     }else{
        float shadowDepth = texture(depthTex, shadowPos.xy).r;
        if(shadowDepth<shadowPos.z-bias)
            visibility = 0.0f;
     }
     return visibility;

}
于 2014-12-25T19:40:33.467 回答