简体   繁体   中英

Fragmentshader and framebuffer solid colour

Dear Stackoverflowers,

Lately I have been making my first baby steps in the world of shaders, using GLSL and openGL in C# with OpenTK, and I've stumbled into some problems which I seem to be unable to solve.

I started off with one shader program and some fast experimental shaders which worked fine, giving me the following result (just a green and red light circling a triangle):

Then I wanted to add some sort of glow to this and I heard you had to do this through applying a fragment shader to a texture. So I set out and rendered my scene to a framebuffer. I then bound the framebuffer texture and drew a quad. This showed me the same scene I had before, which meant it worked. Then to use the new fragmentshader I replace my first shader program with my new program after I wrote to the buffer and before writing the quad. Now the problem is that whatever I do the screen only gives one solid color, not doing any manipulations like I would like.

This is my drawing code:

    protected override void OnRenderFrame(FrameEventArgs e)
    {
        GL.Ext.BindFramebuffer(FramebufferTarget.FramebufferExt, fboID);
        GL.UseProgram(shaderProgramHandle);
        GL.PushAttrib(AttribMask.ViewportBit);
        {
            GL.Viewport(0, 0, 600, 600);

            // clear the screen in green, to make it very obvious what the clear affected. only the FBO, not the real framebuffer
            GL.ClearColor(0.0f, 0.0f, 0.0f, 1.0f);
            GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit | ClearBufferMask.StencilBufferBit);

            GL.PushAttrib(AttribMask.ColorBufferBit);

            // make sure no lingering textures are bound to draw vertices clearly.
            GL.BindTexture(TextureTarget.Texture2D, 0);

            GL.EnableVertexAttribArray(0);
            GL.BindBuffer(BufferTarget.ArrayBuffer, vbo);
            GL.VertexAttribPointer(0, 3, VertexAttribPointerType.Float, false, 0, 0);
            GL.DrawArrays(BeginMode.Triangles, 0, 3);

            GL.DisableVertexAttribArray(0);

            GL.Begin(BeginMode.Points);
            GL.Vertex3(lmp);
            GL.Vertex3(lmp2);
            GL.End();

            GL.PopAttrib();
        }
        GL.PopAttrib();

        GL.Ext.BindFramebuffer(FramebufferTarget.FramebufferExt, 0);

        angle += 0.01f;
        angle2 -= 0.017f;
        lmp = new Vector3((float)(2f * Math.Cos(angle)), (float)(Math.Sin(angle) * 2f), 0f);
        lmp2 = new Vector3((float)(2f * Math.Cos(angle2)), (float)(Math.Sin(angle2) * 2f), 0f);
        GL.Uniform3(uniformLmp, lmp);
        GL.Uniform3(uniformLmp2, lmp2);

        GL.UseProgram(secondProgram);
        GL.Enable(EnableCap.Texture2D);
        GL.ActiveTexture(TextureUnit.Texture0);
        GL.BindTexture(TextureTarget.Texture2D, fboTex);
        GL.Uniform1(GL.GetUniformLocation(secondProgram, "tex"), fboTex);

        GL.Clear(ClearBufferMask.ColorBufferBit);

        GL.Begin(BeginMode.TriangleStrip);

        GL.TexCoord2(.0f, .0f);
        GL.Vertex3(-1f, -1f, 0f);

        GL.TexCoord2(1.0f, .0f);
        GL.Vertex3(1f, -1f, 0f);

        GL.TexCoord2(.0f, 1.0f);
        GL.Vertex3(-1f, 1f, 0f);

        GL.TexCoord2(1.0f, 1.0f);
        GL.Vertex3(1f, 1f, 0f);

        GL.End();
        GL.Disable(EnableCap.Texture2D);
        GL.UseProgram(0);

        SwapBuffers();
    }

And these are my shaders:

//vertex shader
#version 330

layout (location = 0) in vec3 Position;
varying vec2 texCoord;

void main()
{
    gl_Position = vec4(Position.x, Position.y, Position.z, 1.0);
    texCoord = gl_TexCoord[0].st;
}

//fragment shader
#version 330

uniform sampler2D tex;
out vec4 FragColor;
varying vec2 texCoord;

void main()
{
    vec4 color = texture2D(tex, texCoord);
    color.r += 0.5f;
    FragColor = color;
}

This results in a solid red color, instead of giving the existing image a red hue.

Can anybody spot the problem here?

It turns out that I forgot

GL.EnableClientState(ArrayCap.TextureCoordArray);

This meant that my UV coords were not passed to the shader, thus making it sample from just (0,0), explaining this wierd behaviour.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM