OpenTK 着色器三角形顶点

问题描述 投票:0回答:1

我正在尝试使用 C++ 和 OpenTK 库编写一个带有彩色顶点的三角形。这就是我对我想要实现的目标的设想:

但是当我编译程序时,我得到这个,三角形是白色的:

这是我的代码:

    using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using OpenTK.Graphics.OpenGL;
using OpenTK;
using OpenTK.Mathematics;
using OpenTK.Windowing.Common;
using OpenTK.Windowing.Desktop;

namespace BasicOpenTk
{
    public class Game : GameWindow
    {
        private int vertexBufferHandle;
        private int shaderProgramHandle;
        private int vertexArrayHandle;

        public Game(int width = 1280, int height = 768, string title = "Game1")
            : base(
                GameWindowSettings.Default, 
                new NativeWindowSettings()
                {
                    Title = title,
                    ClientSize = new Vector2i(width, height),
                    WindowBorder = WindowBorder.Fixed,
                    StartVisible = false,
                    StartFocused = true,
                    API = ContextAPI.OpenGL,
                    Profile = ContextProfile.Core,
                    APIVersion = new Version(3,3)
                })
        {
            this.CenterWindow(new Vector2i(1280,768));
        }
    protected override void OnResize(ResizeEventArgs e)
    {
        GL.Viewport(0,0,e.Width, e.Height);
        base.OnResize(e);
    }

    protected override void OnLoad(){


        this.IsVisible = true;

        GL.ClearColor(new Color4(0.3f, 0.4f, 0.5f, 1.0f));  

        float[] vertices = new float[]
        {
            0.0f, 0.5f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f,
            0.4f, -0.5f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f,
            -0.4f, -0.5f, 0.0f, 0.0f, 0.0f, 1.0f, 1.0f
        };

        this.vertexBufferHandle = GL.GenBuffer();
        GL.BindBuffer(BufferTarget.ArrayBuffer, this.vertexBufferHandle);
        GL.BufferData(BufferTarget.ArrayBuffer, vertices.Length * sizeof(float), vertices, BufferUsageHint.StaticDraw);
        GL.BindBuffer(BufferTarget.ArrayBuffer, 0);

        this.vertexArrayHandle = GL.GenVertexArray();
        GL.BindVertexArray(this.vertexArrayHandle);
        GL.BindBuffer(BufferTarget.ArrayBuffer, this.vertexBufferHandle);
        GL.VertexAttribPointer(0, 3, VertexAttribPointerType.Float, false, 7 * sizeof(float), 0);
        GL.VertexAttribPointer(1, 4, VertexAttribPointerType.Float, false, 7 * sizeof(float), 3 * sizeof(float));
        GL.EnableVertexAttribArray(0);
        GL.EnableVertexAttribArray(1);
        GL.BindVertexArray(0);

        
        string vertexShaderCode =
        @"
        #version 330 core

            layout(location = 0) in vec3 aPosition;
            layout(location = 1) in vec4 aColor;
            out vec4 vColor;                

            void main(void)
            {
                gl_Position = vec4(aPosition, 1.0);
                vColor= aColor;
            }
        ";
        string pixelShaderCode =
        @"
        #version 330
        in vec4 vColor;
        out vec4 outputColor;

        void main()
        {
            outputColor = vColor;
        }
        ";


        int vertexShaderHandle = GL.CreateShader(ShaderType.FragmentShader);
        GL.ShaderSource(vertexShaderHandle, vertexShaderCode);
        GL.CompileShader(vertexShaderHandle);

        int pixelShaderHandle = GL.CreateShader(ShaderType.FragmentShader);
        GL.ShaderSource(pixelShaderHandle, pixelShaderCode);
        GL.CompileShader(pixelShaderHandle);

        this.shaderProgramHandle = GL.CreateProgram();

        // Después de compilar el shader
        int status;
        GL.GetShader(vertexShaderHandle, ShaderParameter.CompileStatus, out status);
        if (status != 1)
        {
            string infoLog = GL.GetShaderInfoLog(vertexShaderHandle);
            Console.WriteLine("Error compiling vertex shader: " + infoLog);
        }

        // Después de compilar el shader de píxeles
        GL.GetShader(pixelShaderHandle, ShaderParameter.CompileStatus, out status);
        if (status != 1)
        {
            string infoLog = GL.GetShaderInfoLog(pixelShaderHandle);
            Console.WriteLine("Error compiling pixel shader: " + infoLog);
        }


        GL.AttachShader(this.shaderProgramHandle, vertexShaderHandle);
        GL.AttachShader(this.shaderProgramHandle, pixelShaderHandle);

        GL.LinkProgram(this.shaderProgramHandle);

        GL.DetachShader(this.shaderProgramHandle, vertexShaderHandle);
        GL.DetachShader(this.shaderProgramHandle, pixelShaderHandle);

        GL.DeleteShader(vertexShaderHandle);
        GL.DeleteShader(pixelShaderHandle);

        base.OnLoad();
    }

    protected override void OnUnload()
    {

        GL.BindVertexArray(0);
        GL.DeleteVertexArray(this.vertexArrayHandle);

        GL.BindBuffer(BufferTarget.ArrayBuffer, 0);
        GL.DeleteBuffer(this.vertexBufferHandle);

        GL.UseProgram(0);
        GL.DeleteProgram(this.shaderProgramHandle);

        base.OnUnload();
    }

    protected override void OnUpdateFrame(FrameEventArgs args)
    {
        base.OnUpdateFrame(args);
    }

    protected override void OnRenderFrame(FrameEventArgs args)
    {
        GL.Clear(ClearBufferMask.ColorBufferBit);

        GL.UseProgram(this.shaderProgramHandle);
        GL.BindVertexArray(this.vertexArrayHandle);
        GL.DrawArrays(PrimitiveType.TriangleFan, 0,3);

        this.Context.SwapBuffers();
        base.OnRenderFrame(args);
    }
}
}

这是我遇到的错误:

Error compiling vertex shader: ERROR: 0:10: 'gl_Position' : undeclared identifier 
ERROR: 0:10: 'assign' :  cannot convert from '4-component vector of highp float' to 'highp float'

我猜我的着色器出了问题,但我不知道它是什么。我尝试这样做:

string vertexShaderCode =
            @"
            #version 330 core

                layout(location = 0) in vec3 aPosition;
                layout(location = 1) in vec4 aColor;
                out vec4 vColor;                
                out vec4 gl_Position ;

                void main(void)
                {
                    gl_Position = vec4(aPosition, 1.0);
                    vColor= aColor;
                }
            ";

但是没有成功。 请帮助我!!!!

c# opengl opentk
1个回答
0
投票

您正在将顶点编译为片段着色器:

int vertexShaderHandle = GL.CreateShader(ShaderType.FragmentShader);
GL.ShaderSource(vertexShaderHandle, vertexShaderCode);
GL.CompileShader(vertexShaderHandle);
© www.soinside.com 2019 - 2024. All rights reserved.