OpenTK基本三角形不应该绘制

时间:2013-03-04 18:53:26

标签: 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
    {
        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();
        }
    }
}

任何人都可以看到我的错误吗?

2 个答案:

答案 0 :(得分:0)

vec4(position, 1)

在GLSL中,整数不会自动进行类型提升。

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

试试这个:

vec4(position, 1.0)

答案 1 :(得分:0)

您没有链接属性的位置。您可以使用BindAttribLocation​的相应调用来修复此问题,也可以将layout限定符与location一起使用。此外,positionvec3,但您只给它2个花车。

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

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

这给了我这张照片:http://i.imgur.com/H9FEXZ0.png看起来很像你的想法。