OpenTK / OpenGL:无法在 VAO 上获得 运行 的着色器

OpenTK / OpenGL : Unable to Get Shaders to Run on VAO

我正在尝试 运行 我的网格上的着色器。现在它都是二维的,而且非常简单。我认为这只是我缺乏知识的问题,但在 google 4 小时并阅读了 opentk / opengl 教程后,我仍然卡住了。这是我的默认着色器代码:

using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using BuildMe.Core;
using OpenTK.Graphics.OpenGL;

namespace BuildMe.Render.Shaders
{
    abstract class ShaderProgram
    {

        private static readonly string Loc = "Resources/Shaders/";

        // Shader ID
        private readonly int PID;

        // Vertex Shader ID
        private readonly int VSID;

        // Fragment Shader ID
        private readonly int FSID;

        /// <summary>
        /// 
        /// </summary>
        /// <param name="FileName">Prefixed with the correct directories.</param>
        public ShaderProgram(String FileName)
        {
            // Compiling Vertex Shader
            VSID = CreateVertexShader(Loc + "Vertex/" + FileName + ".vert");

            // Compiling Fragment Shader
            FSID = CreateFragShader(Loc + "Fragment/" + FileName + ".frag");

            // Compiling Shader Program
            PID = GL.CreateProgram();
            GL.AttachShader(PID, VSID);
            GL.AttachShader(PID, FSID);
            BindAttributes();
            GL.LinkProgram(PID);
            GL.ValidateProgram(PID);

            if (GL.GetError() != ErrorCode.NoError)
                Console.WriteLine("Error Vaidating Program");

            RenderLoop.Shaders.Add(this);
        }

        public virtual void BindAttributes()
        {

        }

        private int CreateVertexShader(String Location)
        {
            int vsid = GL.CreateShader(ShaderType.VertexShader);

            GL.ShaderSource(vsid, File.ReadAllText(Location));
            GL.CompileShader(vsid);
            GL.GetShader(vsid, ShaderParameter.CompileStatus, out int status_code);
            if (status_code != 1)
                Console.WriteLine("Status Code : " + status_code);
            return (vsid);
        }

        private int CreateFragShader(String Location)
        {
            int fsid = GL.CreateShader(ShaderType.VertexShader);

            GL.ShaderSource(fsid, File.ReadAllText(Location));
            GL.CompileShader(fsid);
            GL.GetShader(fsid, ShaderParameter.CompileStatus, out int status_code);
            if (status_code != 1)
                Console.WriteLine("Status Code : " + status_code);
            return (fsid);
        }

        public virtual void BindAttribute(int attribute, String VariableName)
        {
            GL.BindAttribLocation(PID, attribute, VariableName);
        }

        public void UseShader()
        {
            GL.UseProgram(PID);
        }

        public void StopShader()
        {
            GL.UseProgram(0);
        }

        public void CleanUp()
        {
            // Cleaning Up
            GL.DetachShader(PID, VSID);
            GL.DetachShader(PID, FSID);
            GL.DeleteShader(VSID);
            GL.DeleteShader(FSID);
        }

    }
}

这是我用于我的网格的子 class :

using System;
using System.Collections.Generic;
using System.Text;

namespace BuildMe.Render.Shaders
{
    class StaticShader : ShaderProgram
    {

        public StaticShader() : base("2d")
        {

        }

        public override void BindAttributes()
        {
            BindAttribute(0, "position");
        }

    }
}

这是我的两个着色器: 顶点:

#version 400 core

in vec3 position;

out vec3 colour;

void main(void)
{
    gl_Position = vec4(position, 1.0);
    colour = vec3(position.x + 0.5, 1, position.y + 0.5);

}

片段:

#version 400 core

in vec3 colour;

out vec4 out_Color;

void main(void)
{
    out_Color = vec4(colour, 1.0);
}

这是我使用着色器的地方:

        private void Render(object sender, FrameEventArgs e)
        {
            ss.UseShader();
            foreach (Mesh mesh in SceneMeshes)
                mesh.Render();
            ss.StopShader();
        }

我在编译着色器或着色器程序时没有遇到任何错误,所以我有点不知所措。正如我之前所说,我怀疑这只是我由于缺乏经验而遗漏或搞砸的东西。任何帮助表示赞赏。

您创建了 2 个顶点着色器,但没有创建片段着色器:

因为在CreateFragShader中,使用了着色器类型ShaderType.VertexShader

private int CreateFragShader(String Location)
{
  int fsid = GL.CreateShader(ShaderType.VertexShader);

使用ShaderType.FragmentShader解决问题:

private int CreateFragShader(String Location)
{
    int fsid = GL.CreateShader(ShaderType.FragmentShader);