0

我一直在进行渲染,它对一个纹理工作正常,但不会渲染第二个。我似乎改变了一些东西,它停止渲染除了背景颜色之外的任何东西。我不确定我改变了什么,我无法恢复原来的样子。我尽量不要一次在这里发布大量代码,但我不知道足够的 OpenGL 来隔离问题。如果您能提供任何帮助或提示,我将不胜感激!

我的猜测是它要么来自我绑定坐标或着色器的方式。

以下是代码:

着色器:

       string vertexShaderSource = @"
                        #version 330

                        layout (location = 0) in vec3 Position;

                        uniform mat4 projectionmatrix;
                        uniform mat4 ModelMatrix;
                        uniform mat4 ViewMatrix;

                        attribute vec2 texcoord;
                        varying vec2 f_texcoord;

                        uniform vec2 pos;

                        void main()
                        {
                            f_texcoord = texcoord;
                            gl_Position = projectionmatrix * vec4(Position, 1);

                            //gl_Position = projectionmatrix * vec4(Position.xyz, 1.0);
                        }
                    ";

    string fragmentShaderSource = @"
                    #version 330

                    out vec4 FragColor;

                    varying vec2 f_texcoord;
                    uniform sampler2D mytexture;

                    void main()
                    {
                        FragColor = texture2D(mytexture, f_texcoord);
                        //FragColor = Vec4(0,0,0, 1);
                    }";

顶点:

    Vector2[] g_vertex_buffer_data ={
            new Vector2(-1.0f, 1.0f),
            new Vector2(1.0f, 1.0f),
            new Vector2(1.0f, -1.0f),
            new Vector2(-1.0f, -1.0f)
    };


    Vector2[] g_texture_coords = {
            new Vector2(0.0f, 0.0f),
            new Vector2(1.0f, 0.0f),
            new Vector2(1.0f, -1.0f),
            new Vector2(0.0f, -1.0f)
    };

着色器设置:

        shaderProgramHandle = GL.CreateProgram();

        vertexShaderHandle = GL.CreateShader(ShaderType.VertexShader);
        fragmentShaderHandle = GL.CreateShader(ShaderType.FragmentShader);

        GL.ShaderSource(vertexShaderHandle, vertexShaderSource);
        GL.ShaderSource(fragmentShaderHandle, fragmentShaderSource);

        GL.CompileShader(vertexShaderHandle);
        GL.CompileShader(fragmentShaderHandle);

        GL.AttachShader(shaderProgramHandle, vertexShaderHandle);
        GL.AttachShader(shaderProgramHandle, fragmentShaderHandle);

        GL.LinkProgram(shaderProgramHandle);
        GL.UseProgram(shaderProgramHandle);

基本设置和绑定:

GL.ClearColor(Color4.Red);

        //GL.LoadMatrix(ref projectionMatrix);

        GL.GenBuffers(2, out vertexbuffer);

        GL.BindBuffer(BufferTarget.ArrayBuffer, vertexbuffer);

        GL.BufferData<Vector2>(BufferTarget.ArrayBuffer,
                               new IntPtr(g_vertex_buffer_data.Length * Vector2.SizeInBytes),
                               g_vertex_buffer_data, BufferUsageHint.StaticDraw);

        //Shader Setup
        CreateShaders();

        Matrix4 projectionMatrix = Matrix4.CreateOrthographic(control.Width, control.Height, -1, 1);

        vertexShaderProjectionHandle = GL.GetUniformLocation(shaderProgramHandle, "projectionmatrix");
        GL.UniformMatrix4(vertexShaderProjectionHandle, false, ref projectionMatrix);

        GL.EnableVertexAttribArray(0);
        GL.BindBuffer(BufferTarget.ArrayBuffer, vertexbuffer);
        GL.VertexAttribPointer(0, 2, VertexAttribPointerType.Float, false, 0, 0);

加载和绑定纹理:

        GL.BlendFunc(BlendingFactorSrc.SrcAlpha, BlendingFactorDest.OneMinusSrcAlpha);
        GL.Enable(EnableCap.Blend);

        GL.ActiveTexture(TextureUnit.Texture0 + texture.textureID);
        GL.BindTexture(TextureTarget.Texture2D, texture.textureID);

        textureHandle = GL.GetAttribLocation(shaderProgramHandle, "texcoord");

        GL.GenBuffers(1, out textureBufferHandle);
        GL.BindBuffer(BufferTarget.ArrayBuffer, textureBufferHandle);
        GL.BufferData<Vector2>(BufferTarget.ArrayBuffer, new IntPtr(Vector2.SizeInBytes * 4), g_texture_coords, BufferUsageHint.StaticDraw);

矩阵设置:

        //rotation += MathHelper.DegreesToRadians(1);

        float displayRatio = ((float)control.Height / (float)control.Width);

        Matrix4 ViewMatrix = Matrix4.Identity;

        int ViewMatrixHandle = GL.GetUniformLocation(shaderProgramHandle, "ViewMatrix");
        GL.UniformMatrix4(ViewMatrixHandle, true, ref ViewMatrix);

        Matrix4 ModelMatrix = Matrix4.Identity;

        int modelMatrixHandle = GL.GetUniformLocation(shaderProgramHandle, "ModelMatrix");
        GL.UniformMatrix4(modelMatrixHandle, true, ref ModelMatrix);


        int posHandle = GL.GetUniformLocation(shaderProgramHandle, "pos");
        GL.Uniform2(posHandle, ref offset);

渲染

        GL.Viewport(0, 0, control.Width, control.Height);

        //GL.Enable(EnableCap.Texture2D);

        GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);

        GL.BindVertexArray(0);

        GL.EnableVertexAttribArray(textureHandle);
        GL.BindBuffer(BufferTarget.ArrayBuffer, textureBufferHandle);

        GL.VertexAttribPointer(textureHandle, 2, VertexAttribPointerType.Float, false, 0, 0);

        GL.DrawArrays(BeginMode.Quads, 0, 4);

        GL.Flush();
        control.SwapBuffers();
4

1 回答 1

1

您正在使用旧attribute限定符texcoord在顶点着色器中声明。这在 GLSL 330 中无效,我怀疑如果您在编译/链接 GLSL 程序时阅读程序/着色器信息日志,它会在日志中包含此信息。

要更正此问题,请替换attribute vec2 texcoordin vec2 texcoord。然后,当您查询属性位置时,您应该得到一个有效的位置,这是正确设置顶点属性指针所必需的。

varying在 GLSL 330 中也是无效的。您需要在顶点着色器和片段着色器中声明f_texcoord为,以便您的程序正确链接。outin

您的代码清单中根本没有错误检测代码。您应该阅读和的手册页glValidateProgram (...),因为我很确定如果您阅读 GLSL 编译器的输出日志,它会告诉您确切的问题。glGetProgramInfoLog (...)glGetShaderInfoLog (...)

于 2013-09-08T02:24:29.700 回答