Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/opengl/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# OpenTK基本三角形未按应有方式绘制_C#_Opengl_Opentk - Fatal编程技术网

C# OpenTK基本三角形未按应有方式绘制

C# OpenTK基本三角形未按应有方式绘制,c#,opengl,opentk,C#,Opengl,Opentk,我试图用指定的颜色和垂直度绘制三角形,但目前它似乎在为位置选择一些颜色编号,而不是它应该做的 using System; using System.Collections.Generic; using System.Linq; using System.Text; using OpenTK; using OpenTK.Graphics.OpenGL; using OpenTK.Graphics; namespace newTriangle { class Program {

我试图用指定的颜色和垂直度绘制三角形,但目前它似乎在为位置选择一些颜色编号,而不是它应该做的

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using OpenTK;
using OpenTK.Graphics.OpenGL;
using OpenTK.Graphics;

namespace newTriangle
{
    class Program
    {
        static void Main(string[] args)
        {
            MyWindow myWindow = new MyWindow();
            myWindow.Run();
        }
    }

    class MyWindow : GameWindow
    {
        private uint[] vertexBufferObjectIDs = new uint[2];
        private int vertexArrayID, vertexShaderID, fragmentShaderID, shaderProgramID;

        public MyWindow()
            : base(800, // Width
                600, // Height
                GraphicsMode.Default,
                "My OpenTK Window",
                GameWindowFlags.Default,
                DisplayDevice.Default,
                3, // major
                0, // minor
                GraphicsContextFlags.ForwardCompatible) { }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            GL.ClearColor(Color4.CornflowerBlue);

            GL.GenVertexArrays(1, out vertexArrayID);
            GL.BindVertexArray(vertexArrayID);

            ushort[] indices = new ushort[] { 0, 1, 2 };
            float[] vertices = new float[] {-1.0f, 1.0f, 0.0f, 1.0f, 0.0f,
                                            0.0f, -1.0f, 1.0f, 0.0f, 0.0f,
                                            1.0f, 1.0f, 0.0f, 0.0f, 1.0f };

            GL.GenBuffers(vertexBufferObjectIDs.Length, vertexBufferObjectIDs);
            GL.BindBuffer(BufferTarget.ArrayBuffer, vertexBufferObjectIDs[0]);
            GL.BufferData(BufferTarget.ArrayBuffer, (IntPtr)(vertices.Length * sizeof(float)), vertices, BufferUsageHint.StaticDraw);

            GL.BindBuffer(BufferTarget.ElementArrayBuffer, vertexBufferObjectIDs[1]);
            GL.BufferData(BufferTarget.ElementArrayBuffer, (IntPtr)(indices.Length * sizeof(ushort)), indices, BufferUsageHint.StaticDraw);

            GL.VertexAttribPointer(0, 2, VertexAttribPointerType.Float, true, 5 * sizeof(float), 0);
            GL.EnableVertexAttribArray(0);

            GL.VertexAttribPointer(1, 3, VertexAttribPointerType.Float, true, 5 * sizeof(float), 2 * sizeof(float));
            GL.EnableVertexAttribArray(1);

            vertexShaderID = GL.CreateShader(ShaderType.VertexShader);
            string vertShaderText =
                @"
                            #version 150

                            in vec3 position;
                            in vec3 colour;
                            out vec3 Colour;
                            void main()
                            {
                            Colour = colour;
                            gl_Position = vec4(position, 1) ;
                            }";

            GL.ShaderSource(vertexShaderID, vertShaderText);
            GL.CompileShader(vertexShaderID);

            fragmentShaderID = GL.CreateShader(ShaderType.FragmentShader);
            string fragShaderText =
                @"
                            #version 150
                            in vec3 Colour;
                            out vec4 outputF;
                            void main() 
                            {
                            outputF = vec4(Colour, 1.0);
                            }";
            GL.ShaderSource(fragmentShaderID, fragShaderText);
            GL.CompileShader(fragmentShaderID);

            shaderProgramID = GL.CreateProgram();
            GL.AttachShader(shaderProgramID, fragmentShaderID);
            GL.AttachShader(shaderProgramID, vertexShaderID);
            GL.LinkProgram(shaderProgramID);
            GL.UseProgram(shaderProgramID);
        }

        protected override void OnUnload(EventArgs e)
        {
            base.OnUnload(e);
            GL.DeleteBuffers(vertexBufferObjectIDs.Length, vertexBufferObjectIDs);
            GL.DeleteVertexArrays(1, ref vertexArrayID);

            GL.UseProgram(0); GL.DetachShader(shaderProgramID, vertexShaderID);
            GL.DetachShader(shaderProgramID, fragmentShaderID);
            GL.DeleteShader(fragmentShaderID);
            GL.DeleteShader(vertexShaderID);
            GL.DeleteProgram(shaderProgramID);
        }

        protected override void OnRenderFrame(FrameEventArgs e)
        {
            base.OnRenderFrame(e);

            GL.Clear(ClearBufferMask.ColorBufferBit);

            GL.DrawElements(BeginMode.Triangles, 3, DrawElementsType.UnsignedShort, IntPtr.Zero);

            this.SwapBuffers();
        }
    }
}
有人能看出我的错误吗

vec4(position, 1)
在GLSL中,整数不会自动进行类型升级

有点奇怪,因为你在片段着色器中找到了它

试试这个:

vec4(position, 1.0)

您没有链接属性的位置。您可以通过使用对
BindAttribLocation的适当调用来修复它​,或将
布局
限定符与
位置
s一起使用。另外,
position
是一个
vec3
,但您只给它两个浮点数

使用
layout
限定符是一个简单的解决方法:

layout(location = 0) in vec2 position;
layout(location = 1) in vec3 colour;

这让我看到了这样一幅画面:看起来这可能就是你的想法。

你为什么不检查着色器编译和程序链接日志?