OpenGL帧缓冲深度缓冲不起作用

时间:2014-06-08 18:05:36

标签: c# opengl fbo depth-buffer depth-testing

我正在尝试使用颜色和深度缓冲区创建一个帧缓冲区对象。颜色缓冲区工作正常,但深度缓冲区没有。

问题不在于场景绘制例程,因为它在使用默认帧缓冲区时工作正常。当禁用深度测试时,场景会绘制一些内容,但是当启用深度测试时,场景不会绘制。我只能得出结论,深度缓冲区的创建和附加到帧缓冲区的方式存在问题。帧缓冲区已成功创建,我尝试了许多不同的深度缓冲区格式以及等效的渲染缓冲区,但这些变化似乎没有任何区别。

我确实尝试使用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.");

0 个答案:

没有答案