C# 无法渲染到绑定到帧缓冲区的纹理
我正在尝试设置渲染管道,因此,我正在尝试将渲染设置为纹理,以便稍后可以对其进行采样以进行模糊等操作 我有一个顶点着色器,如下所示:C# 无法渲染到绑定到帧缓冲区的纹理,c#,opengl,framebuffer,opentk,C#,Opengl,Framebuffer,Opentk,我正在尝试设置渲染管道,因此,我正在尝试将渲染设置为纹理,以便稍后可以对其进行采样以进行模糊等操作 我有一个顶点着色器,如下所示: #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_c
#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;
}
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);
}
纹理最终变成黑色。我是否误解了将颜色数据导入纹理的某些内容?您正确设置了帧缓冲区,并且正确设置了颜色附件和绘制缓冲区,但指定了使用
DrawBufferMode绘制的颜色缓冲区。无
删除此行:
GL.DrawBuffer(DrawBufferMode.None);
注意,由于帧缓冲区是绑定的GL。DrawBuffer
将指定绑定帧缓冲区的颜色缓冲区
对这个问题的公认答案是:
默认值为GL_COLOR_ATTACHMENT0
因此,您可以完全跳过以指定颜色缓冲区。为什么要调用GL.DrawBuffer(DrawBufferMode.None)代码>?这将禁用对帧缓冲区的所有写入操作。什么gfx卡和驱动程序?BDL是正确的,注释掉行解决了问题。