Почему мой рендеринг треугольников не в OpenTK? - PullRequest
0 голосов
/ 04 ноября 2018

Я пытаюсь визуализировать треугольник в 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();
        }
    }
}
...