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

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

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

  1. _depthMapFBO = GL.GenFramebuffer();
  2. _depthMapFBOColorBuffer = BufferObjects.FBO_TextureAttachment(_depthMapFBO, PixelInternalFormat.DepthComponent, PixelFormat.DepthComponent, FramebufferAttachment.DepthAttachment, 1024, 1024);
  3. GL.BindFramebuffer(FramebufferTarget.Framebuffer, _depthMapFBO);
  4. GL.DrawBuffer(DrawBufferMode.None);
  5. GL.ReadBuffer(ReadBufferMode.None);
  6. ====================================
  7. public static int FBO_TextureAttachment(int FrameBuffer, PixelInternalFormat PixelInternalFormat, PixelFormat PixelFormat, FramebufferAttachment FramebufferAttachment, int Width, int Height)
  8. {
  9. // PixelInternalFormat = DepthComponent && PixelFormat = DepthComponent && FramebufferAttachment = DepthAttachment && Width, Height = 1024,
  10. GL.BindFramebuffer(FramebufferTarget.Framebuffer, FrameBuffer);
  11. int _texture = GL.GenTexture();
  12. GL.BindTexture(TextureTarget.Texture2D, _texture);
  13. GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat, Width, Height, 0, PixelFormat, PixelType.Float, IntPtr.Zero);
  14. GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)All.Nearest);
  15. GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int)All.Nearest);
  16. GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapS, (int)All.Repeat);
  17. GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapT, (int)All.Repeat);
  18. GL.FramebufferTexture2D(FramebufferTarget.Framebuffer, FramebufferAttachment, TextureTarget.Texture2D, _texture, 0);
  19. return _texture;
  20. }

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

  1. GL.BindFramebuffer(FramebufferTarget.Framebuffer, _depthMapFBO);
  2. GL.Clear(ClearBufferMask.DepthBufferBit);
  3. GL.Viewport(0, 0, 1024, 1024);
  4. _simpleDepthProgram.Use();
  5. float _nearPlane = 1.0f, _farPlane = 100f;
  6. _lightProjection = Matrix4.CreateOrthographicOffCenter(-100.0f, 100.0f, -100.0f, 100.0f, _nearPlane, _farPlane);
  7. _ligthView = Matrix4.LookAt(_allLamps[0].Position, new Vector3(0f), new Vector3(0.0f, 1.0f, 0.0f));
  8. _lightSpaceMatrix = _lightProjection * _ligthView;
  9. GL.UniformMatrix4(21, false, ref _lightSpaceMatrix);
  10. // Copy all SSBO's
  11. GL.ActiveTexture(TextureUnit.Texture2);
  12. GL.BindTexture(TextureTarget.Texture2D, _depthMapFBOColorBuffer);
  13. Scene();


以及绘制depthMap的着色器:

  1. #version 450 core
  2. out vec4 FragColor;
  3. uniform sampler2D scene;
  4. uniform sampler2D bloomed;
  5. uniform sampler2D depthMap;
  6. uniform float zNear;
  7. uniform float zFar;
  8. float LinearizeDepth(float depth)
  9. {
  10. float z = depth * 2.0 - 1.0; // Back to NDC
  11. return (2.0 * zNear * zFar) / (zFar + zNear - z * (zFar - zNear));
  12. }
  13. in vec2 TexCoord;
  14. void main()
  15. {
  16. float depthValue = texture(depthMap, TexCoord).r;
  17. //float depth = LinearizeDepth(gl_FragCoord.z) / far; // only for perspective
  18. FragColor = vec4(vec3(depthValue), 1.0);
  19. }

6kkfgxo0

6kkfgxo01#

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

  1. _lightSpaceMatrix = _ligthView * _lightProjection;

字符串

相关问题