0

我目前正在我的应用程序中实现阴影贴图支持,并且我一直在遵循此链接中演示的程序:http: //www.fabiensanglard.net/shadowmapping/index.php

但是,我注意到他正在使用纹理矩阵来投影阴影贴图,但我想这样做而不必使用纹理矩阵。

我渲染场景的方式如下:

  • 为深度纹理设置 FBO
  • 从灯光的角度设置投影和模型视图矩阵
  • 禁用颜色蒙版和渲染场景
  • 设置阴影矩阵:shadowMatrix = bias * lightProjectionMatrix * lightModelViewMatrix
  • 将阴影矩阵与实际相机模型视图矩阵的逆矩阵相乘
  • 将 shadowMatrix 作为 mat4 统一变量发送到着色器

在我的顶点着色器中,我有一个不同的 vec4 shadowCoord 变量,它是通过将 shadowMatrix 均匀乘以 gl_ModelViewMatrix 和 gl_Vertex (shadowCoord = shadowMatrix * gl_ModelViewMatrix * gl_Vertex) 计算得出的

片段着色器与上面链接中提到的完全相同。结果显示场景完全黑色或阴影设置的任何阴影/颜色。

我假设在将偏差、lightProjectionMatrix、lightModelViewMatrix 与相机模型视图矩阵的倒数相乘时我做错了。有人可以指出我做错了什么吗?

编辑:我希望有人可以识别问题而不必由于复杂性而粘贴代码,但无论如何我都会尝试粘贴我可以粘贴的内容。对不起,如果代码是一团糟,为了简单起见,我不得不删除很多代码:

int vp[4];
kexMatrix lightProj, lightMV;
kexCamera tmpCam;

renderBackend.SetColorMask(0);

// save actual camera viewport
glGetIntegerv(GL_VIEWPORT, vp);

// set viewport to framebuffer object size (512x512)
// smFbo is my framebuffer class used for the shadowmap/depth
glViewport(0, 0, smFbo.Width(), smFbo.Height());

// bind framebuffer
smFbo.Bind();
glClear(GL_DEPTH_BUFFER_BIT);

// setup temp. camera for light source
tmpCam.SetOrigin(kexVec3(-2048, 860, 450)); // x, y, z with y being up, and z being forward/back
tmpCam.SetAngles(kexAngle(1.0f, 0.5f, 0.0f)); // yaw, pitch and roll in radians
tmpCam.SetupMatrices();

lightProj = tmpCam.Projection();
lightMV = tmpCam.ModelView();

// setup matrices for OGL
glMatrixMode(GL_PROJECTION);
glLoadMatrixf(lightProj.ToFloatPtr());
glMatrixMode(GL_MODELVIEW);
glLoadMatrixf(lightMV.ToFloatPtr());

bShadowPass = true;
// note that each static actor object has its own matrix so before drawing I do:
// glPushMatrix();
// glMultMatrixf(actor->Matrix());
// draw and pop matrix
DrawStaticActors();
bShadowPass = false;

// unbind framebuffer object and restore viewport
smFbo.UnBind();
glViewport(vp[0], vp[1], vp[2], vp[3]);
renderBackend.SetColorMask(1);
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);

kexMatrix bias;

bias.vectors[0].Set(0.5f, 0, 0, 0);
bias.vectors[1].Set(0, 0.5f, 0, 0);
bias.vectors[2].Set(0, 0, 0.5f, 0);
bias.vectors[3].Set(0.5f, 0.5f, 0.5f, 1.0f);

// setup matrices from world camera. handles gl projection/modelview setup as well
SetCameraView(world->Camera());

// shadowmap matrix for GLSL shader
shadowMatrix = bias * lightProj * lightMV * kexMatrix::Invert(world->Camera()->ModelView());

// draw scene as normal and sets up the shader
// shadowMap sampler2D uniform is binded to 1
// also calls smFbo.BindImage() which binds the actual texture to texture unit # 1
// shadowMatrix is also sent as uniform to shader
DrawStaticActors();

深度帧缓冲设置

// texture
glGenTextures(1, &fboTexId);
glBindTexture(GL_TEXTURE_2D, fboTexId);
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_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D,
              0,
              GL_DEPTH_COMPONENT,
              fboWidth,
              fboHeight,
              0,
              GL_DEPTH_COMPONENT,
              GL_FLOAT,
              0);

// framebuffer
glGenFramebuffers(1, &fboId);
glBindFramebuffer(GL_FRAMEBUFFER_EXT, fboId);
glDrawBuffer(GL_NONE);
glReadBuffer(GL_NONE);
glFramebufferTexture2D(GL_FRAMEBUFFER_EXT,
                        GL_DEPTH_ATTACHMENT_EXT,
                        GL_TEXTURE_2D,
                        fboTexId,
                        0);

CheckStatus();

glBindFramebuffer(GL_FRAMEBUFFER_EXT, 0);
glBindTexture(GL_TEXTURE_2D, 0);
glDrawBuffer(GL_BACK);
glReadBuffer(GL_BACK);

FBO绑定功能

glBindFramebuffer(GL_FRAMEBUFFER_EXT, fboId);
glReadBuffer(GL_NONE);
glDrawBuffer(GL_NONE);

FBO 解绑功能

glBindFramebuffer(GL_FRAMEBUFFER_EXT, 0);
glDrawBuffer(GL_BACK);
glReadBuffer(GL_BACK);

FBO 绑定图像功能

int unit = renderBackend.glState.currentUnit;
dtexture currentTexture = renderBackend.glState.textureUnits[unit].currentTexture;

if(fboTexId == currentTexture) {
    return;
}

glBindTexture(GL_TEXTURE_2D, fboTexId);
renderBackend.glState.textureUnits[unit].currentTexture = fboTexId;

顶点着色器

//varying vec3 vertex_normal;
varying vec4 shadowCoord;

uniform mat4 shadowMatrix;

void main() {
    gl_FrontColor   = gl_Color;
    gl_Position     = gl_ProjectionMatrix * gl_ModelViewMatrix * gl_Vertex;
    gl_TexCoord[0]  = gl_MultiTexCoord0;

    //vertex_normal = normalize(gl_NormalMatrix * gl_Normal);
    shadowCoord = shadowMatrix * (gl_ModelViewMatrix * gl_Vertex);
}

片段着色器

uniform sampler2D diffuse;
uniform sampler2D shadowMap;

//varying vec3 vertex_normal;
varying vec4 shadowCoord;

void main() {
    vec4 color = texture2D(diffuse, gl_TexCoord[0].st);

    vec4 shadowCoordinateWdivide = shadowCoord / shadowCoord.w;
    float shadow = 1.0;

    if(shadowCoordinateWdivide.x >= 0.0 && shadowCoordinateWdivide.x < 1.0 &&
       shadowCoordinateWdivide.y >= 0.0 && shadowCoordinateWdivide.y < 1.0 &&
       shadowCoordinateWdivide.z >= 0.0 && shadowCoordinateWdivide.z < 1.0) {
        // Used to lower moiré pattern and self-shadowing
        shadowCoordinateWdivide.z += 0.0005;


        float distanceFromLight = texture2D(shadowMap, shadowCoordinateWdivide.st).z;

        if(shadowCoord.w > 0.0) {
            shadow = distanceFromLight < shadowCoordinateWdivide.z ? 0.0 : 1.0;
        }
    }

    gl_FragColor = vec4((color * shadow).xyz, color.a);

}

4

0 回答 0