繁体   English   中英

Fragmentshader和帧缓冲区纯色

[英]Fragmentshader and framebuffer solid colour

亲爱的Stackoverflowers,

最近,我在着色器领域迈出了第一步,在带有OpenTK的C#中使用GLSL和openGL,但我偶然发现了一些我似乎无法解决的问题。

我从一个着色器程序和一些运行良好的快速实验着色器开始,这给了我以下结果(只是绿色和红色的光围绕一个三角形):

然后,我想为此添加某种发光效果,听说您必须通过将片段着色器应用于纹理来完成此操作。 因此,我开始将场景渲染到帧缓冲区中。 然后,我绑定帧缓冲区纹理并绘制一个四边形。 这向我展示了以前的场景,这意味着它可以正常工作。 然后,要使用新的fragmenthader,在写入缓冲区之后并在写入四边形之前,请用新程序替换第一个着色器程序。 现在的问题是,无论我做什么,屏幕只能给出一种纯色,而没有像我想要的那样进行任何操作。

这是我的绘图代码:

    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();
    }

这些是我的着色器:

//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;
}

这样会产生纯红色,而不是使现有图像具有红色调。

有人可以在这里发现问题吗?

原来我忘了

GL.EnableClientState(ArrayCap.TextureCoordArray);

这意味着我的UV坐标未传递到着色器,因此仅从(0,0)进行采样,从而解释了这种奇怪的行为。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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