C++ 比较阴影贴图矩阵和绘制矩阵不会产生阴影

标签 c++ opengl glsl shader shadow-mapping

我正在尝试实现阴影贴图。我能够将深度图渲染到帧缓冲区纹理上。并将其发送到着色器,以及灯光正交矩阵以测试片段是否在阴影中。但结果不正确。如果它们看起来像在光矩阵“内部”,则所有片段都不会创建阴影,而是被认真地绘制。

img

正如您在左下角看到的,生成的深度纹理是正确的。

我试过很多教程,但都没有提到这样的问题。这可能是因为光线方向和正射投影是错误的,但我似乎无法找到产生任何阴影的工作光线方向。

这就是我生成帧缓冲区和纹理的方式(frameBuffer 和 framebufferTexture 在类头中实例化):

glGenFramebuffers(1, &frameBuffer);
glBindFramebuffer(GL_FRAMEBUFFER, frameBuffer);

//texture buffer for frame buffer
glGenTextures(1, &framebufferTexture);
glBindTexture(GL_TEXTURE_2D, framebufferTexture);
glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT16, 896, 504, 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);

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_FUNC, GL_LESS);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE, GL_COMPARE_REF_TO_TEXTURE);


//Set framebufferTexture as our color attachment #0
glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_DEPTH_ATTACHMENT,
    GL_TEXTURE_2D, framebufferTexture, 0);

glDrawBuffer(GL_NONE);

这就是我创建深度纹理的方式

glCullFace(GL_BACK);
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, frameBuffer);
//Setup depth texture framebuffer rendering
glEnable(GL_POLYGON_OFFSET_FILL);
glPolygonOffset(2.0f, 2.0f);
glViewport(0, 0, 896, 504);
glClear(GL_DEPTH_BUFFER_BIT);

glUniform1i(glGetUniformLocation(shaderProgram, "fragmentTypeTarget"), -1);


glm::vec3 lightInvDir = glm::vec3(5, 17, 1);
//Calculate the matrix for drawing the framebuffer
//Make it an orthographic that takes the whole screen
glm::mat4 depthProjectionMatrix = glm::ortho<float>(-17, 17, -17, 17, 0, 37);
glm::mat4 depthViewMatrix = glm::lookAt(
    lightInvDir,
    glm::vec3(0, 0, 0),
    glm::vec3(0, 1, 0));
glm::mat4 lightSpaceMatrix = depthProjectionMatrix * depthViewMatrix;

//Render the objects
for (int i = 0; i < objectPool.size(); i++) {
    objectPool[i]->draw(lightSpaceMatrix);
}

这就是我发送用于绘制阴影的阴影贴图矩阵的方式:

glm::mat4 Model = glm::translate(glm::mat4(1.0), glm::vec3(x, y, z));
glm::vec3 lightInvDir = glm::vec3(5, 17, 1);
    //Calculate the matrix for drawing the framebuffer
    //Make it an orthographic that takes the whole screen
    glm::mat4 depthProjectionMatrix = glm::ortho<float>(-17, 17, -17, 17, 0, 37);
    glm::mat4 depthViewMatrix = glm::lookAt(
        lightInvDir,
        glm::vec3(0, 0, 0),
        glm::vec3(0, 1, 0));
    glm::mat4 lightSpaceMatrix = depthProjectionMatrix * depthViewMatrix * Model;
glUniformMatrix4fv(glGetUniformLocation(shader, "shadowMatrix"), 1, GL_FALSE, &lightSpaceMatrix[0][0]);

我的片段着色器:

#version 330 core
layout(location = 0) out vec4 outColor;
in vec2 UV;
in vec4 ShadowCoord;
uniform sampler2D textureSampler;
uniform sampler2D shadowMap;
uniform int fragmentTypeTarget;


// 'colorImage' is a sampler2D with the depth image
// read from the current depth buffer bound to it.
float LinearizeDepth(in vec2 uv, sampler2D t)
{
    float zNear = 1.0;    
    float zFar  = 5.0; 
    float depth = texture2D(t, uv).x;
    return (2.0 * zNear) / (zFar + zNear - depth * (zFar - zNear));
}

