Search code examples
c#openglframebufferopentk

Why do I get a white square with a Framebuffer?


I am trying to add the use of Framebuffers to my OpenGL (OpenTK) project.

Into the initialization method I added at the very beginning: this.RenderBufferTex = GL.GenTexture();

GL.BindTexture(TextureTarget.Texture2D, this.BufferTex);
GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgba8, this.glc.Width, this.glc.Height, 0, PixelFormat.Rgba, PixelType.UnsignedByte, IntPtr.Zero);
GL.BindTexture(TextureTarget.Texture2D, 0);

GL.GenFramebuffers(1, out this.Buffer);
GL.BindFramebuffer(FramebufferTarget.FramebufferExt, this.Buffer);
GL.FramebufferTexture2D(FramebufferTarget.FramebufferExt, FramebufferAttachment.ColorAttachment0, TextureTarget.Texture2D, this.BufferTex, 0);

this.glc is the GLControl the scene runs in.

Before rendering I added

GL.BindFramebuffer(FramebufferTarget.Framebuffer, this.Buffer);

followed by GL.Clear() and all my old code for drawing the scene.

And then the method to display the rendered scene:

GL.BindFramebuffer(FramebufferTarget.Framebuffer, 0);
GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
GL.Disable(EnableCap.DepthTest);
GL.Disable(EnableCap.Lighting);
GL.Disable(EnableCap.LineSmooth);
GL.Disable(EnableCap.PointSmooth);
GL.MatrixMode(MatrixMode.Projection);
GL.PushMatrix();
GL.LoadIdentity();
GL.Ortho(0.0, (double)this.glc.Width, 0.0, (double)this.glc.Height, 0.0, 10000.0);
GL.MatrixMode(MatrixMode.Modelview);
GL.PushMatrix();
GL.LoadIdentity();

GL.BindTexture(TextureTarget.Texture2D, this.BufferTex);
GL.Begin(BeginMode.Quads);
GL.TexCoord2(0, 0);
GL.Vertex2(0, 0);
GL.TexCoord2(1, 0);
GL.Vertex2(this.glc.Height, 0);
GL.TexCoord2(1, 1);
GL.Vertex2(this.glc.Height, this.glc.Width);
GL.TexCoord2(0, 1);
GL.Vertex2(0, this.glc.Width);
GL.End();

GL.PopMatrix();
GL.MatrixMode(MatrixMode.Projection);
GL.PopMatrix();
GL.Enable(EnableCap.DepthTest);
this.glc.SwapBuffers();

The result I am getting from this is a white square filling the left part of my screen (so it appears to be square from my screen height).

What am I doing wrong?


Solution

  • You need to set the GL_MIN_FILTER and GL_MAG_FILTER of your texture to something reasonable like GL_LINEAR or GL_NEAREST, the default uses mipmaps and this makes the texture incomplete.