我正在尝试创建一个带有颜色和深度缓冲区的帧缓冲区对象。颜色缓冲区工作正常,但深度缓冲区不行。
问题不太可能出在场景绘制例程中,因为它在使用默认帧缓冲区时工作正常。禁用深度测试时场景会绘制一些东西,但启用深度测试时场景不会绘制。我只能得出结论,创建深度缓冲区并将其附加到帧缓冲区的方式存在问题。帧缓冲区已成功创建,我尝试了许多不同的深度缓冲区格式以及等效的渲染缓冲区,但这些更改似乎都没有任何区别。
我确实尝试使用 glReadPixels 读取深度缓冲区的像素。它似乎有一些变化,表明场景正在成功写入深度缓冲区。
以下是创建帧缓冲区的代码。还有什么我可以尝试修复或帮助我进一步调试问题的吗?也许需要为自定义帧缓冲区设置一些东西,但不是默认设置?
// Create and attach texture to framebuffer
_texture = (GLTexture)context.CreateTexture(width, height);
// Create depth texture
_depthBufferName = GL.GenTexture();
GL.BindTexture(TextureTarget.Texture2D, _depthBufferName);
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapS, (int)TextureWrapMode.ClampToEdge);
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapT, (int)TextureWrapMode.ClampToEdge);
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int)TextureMagFilter.Linear);
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.Linear);
GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.DepthComponent, width, height, 0, PixelFormat.DepthComponent, PixelType.Float, IntPtr.Zero);
// Create depth render buffer
// _depthBufferName = GL.GenRenderbuffer();
// GL.BindRenderbuffer(RenderbufferTarget.Renderbuffer, _depthBufferName);
// GL.RenderbufferStorage(RenderbufferTarget.Renderbuffer, RenderbufferStorage.Depth24Stencil8, width, height);
// Create and bind frame buffer
GL.GenFramebuffers(1, out _name);
GL.BindFramebuffer(FramebufferTarget.Framebuffer, _name);
GL.FramebufferTexture(FramebufferTarget.Framebuffer, FramebufferAttachment.ColorAttachment0, _texture.Id, 0);
GL.FramebufferTexture(FramebufferTarget.Framebuffer, FramebufferAttachment.DepthAttachment, _depthBufferName, 0);
// GL.FramebufferRenderbuffer(FramebufferTarget.Framebuffer, FramebufferAttachment.DepthStencilAttachment, RenderbufferTarget.Renderbuffer, _depthBufferName);
GL.DrawBuffers(1, new[] { DrawBuffersEnum.ColorAttachment0 });
// Check if framebuffer has been setup successfully
if (GL.CheckFramebufferStatus(FramebufferTarget.Framebuffer) != FramebufferErrorCode.FramebufferComplete)
throw new SDL2Exception("Unable to create framebuffer.");