C# Opengl OpenTK-绘制深度缓冲区时的白色屏幕

C# Opengl OpenTK-绘制深度缓冲区时的白色屏幕,c#,opengl,shadow,opentk,depth-buffer,C#,Opengl,Shadow,Opentk,Depth Buffer,我目前正在尝试将阴影贴图添加到我的3D引擎中 首先,我从灯光的角度渲染场景,并将深度值保存在纹理中。然后我使用default FBO从纹理中绘制。就像在教程中一样。 问题是无论我移动到哪里,我的屏幕都保持白色 GL.GetError()输出noError和我在顶点着色器中使用的SSBO具有正确的值GL.CheckFramebufferStatus()返回FrameBufferCompleteText 以下是我为深度值创建FBO的方式: _depthMapFBO = GL.GenFramebuff

我目前正在尝试将阴影贴图添加到我的3D引擎中

首先,我从灯光的角度渲染场景,并将深度值保存在纹理中。然后我使用default FBO从纹理中绘制。就像在教程中一样。 问题是无论我移动到哪里,我的屏幕都保持白色

GL.GetError()
输出noError和我在顶点着色器中使用的SSBO具有正确的值
GL.CheckFramebufferStatus()
返回
FrameBufferCompleteText

以下是我为深度值创建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();
以及绘制深度贴图的着色器:

#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);
}

\u lightSpaceMatrix
的计算错误。OpenTK矩阵乘法是反向的。见:

由于在C#和OpenTK中处理矩阵的方式,乘法顺序与您在C/C++和GLSL中所期望的相反。这是图书馆里的一件旧艺术品,不幸的是,现在改变已经太晚了

将矩阵相乘时,交换
\u LIGHTHVIEW
\u lightProjection

\u lightSpaceMatrix=\u lightProjection*\u ligthView

\u lightSpaceMatrix=\u ligthView*\u lightProjection;