无法呈现到绑定到帧缓冲区的纹理

问题描述:

我想设置渲染管道,并且由于此,我正在尝试将纹理设置为渲染,以便稍后对其进行采样以进行模糊处理类似。无法呈现到绑定到帧缓冲区的纹理

我有个顶点着色器,看起来像这样:

#version 400 

//Heyy renderpasses 
subroutine void RenderPass(); 
subroutine uniform RenderPass currentPass; 

in vec3 ex_normal; 
in vec3 ex_color; 
in vec4 ShadowCoord; 
in vec3 lightDir_cameraspace; 
in vec3 normal_cameraspace; 

layout(location=0) out vec4 color; 
layout(location=1) out float depth; 

uniform vec3 light_pos; 
uniform sampler2D shadowMap; 

void main() 
{ 
    //Bootstrap for render pass 
    currentPass(); 
} 

//Render passes 

subroutine(RenderPass) 
void Default() 
{ 
    //Config 
    vec3 lightColor = vec3(1.0, 1.0, 1.0); 
    float lightIntensity = 1.0; 
    vec3 diffuseColor = ex_color; 

    vec3 n = normalize(normal_cameraspace); 
    // Direction of the light (from the fragment to the light) 
    vec3 l = normalize(lightDir_cameraspace); 

    float cosTheta = clamp(dot(n,l), 0,1); 
    vec3 ambientLight = vec3(0.2, 0.2, 0.2); 
    vec3 lightSum = ambientLight; 
    float visibility = 1.0; 
    float shadowSample = texture(shadowMap, ShadowCoord.xy).z; 

    float bias = 0.005*tan(acos(cosTheta)); // cosTheta is dot(n,l), clamped between 0 and 1 
    bias = clamp(bias, 0,0.01); 

    if (shadowSample < ShadowCoord.z-bias){ 
     visibility = 0.3; 
    } 

    color = vec4(ambientLight + (diffuseColor*visibility*lightColor*lightIntensity/*cosTheta*/), 1.0); 

} 

subroutine(RenderPass) 
void Diffuse() 
{ 
    color = vec4(ex_color, 1.0); 
    depth = gl_FragCoord.z; 
} 

subroutine(RenderPass) 
void Depth() 
{ 
    depth = gl_FragCoord.z; 
} 

渲染时的窗口/帧缓冲区0。但是,试图呈现一个自定义设置帧缓冲的时候,我已经包裹也能正常工作为了方便的一类这样的:

private GameWindow _gameWindow; 
    private Texture _texture; 
    private int _framebufferId; 

    public Framebuffer(GameWindow window) 
    { 
     _gameWindow = window; 

     _framebufferId = GL.GenFramebuffer(); 
     GL.BindFramebuffer(FramebufferTarget.Framebuffer, _framebufferId); 

     _texture = new Texture(GL.GenTexture()); 
     GL.BindTexture(TextureTarget.Texture2D, _texture.Handle); 
     GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgba, window.Width, window.Height, 0, PixelFormat.Rgba, PixelType.UnsignedByte, (IntPtr)0); 

     GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int)TextureMagFilter.Nearest); 
     GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.Nearest); 

     GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapS, (int)TextureWrapMode.ClampToEdge); 
     GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapT, (int)TextureWrapMode.ClampToEdge); 

     GL.FramebufferTexture(FramebufferTarget.DrawFramebuffer, FramebufferAttachment.ColorAttachment0, _texture.Handle, 0); 

     GL.DrawBuffers(1, new DrawBuffersEnum[] { DrawBuffersEnum.ColorAttachment0 }); 

     GL.DrawBuffer(DrawBufferMode.None); 

     if (GL.CheckFramebufferStatus(FramebufferTarget.Framebuffer) != FramebufferErrorCode.FramebufferComplete) 
      throw new Exception("Framebuffer creation failed"); 
     GL.BindFramebuffer(FramebufferTarget.Framebuffer, 0); 
    } 

    public Texture Texture 
    { 
     get 
     { 
      return _texture; 
     } 
    } 

    public void BeginDraw() 
    { 
     GL.BindFramebuffer(FramebufferTarget.Framebuffer, _framebufferId); 
     GL.Viewport(0, 0, _gameWindow.Width, _gameWindow.Height); 
     GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit); 
    } 
    public void EndDraw() 
    { 
     GL.BindFramebuffer(FramebufferTarget.Framebuffer, 0); 
    } 

纹理最终的黑色。我误解了有关将颜色数据输入到纹理的内容?

+1

你为什么要调用'GL.DrawBuffer(DrawBufferMode.None);'?这会禁用对帧缓冲区的所有写入操作。 – BDL

+0

什么gfx卡和驱动程序? – Spektre

+0

BDL是正确的,注释掉这条线解决了这个问题。 –

您正确设置了帧缓冲区,并且正确设置了颜色附件和绘制缓冲区,但是却指定了与DrawBufferMode.None一起绘制的颜色缓冲区。

删除此行:

GL.DrawBuffer(DrawBufferMode.None); 

注意,由于帧缓冲器被绑定GL.DrawBuffer将指定颜色缓冲器,其被吸入,结合的帧缓冲液中。

接受的问题的答案What does GL_COLOR_ATTACHMENT do? 说:

默认为GL_COLOR_ATTACHMENT0。

因此,您可以完全跳过指定颜色缓冲区。

+0

这解决了这个问题!谢谢! –