2

I'm trying to learn how to draw with VBOs in C# OpenTK - following examples like http://www.opentk.com/node/2292 and VBOs Using Interleaved Vertices in C#.

I'm pretty sure I want the interleaved single array method like this, with a neat struct for each vertex. I got the code to compile with no errors, but it simply draws a blank brown screen, no white triangle. I'm sure I've made a stupid error, please help me learn from it!

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();
        }
    }

}   
Community
  • 1
  • 1
Mr Pie
  • 327
  • 2
  • 11

0 Answers0