void main(){
if(fragmentTypeTarget == -1){//draw frame buffer

}else if(fragmentTypeTarget == 0){//Draw everything normal
    vec4 tex = texture2D(textureSampler, UV);
    outColor = vec4(tex.rgb, tex.a);
}else if(fragmentTypeTarget == 1){//Draw depth texture
    //vec2 res = gl_FragCoord.xy / vec2(1024, 1024);
    outColor = texture(textureSampler, UV);
    float c = LinearizeDepth(UV, textureSampler);
    outColor = vec4(c, c, c, 1.0);
}else if(fragmentTypeTarget == 2){//Draw everything but apply the shadow
    float visibility = 1.0;
    float bias = 0.0039;

    if(texture(shadowMap, ShadowCoord.xy).z < ShadowCoord.z){
        visibility = 0.3;
    }
    vec4 tex = texture2D(textureSampler, UV);
    outColor = visibility * vec4(tex.rgb, tex.a);
}
}

我的顶点着色器:

#version 330 core
in vec2 UVsIn;
out vec2 UV;
in vec3 position;
in vec3 vertexColor;
out vec3 fragmentColor;
uniform mat4 mvp;
uniform mat4 shadowMatrix;
out vec4 ShadowCoord;

void main(){
gl_Position = mvp * vec4(position, 1.0);
fragmentColor = vertexColor;
UV = UVsIn;

ShadowCoord = shadowMatrix * vec4(position, 1.0);
}

结果应该更像这样:

img

编辑:

我想我让它工作了。 在生成帧缓冲区纹理时,我使用了错误的比较函数。它应该是:“GL_COMPARE_R_TO_TEXTURE”而不是“GL_COMPARE_REF_TO_TEXTURE”。

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE, GL_COMPARE_R_TO_TEXTURE);

openGl 文档说:GL_COMPARE_R_TO_TEXTURE:指定应将插值和钳位的 r 纹理坐标与当前绑定(bind)的深度纹理中的值进行比较

这正是我想要的。

我还发现纹理坐标在 (s, t, r) 而不是 (x, y, z)。

因为我想比较矩阵和纹理的深度,所以我需要使用 r 而不是 z。 所以片段着色器应该这样写:

float visibility = 1.0;

vec2 mapUV = ShadowCoord.xy * 0.5 + 0.5;
float depthFrag = ShadowCoord.z * 0.5 + 0.5;

float depthMap = texture(shadowMap, mapUV).r;//r instead of z since its (s, t, r)

if(depthMap < depthFrag){
    visibility = 0.3;
}

vec4 tex = texture2D(textureSampler, UV);
outColor = visibility * vec4(tex.rgb, tex.a);

还需要从 Rabbid76 响应中添加“* 0.5 + 0.5”。

最佳答案

阴影贴图的内容是深度范围 [0.0, 1.0] 中的深度值(除非您将深度范围更改为 glDepthRange )

ShadowCoord 是剪辑空间坐标。剪辑空间坐标可以通过 Perspective divide 转换为规范化的设备空间坐标。 (ShadowCoord.xyz/ShadowCoord.w)。由于阴影投影是正交的,这可以跳过,因为 ShadowCoord.w == 1.0。 标准化的设备空间坐标在 [-1.0, 1.0] 范围内。

所以它必须是:

vec2  mapUV     = ShadowCoord.xy * 0.5 + 0.5; // [-1.0, 1.0] -> [0.0, 1.0]
float depthFrag = ShadowCoord.z  * 0.5 + 0.5; // [-1.0, 1.0] -> [0.0, 1.0]

float depthMap  = texture(shadowMap, mapUV).r;

if(depthMap < depthFrag) {
    visibility = 0.3;
}

关于C++ 比较阴影贴图矩阵和绘制矩阵不会产生阴影,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54115317/

相关文章:

c++ - 在 MATLAB MEX 库中使用 boost,与 MATLAB 的版本不同

c++ - 在带有 openGL 和 SDL 的 Linux 上使用 C++ 时出现 SIGSEGV 错误

linux - LWJGL - OpenGL 不渲染

c++ - 片段着色器中的“错误 : sampler arrays indexed with non-constant expressions are forbidden in GLSL 1. 30 及更高版本”

c++ - OpenGL Planet Generation - 简单矩阵问题(Planet "Spins"With Mouse)

c++ - 在 windows 和 linux 下用 ifstream 打开二进制 .ply 文件时的不同结果

c++ - 如何在这种 C++ 设计场景中避免胖/污染接口(interface) - OOPS?

c++ - 声明不相关的类,但它不能正确运行

c++ - 四元数和翻译

java - OpenGL VAO 多个 VBO 绑定(bind)问题