0

シャドウ マップをサンプリングするための正しいテクスチャ座標を取得するのに問題があります。私のコードを見ると、問題は間違った行列にあるようです。これは、シャドウを行うレンダリング パスのフラグメント シェーダーです。

in vec2 st;

uniform sampler2D colorTexture;
uniform sampler2D normalTexture;
uniform sampler2D depthTexture;
uniform sampler2D shadowmapTexture;

uniform mat4 invProj;
uniform mat4 lightProj;

uniform vec3 lightPosition;

out vec3 color;

void main () {
    vec3 clipSpaceCoords;
    clipSpaceCoords.xy = st.xy * 2.0 - 1.0;
    clipSpaceCoords.z = texture(depthTexture, st).x * 2.0 - 1.0;
    vec4 position = invProj * vec4(clipSpaceCoords,1.0);
    position.xyz /= position.w;
    //At this point, position.xyz seems to be what it should be, the world space coordinates of the pixel. I know this because it works for lighting calculations.

    vec4 lightSpace = lightProj * vec4(position.xyz,1.0);
    //This line above is where I think things go wrong.
    lightSpace.xyz /= lightSpace.w;
    lightSpace.xyz = lightSpace.xyz * 0.5 + 0.5;
    float lightDepth = texture(shadowmapTexture, lightSpace.xy).x;
    //Right here lightDepth seems to be incorrect. The only explanation I can think of for this is if there is a problem in the above calculations leading to lightSpace.xy.

    float shadowFactor = 1.0;
    if(lightSpace.z > lightDepth+0.0005) {
         shadowFactor = 0.2;
    }
    color = vec3(lightDepth);
}

このシェーダーからシャドウイングに関係のないすべてのコード (ライティングなど) を削除しました。これは、最終パスをレンダリングするために使用するコードです。

glCullFace(GL_BACK);
glBindFramebuffer(GL_FRAMEBUFFER, 0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

postShader->UseShader();
postShader->SetUniform1I("colorTexture", 0);
postShader->SetUniform1I("normalTexture", 1);
postShader->SetUniform1I("depthTexture", 2);
postShader->SetUniform1I("shadowmapTexture", 3);
//glm::vec3 cp = camera->GetPosition();
postShader->SetUniform4FV("invProj", glm::inverse(camera->GetCombinedProjectionView()));
postShader->SetUniform4FV("lightProj", lights[0].camera->GetCombinedProjectionView());
//Again, if I had to guess, these two lines above would be part of the problem.
postShader->SetUniform3F("lightPosition", lights[0].x, lights[0].y, lights[0].z);

glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, frameBuffer->GetColor());
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, frameBuffer->GetNormals());
glActiveTexture(GL_TEXTURE2);
glBindTexture(GL_TEXTURE_2D, frameBuffer->GetDepth());
glActiveTexture(GL_TEXTURE3);
glBindTexture(GL_TEXTURE_2D, lights[0].shadowmap->GetDepth());

this->BindPPQuad();
glDrawArrays(GL_TRIANGLES, 0, 6);

私の問題に関連する場合は、深度マップとシャドウ マップの深度フレームバッファ アタッチメントを生成する方法を次に示します。

void FrameBuffer::Init(int textureWidth, int textureHeight) {
    glGenFramebuffers(1, &fbo);
    glGenTextures(1, &depth);

    glBindTexture(GL_TEXTURE_2D, depth);
    glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT24, textureWidth, textureHeight, 0, GL_DEPTH_COMPONENT, GL_UNSIGNED_BYTE, NULL);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
    glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, depth, 0);

    glBindFramebuffer(GL_FRAMEBUFFER, 0);
}

数学またはコードのどこに問題があり、それを修正するにはどうすればよいですか?

4

1 に答える 1

0

いくつかの実験の後、私の問題はマトリックスにあるのではなく、クランプにあることがわかりました。GL_CLAMP や GL_CLAMP_TO_EDGE を使うと変な値になるようですが、GL_CLAMP_TO_BORDER を使うとほぼ正しい値になります。問題は他にもありますが、思ったほどマトリックス関連ではないようです。

于 2014-03-18T16:33:23.420 に答える