我在https://en.wikibooks.org/wiki/OpenGL_Programming/Post-Processing上遵循了C ++ OpenGL教程,并尝试使用OpenTK将其翻译为C#。
现在,我创建了一个Framebuffer对象和一个Shader对象
在我调用GL.VertexAttribPointer
之前,它一切正常(没有OpenGL错误,没有着色器编译/链接错误,没有程序错误)。有一个原生的例外:
Exception thrown at 0x68A9ED7C (nvoglv32.dll) in MyApplication.exe: 0xC0000005: Access violation reading location 0x0EB02DE8.
此外,在我的渲染代码中,我也使用GL.VertexPointer
和GL.DrawArrays
。这可能与此有关吗?
这是帧缓冲码:
int screenWidth = CraftGame.GetCraft().ClientRectangle.Width;
int screenHeight = CraftGame.GetCraft().ClientRectangle.Height;
GL.ActiveTexture(TextureUnit.Texture0);
fbo_texture = GL.GenTexture();
GL.BindTexture(TextureTarget.Texture2D, fbo_texture);
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int)TextureMagFilter.Linear);
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.Linear);
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapS, (int)TextureWrapMode.ClampToEdge);
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapT, (int)TextureWrapMode.ClampToEdge);
GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgba, screenWidth, screenHeight, 0, PixelFormat.Rgba, PixelType.UnsignedByte, IntPtr.Zero);
GL.BindTexture(TextureTarget.Texture2D, 0);
rbo_depth = GL.GenRenderbuffer();
GL.BindRenderbuffer(RenderbufferTarget.Renderbuffer, rbo_depth);
GL.RenderbufferStorage(RenderbufferTarget.Renderbuffer, RenderbufferStorage.DepthComponent16, screenWidth, screenHeight);
GL.BindRenderbuffer(RenderbufferTarget.Renderbuffer, 0);
GL.GenFramebuffers(1, out fbo);
GL.BindFramebuffer(FramebufferTarget.Framebuffer, fbo);
GL.FramebufferTexture2D(FramebufferTarget.Framebuffer, FramebufferAttachment.ColorAttachment0, TextureTarget.Texture2D, fbo_texture, 0);
GL.FramebufferRenderbuffer(FramebufferTarget.Framebuffer, FramebufferAttachment.DepthAttachment, RenderbufferTarget.Renderbuffer, rbo_depth);
GL.BindFramebuffer(FramebufferTarget.Framebuffer, 0);
float[] fbo_vertices =
{
-1, -1,
1, -1,
-1, 1,
1, 1,
};
GL.GenBuffers(1, out vbo_fbo_vertices);
GL.BindBuffer(BufferTarget.ArrayBuffer, vbo_fbo_vertices);
GL.BufferData(BufferTarget.ArrayBuffer, new IntPtr(fbo_vertices.Length * sizeof(float)), fbo_vertices, BufferUsageHint.StaticDraw);
GL.BindBuffer(BufferTarget.ArrayBuffer, 0);
这是着色器代码
public void Initialize()
{
int vs = CreateShader("postproc.v.glsl", ShaderType.VertexShader);
int fs = CreateShader("postproc.f.glsl", ShaderType.FragmentShader);
program_postproc = GL.CreateProgram();
GL.AttachShader(program_postproc, vs);
GL.AttachShader(program_postproc, fs);
GL.LinkProgram(program_postproc);
GL.ValidateProgram(program_postproc);
attribute_v_coord_postproc = GL.GetAttribLocation(program_postproc, "v_coord");
uniform_fbo_texture = GL.GetUniformLocation(program_postproc, "fbo_texture");
}
private int CreateShader(string name, ShaderType shaderType)
{
string shaderCode = File.ReadAllText(Path.Combine(Application.StartupPath, "shader", name));
int shaderId = GL.CreateShader(shaderType);
GL.ShaderSource(shaderId, shaderCode);
GL.CompileShader(shaderId);
if (!GetCompileStatus(shaderId))
{
MessageBox.Show("ERROR COMPILING SHADER\n" + shaderCode);
}
return shaderId;
}
渲染
// Rendering to framebuffer
Framebuffer.BindFramebuffer();
GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
GLStateManager.SetupViewport();
ApplyCamera();
// ...
Framebuffer.UnbindFramebuffer();
// Rendering framebuffer to screen
GL.ClearColor(0.0F, 0.0F, 0.0F, 1.0F);
GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
GL.UseProgram(Shader.program_postproc);
GL.BindTexture(TextureTarget.Texture2D, Framebuffer.fbo_texture);
GL.Uniform1(Shader.uniform_fbo_texture, 0);
// TODO: Why does it fail here
GL.EnableVertexAttribArray(Shader.attribute_v_coord_postproc);
GL.BindBuffer(BufferTarget.ArrayBuffer, Framebuffer.vbo_fbo_vertices);
GL.VertexAttribPointer(
Shader.attribute_v_coord_postproc,
2,
VertexAttribPointerType.Float,
false,
0,
0
);
GL.DrawArrays(PrimitiveType.TriangleStrip, 0, 4);
GL.DisableVertexAttribArray(Shader.attribute_v_coord_postproc);
SwapBuffers();
着色器的代码来自教程
我是使用framebuffers的新手。任何帮助将非常感谢。