0

I'm working on a little OpenTK based 2D graphics library and I'm trying to apply a shader to a Surface object:

public void ApplyTo(Surface surface)
    {
        using (Surface pong = new Surface())
        {
            pong.Create(surface.Width, surface.Height);
            pong.Clear(0, 0, 0, 0);

            GL.Viewport(0, 0, surface.Width, surface.Height);
            GL.LoadIdentity();
            GL.Ortho(0, 1.0, 1.0, 0.0, 0.0, 4.0);

            GL.UseProgram(0);
            surface.BindTexture();
            pong.BindFramebuffer();


            GL.Begin(PrimitiveType.Quads);
            GL.TexCoord2(0.0f, 1.0f);
            GL.Vertex3(0, 0, 0);
            GL.TexCoord2(0.0f, 0.0f);
            GL.Vertex3(0, 1, 0);
            GL.TexCoord2(1.0f, 0.0f);
            GL.Vertex3(1, 1, 0);
            GL.TexCoord2(1.0f, 1.0f);
            GL.Vertex3(1, 0, 0);
            GL.End();

            Use(); // calls GL.UseProgram()
            pong.BindTexture();
            surface.BindFramebuffer();

            GL.Begin(PrimitiveType.Quads);
            GL.TexCoord2(0.0f, 1.0f);
            GL.Vertex3(0, 0, 0);
            GL.TexCoord2(0.0f, 0.0f);
            GL.Vertex3(0, 1, 0);
            GL.TexCoord2(1.0f, 0.0f);
            GL.Vertex3(1, 1, 0);
            GL.TexCoord2(1.0f, 1.0f);
            GL.Vertex3(1, 0, 0);
            GL.End();

            GL.BindTexture(TextureTarget.Texture2D, 0);
            GL.BindFramebuffer(FramebufferTarget.Framebuffer, 0);
        }
    }

This somehow clears the surface instead of applying the shader. My suspicion is that I'm doing something wrong with the viewport / projection, but what? I hope the provided code is sufficient.

pixartist
  • 1,137
  • 2
  • 18
  • 40

1 Answers1

0

For full screen rendering on a quad, I use that projection:

OpenTK.Matrix4 ortho = OpenTK.Matrix4.CreateOrthographicOffCenter(-1, 1, -1, 1, 1, -1);

Theres a division by 0 somewhere when your vertices are exactly on the near plane, you may also move a bit further on the z axis.

j-p
  • 1,622
  • 10
  • 18