OpenGL 2d精灵不渲染(OpenTK)

时间:2013-09-08 02:03:54

标签: c# opengl graphics opentk

我一直在处理一个渲染,它已经为一个纹理工作正常,但不会渲染一秒。我似乎改变了一些东西,它停止渲染任何东西,但背景颜色。我不确定我改变了什么,我无法恢复原状。我尽量不要一次发布大量的代码到这里,但我不知道足够的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);

Matrix Setup:

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

1 个答案:

答案 0 :(得分:1)

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

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

varying在GLSL 330中也无效。您需要在顶点着色器中将f_texcoord声明为out,并在片段着色器中将in声明为您的程序正确链接

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