OpenTK - VertexBufferObject不会绘制任何内容

时间:2015-05-05 20:16:38

标签: c# vbo opentk

我正在尝试学习如何使用C#OpenTK中的VBO进行绘制 - 遵循http://www.opentk.com/node/2292VBOs Using Interleaved Vertices in C#等示例。

我非常确定我想要像这样的交错单数组方法,每个顶点都有一个整齐的结构。我得到了编译没有错误的代码,但它只是绘制一个空白的棕色屏幕,没有白色三角形。我确定我犯了一个愚蠢的错误,请帮助我学习它!

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using OpenTK;
using OpenTK.Graphics;
using OpenTK.Graphics.OpenGL;
using OpenTK.Input;
using System.Drawing;
using System.Runtime.InteropServices;

namespace VBOTest2
{
    [StructLayout(LayoutKind.Sequential)]
    public struct Vertex
    {
        public Vector3 Position;
        public byte[] Colour;

        public Vertex(byte[] colour, Vector3 position)
        {
            Colour = colour;
            Position = position;
        }

        public static readonly int Stride = Marshal.SizeOf(default(Vertex));
    }

    public class VBOTest2 : GameWindow
    {
        uint vbo;

        public VBOTest2() :
            base(1, 1, new GraphicsMode(32, 24, 8, 0), "Test")
        {
            Width = 1500;
            Height = 800;
            VSync = VSyncMode.On;
            ClientSize = new Size(1500, 800);
            this.Location = new System.Drawing.Point(100, 300);
            GL.Viewport(0, 0, Width, Height);
        }

        void CreateVertexBuffer()
        {
            Vertex[] vertices = new Vertex[3];
            vertices[0] = new Vertex(new byte[]{255,255,255,255}, new Vector3(-1f, -1f, 0f));
            vertices[1] = new Vertex(new byte[] { 255, 255, 255, 255 }, new Vector3(1f, -1f, 0f));
            vertices[2] = new Vertex(new byte[] { 255, 255, 255, 255 }, new Vector3(0f, 1f, 0f));

            GL.GenBuffers(1, out vbo);
            GL.BindBuffer(BufferTarget.ArrayBuffer, vbo);
            GL.BufferData<Vertex>(BufferTarget.ArrayBuffer, (IntPtr)Vertex.Stride, vertices, BufferUsageHint.StaticDraw);           
        }

        protected override void OnLoad(EventArgs e)
        {
            GL.ClearColor(Color.Brown);
            CreateVertexBuffer();
        }


        protected override void OnRenderFrame(FrameEventArgs e)
        {
            base.OnRenderFrame(e);
            GL.Clear(ClearBufferMask.ColorBufferBit);

            GL.EnableClientState(ArrayCap.VertexArray);
            GL.EnableClientState(ArrayCap.ColorArray);

            GL.BindBuffer(BufferTarget.ArrayBuffer, vbo);

            GL.VertexPointer(3, VertexPointerType.Float, Vertex.Stride, (IntPtr)(0));
            GL.ColorPointer(4, ColorPointerType.UnsignedByte, Vertex.Stride, (IntPtr)(3 * sizeof(float)));

            GL.DrawArrays(PrimitiveType.Triangles, 0, 3);

            //release buffer
            GL.BindBuffer(BufferTarget.ArrayBuffer, 0);

            GL.DisableClientState(ArrayCap.VertexArray);
            GL.DisableClientState(ArrayCap.ColorArray);

            SwapBuffers();
        }
    }

}   

0 个答案:

没有答案