为什么我的三角形未在OpenTK中渲染?

时间:2018-11-04 07:56:35

标签: c# opengl opentk

我正在尝试在OpenTK(C#)中渲染一个三角形,但是由于某种原因,它没有出现。我已经查看了我正在关注的视频的源代码,但是,即使它匹配了,它仍然无法渲染。接下来的几个代码块是用于绘制所有内容并将其放在一起的主文件,其中 Shapes.cs 是主程序。但是,当我运行该程序时,它没有显示任何形状,但是当我设置背景色时,它就可以正常工作。我究竟做错了什么? 执行程序时没有错误。

Vertex.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using OpenTK;

namespace The_Joker_Engine
{
    public class Vertex
    {
        public const int Size = 2;

        private Vector2 position;
        public Vector2 Position
        {
            get { return position; }
            set { position = value; }
        }

        public Vertex(Vector2 position)
        {
            this.position = position;
        }

        public Vertex(float x, float y) : this(new Vector2(x, y)) { }

        public static float[] Process(Vertex[] vertices)
        {
            int count = 0;

            float[] data = new float[vertices.Length * Size];
            for (int i = 0; i < vertices.Length; i++)
            {
                data[count] = vertices[i].Position.X;
                data[count + 1] = vertices[i].Position.Y;

                count += 2;
            }

            return data;
        }
    }
}

Mesh2D.cs:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using OpenTK;
    using OpenTK.Graphics.OpenGL4;

    namespace The_Joker_Engine
    {
        public class Mesh2D
        {
            private int vboID;
            private int size;

            public Mesh2D(Vertex[] vertices)
            {
                vboID = GL.GenBuffer();
                size = vertices.Length;

                float[] data = Vertex.Process(vertices);

                GL.BindBuffer(BufferTarget.ArrayBuffer, vboID);
                GL.BufferData(BufferTarget.ArrayBuffer, (IntPtr)(data.Length * sizeof(float)), data, BufferUsageHint.StaticDraw);
                GL.BindBuffer(BufferTarget.ArrayBuffer, 0);
            }

            public void Draw()
            {
                GL.EnableVertexAttribArray(0);

                GL.BindBuffer(BufferTarget.ArrayBuffer, vboID);
                GL.VertexAttribPointer(0, Vertex.Size, VertexAttribPointerType.Float, false, Vertex.Size * 4, 0);
                GL.DrawArrays(PrimitiveType.Triangles, 0, size);
                GL.BindBuffer(BufferTarget.ArrayBuffer, 0);

                GL.DisableVertexAttribArray(0);
            }
        }
    }

Shapes.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Drawing;

namespace The_Joker_Engine
{
    class ShapesAndShaders : Game
    {
        public ShapesAndShaders(int width, int height, string title) : base(width, height, title) { }

        private Mesh2D mesh2d;

        // This void is played when the program starts
        protected override void Initialize()
        {   
            Vertex[] vertices = new Vertex[]
            {
                new Vertex(-1f, -1f),
                new Vertex(1f, -1f),
                new Vertex(0f, 1f),
            };

            mesh2d = new Mesh2D(vertices);
        }

        // This void is played for every frame.
        protected override void Update()
        {
            base.Update();
        }

        // This void is used to render sprites and colours.
        protected override void Render()
        {
            mesh2d.Draw();

            RenderingSystem.ClearScreen();
        }

        // This is the Shutdown function, and it runs upon the program shutting down.
        protected override void Shutdown()
        {
            base.Shutdown();
        }
    }
}

0 个答案:

没有答案