I'm trying to learn how to use VBO (Vertex Buffer Objects) by putting together a simple program that draws a quad to the screen using OpenTK (C# OpenGL bindings).
Unfortunately I'm not seeing anything on screen. If I draw the same quad in immediate mode it shows up fine.
Since it wasn't working I tried adding Color Data too (in case this was the problem) but it didn't help.
Any help would be greatly appreciated!
private uint indexBufferId;
private uint vertexBufferId;
private uint colorBufferId;
private void InitialiseData()
{
// Set-up index buffer:
ushort[] indices = new ushort[] { 0, 1, 2, 3 };
GL.GenBuffers(1, out indexBufferId);
GL.BindBuffer(BufferTarget.ElementArrayBuffer, indexBufferId);
GL.BufferData(
BufferTarget.ElementArrayBuffer,
(IntPtr)(indices.Length * sizeof(ushort)),
indices,
BufferUsageHint.StaticDraw);
// Set-up vertex buffer:
float[] vertexData = new float[] {
50.0f, 50.0f,
100.0f, 50.0f,
100.0f, 100.0f,
50.0f, 100.0f };
GL.GenBuffers(1, out vertexBufferId);
GL.BindBuffer(BufferTarget.ArrayBuffer, vertexBufferId);
GL.BufferData(
BufferTarget.ArrayBuffer,
(IntPtr)(vertexData.Length * sizeof(float)),
vertexData,
BufferUsageHint.StaticDraw);
// Set-up color buffer:
float[] colorData = new float[] {
1.0f, 1.0f, 1.0f, 1.0f,
1.0f, 1.0f, 1.0f, 1.0f,
1.0f, 1.0f, 1.0f, 1.0f,
1.0f, 1.0f, 1.0f, 1.0f
};
GL.GenBuffers(1, out colorBufferId);
GL.BindBuffer(BufferTarget.ArrayBuffer, colorBufferId);
GL.BufferData(
BufferTarget.ArrayBuffer,
(IntPtr)(colorData.Length * sizeof(float)),
colorData,
BufferUsageHint.StaticDraw);
}
protected override void OnRenderFrame(FrameEventArgs e)
{
base.OnRenderFrame(e);
GL.Clear(ClearBufferMask.ColorBufferBit);
// Bind vertex buffer:
GL.BindBuffer(BufferTarget.ArrayBuffer, vertexBufferId);
GL.EnableClientState(ArrayCap.VertexArray);
GL.VertexPointer(2, VertexPointerType.Float, 0, IntPtr.Zero);
// Bind color buffer:
GL.BindBuffer(BufferTarget.ArrayBuffer, colorBufferId);
GL.EnableClientState(ArrayCap.ColorArray);
GL.ColorPointer(4, ColorPointerType.Float, 0, IntPtr.Zero);
// Bind index buffer:
GL.BindBuffer(BufferTarget.ElementArrayBuffer, indexBufferId);
// Draw:
GL.DrawElements(
BeginMode.Quads,
1,
DrawElementsType.UnsignedShort,
IntPtr.Zero);
// Disable:
GL.DisableClientState(ArrayCap.VertexArray);
GL.DisableClientState(ArrayCap.ColorArray);
SwapBuffers();
}