如何调试 OpenTK?

How can I debug OpenTK?

尝试制作游戏,但纹理从未出现在屏幕上。

只对四边形使用颜色是可行的,但是当我尝试为四边形添加纹理时, 它只是显示为黑色方块。

基本片段着色器:

#version 300 es
precision highp float;

uniform sampler2D SS;

in vec4 Color;

in vec2 UVtrans;
out vec4 color;

void main()
{
    
        color = texture(SS, UVtrans); 
        
        //color = Color;
}

顶点着色器:

#version 300 es
layout(location = 0) in vec2 Vertex;
layout(location = 1) in vec2 UV;

uniform vec4 ColorIn;
uniform mat4 QuadMatrix;
out vec2 UVtrans;
out vec4 Color;

void main()
{
    gl_Position = QuadMatrix* vec4(Vertex, 0.0, 1.0);
    gl_Position.w = 1.0;
    UVtrans = UV;
    Color = ColorIn;
}

用于将纹理绑定到四边形的部分代码:

    public void Draw()
        {
            
            GL.UseProgram(VSID);
            Vector3 SS = new Vector3((new Vector(1, 1) * (Position - Offset)));
            SS.Z = Layer * 0.1f;
            Matrix4 Scale = Matrix4.CreateScale(Width, Height, 0);
            Matrix4 Translation = Matrix4.CreateTranslation(SS);
            
            Matrix4 ViewPoint = Matrix4.CreateOrthographic(Game.window.Width , Game.window.Height, -1,1000);
            Matrix4 Rotation = Matrix4.CreateRotationZ((float)Angle);

            Matrix4 Combined = Scale * Rotation * Translation * ViewPoint ;

            GL.ProgramUniformMatrix4(VSID, GL.GetUniformLocation(VSID, "QuadMatrix"), false, ref Combined);
            GL.ProgramUniform4(VSID, GL.GetUniformLocation(VSID, "ColorIn"), Color);
            GL.ProgramUniform1(VSID, GL.GetUniformLocation(VSID, "SS"), Texture);

            GL.ActiveTexture(TextureUnit.Texture0);
            GL.BindTexture(TextureTarget.Texture2D, 0);
            GL.BindVertexArray(QID);
            GL.DrawArrays(PrimitiveType.Triangles, 0, 6);
            GL.Enable(EnableCap.Texture2D);
            GL.Color3(Color);
            
        }

四边形的形状生成器:

    public static void CreateQuad()
        {
            QID = GL.GenVertexArray();
            GL.BindVertexArray(QID);

            int VID =GL.GenBuffer();
            float[] Verticles = 
            {
                -0.5f,  0.5f,
                 0.5f,  0.5f,
                -0.5f, -0.5f,
                -0.5f, -0.5f,
                 0.5f,  0.5f,
                 0.5f, -0.5f


            };
            GL.BindBuffer(BufferTarget.ArrayBuffer, VID);
            GL.BufferData(BufferTarget.ArrayBuffer, sizeof(float)*Verticles.Length, Verticles,BufferUsageHint.StaticDraw);

            GL.VertexAttribPointer(0, 2, VertexAttribPointerType.Float, false, 0, 0);
            GL.EnableVertexAttribArray(0);
            
            int UVID = GL.GenBuffer();
            float[] UVs =
            {
                0, 1,
                1, 1,
                0, 0,
                0, 0,
                1, 1,
                1, 0
            };
            GL.BindBuffer(BufferTarget.ArrayBuffer, UVID);
            GL.BufferData(BufferTarget.ArrayBuffer, sizeof(float) * UVs.Length, UVs, BufferUsageHint.StaticDraw);

            GL.VertexAttribPointer(1, 2, VertexAttribPointerType.Float, false, 0, 0);
            GL.EnableVertexAttribArray(1);
        }

纹理加载器:

    public static int LoadTexture(string path)
        {
            if (!File.Exists("Content/" + path))
            {
                throw new FileNotFoundException("file not found at Content/" + path);
            }

            int ID = GL.GenTexture();
            GL.BindTexture(TextureTarget.Texture2D, ID);

            Bitmap BMP = new Bitmap("Content/" + path);
            BitmapData data = BMP.LockBits(
                new Rectangle(0, 0, BMP.Width, BMP.Height),
                ImageLockMode.ReadOnly,System.Drawing.Imaging.PixelFormat.Format32bppArgb);

            //int ID = GL.GenTexture();
            //GL.BindTexture(TextureTarget.Texture2D, ID);

            GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgba, data.Width, data.Height,
                0, OpenTK.Graphics.OpenGL.PixelFormat.Bgra, PixelType.UnsignedByte, data.Scan0);

            BMP.UnlockBits(data);

            GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapS, (int)TextureWrapMode.Clamp);
            GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapT, (int)TextureWrapMode.Clamp);

            GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.Linear);
            GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int)TextureMagFilter.Linear);

            
            return ID;

        }

没有错误消息

必须设置为纹理采样器统一的值不是纹理对象(名称)值。它必须是纹理单元的索引。这意味着 TextureUnit.Texture0 为 0,TextureUnit.Texture1 为 1 ...

GL.ProgramUniform1(VSID, GL.GetUniformLocation(VSID, "SS"), Texture);

GL.ProgramUniform1(VSID, GL.GetUniformLocation(VSID, "SS"), 0);

但是在绘制对象之前,您必须将纹理对象绑定到设置为统一的纹理单元:

GL.ActiveTexture(TextureUnit.Texture0);
GL.BindTexture(TextureTarget.Texture2D, Texture);

GL.BindVertexArray(QID);
GL.DrawArrays(PrimitiveType.Triangles, 0, 6);

注意,BindTexture bind a named texture object to the active texture unit of the specified target. The active texture unit is set by ActiveTexture
texture sampler uniform和texture的绑定点是texture unit的索引。


分别在OpenGL ES 3.2中GLSL ES 3.2, the Binding point也可以在着色器代码中设置(类似于属性位置):

layout(binding = 0) uniform sampler2D SS;