OpenGL OpenTK -绘制深度缓冲区时显示白色屏幕

hsvhsicv  于 2024-01-07  发布在  其他
关注(0)|答案(1)|浏览(154)

我目前正在尝试添加阴影与阴影Map到我的3D引擎。
首先,我从光线的Angular 渲染场景,并将深度值保存在纹理中。然后,我使用defeault FBO从纹理中绘制。就像在this教程中一样。问题是,我的屏幕保持白色,无论我移动到哪里。
GL.GetError()输出noError,我在顶点着色器中使用的SSBO具有正确的值。GL.CheckFramebufferStatus()返回FramebufferCompleteExt
这是我如何创建深度值的FBO:

_depthMapFBO = GL.GenFramebuffer();
_depthMapFBOColorBuffer = BufferObjects.FBO_TextureAttachment(_depthMapFBO, PixelInternalFormat.DepthComponent, PixelFormat.DepthComponent, FramebufferAttachment.DepthAttachment, 1024, 1024);
GL.BindFramebuffer(FramebufferTarget.Framebuffer, _depthMapFBO);
GL.DrawBuffer(DrawBufferMode.None);
GL.ReadBuffer(ReadBufferMode.None);

====================================

public static int FBO_TextureAttachment(int FrameBuffer, PixelInternalFormat PixelInternalFormat, PixelFormat PixelFormat, FramebufferAttachment FramebufferAttachment, int Width, int Height)
{
    // PixelInternalFormat = DepthComponent && PixelFormat = DepthComponent && FramebufferAttachment = DepthAttachment && Width, Height = 1024, 

    GL.BindFramebuffer(FramebufferTarget.Framebuffer, FrameBuffer);
    int _texture = GL.GenTexture();
            
    GL.BindTexture(TextureTarget.Texture2D, _texture);
    GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat, Width, Height, 0, PixelFormat, PixelType.Float, IntPtr.Zero);

    GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)All.Nearest);
    GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int)All.Nearest);
    GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapS, (int)All.Repeat);
    GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapT, (int)All.Repeat);

    GL.FramebufferTexture2D(FramebufferTarget.Framebuffer, FramebufferAttachment, TextureTarget.Texture2D, _texture, 0);
    return _texture;
}

字符串
在我的渲染函数中,它看起来像这样:

GL.BindFramebuffer(FramebufferTarget.Framebuffer, _depthMapFBO);
GL.Clear(ClearBufferMask.DepthBufferBit);

GL.Viewport(0, 0, 1024, 1024);
_simpleDepthProgram.Use();

float _nearPlane = 1.0f, _farPlane = 100f;
_lightProjection = Matrix4.CreateOrthographicOffCenter(-100.0f, 100.0f, -100.0f, 100.0f, _nearPlane, _farPlane);
_ligthView = Matrix4.LookAt(_allLamps[0].Position, new Vector3(0f), new Vector3(0.0f, 1.0f, 0.0f));

_lightSpaceMatrix = _lightProjection * _ligthView;
GL.UniformMatrix4(21, false, ref _lightSpaceMatrix);

// Copy all SSBO's 

GL.ActiveTexture(TextureUnit.Texture2);
GL.BindTexture(TextureTarget.Texture2D, _depthMapFBOColorBuffer);
Scene();


以及绘制depthMap的着色器:

#version 450 core
out vec4 FragColor;

uniform sampler2D scene;
uniform sampler2D bloomed;
uniform sampler2D depthMap;

uniform float zNear;
uniform float zFar;

float LinearizeDepth(float depth)
{
    float z = depth * 2.0 - 1.0; // Back to NDC 
    return (2.0 * zNear * zFar) / (zFar + zNear - z * (zFar - zNear));  
}

in vec2 TexCoord;
void main()
{ 
    float depthValue = texture(depthMap, TexCoord).r;
    //float depth = LinearizeDepth(gl_FragCoord.z) / far; // only for perspective 
    FragColor = vec4(vec3(depthValue), 1.0);
}

6kkfgxo0

6kkfgxo01#

_lightSpaceMatrix的计算是错误的,OpenTK矩阵乘法是反的,参见Problem with matrices #687
由于C#和OpenTK中矩阵的处理方式,乘法顺序与C/C++和GLSL中的顺序相反。这是库中的一个旧工件,不幸的是,现在改变已经太晚了。
当你乘矩阵时,交换_ligthView_lightProjection
_lightSpaceMatrix = _lightProjection * _ligthView;

_lightSpaceMatrix = _ligthView * _lightProjection;

字符串

相关问题