-
Notifications
You must be signed in to change notification settings - Fork 5
/
VertexBuffer.cs
69 lines (47 loc) · 1.47 KB
/
VertexBuffer.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
namespace glvertexid;
public unsafe abstract class VertexBuffer<T> where T : unmanaged
{
public VertexBuffer(uint vertexSize)
{
this.vertexSize = vertexSize;
// Create a VAO
vaoHandle = Gl.GenVertexArray();
Gl.BindVertexArray(vaoHandle);
// Create a VBO
vboHandle = Gl.GenBuffer();
Gl.BindBuffer(BufferTargetARB.ArrayBuffer, vboHandle);
// Set up attribs
SetupVAO();
// Clean up
UnbindVAO();
UnbindVBO();
}
public void BindAndDraw()
{
BindVAO();
Gl.DrawArrays(primitiveType, 0, (uint)size);
UnbindVAO();
}
public void BufferData(int size, T* data)
{
this.size = size;
BindVBO();
Gl.BufferData(BufferTargetARB.ArrayBuffer, (uint)(size * vertexSize), data, BufferUsageARB.StaticDraw);
UnbindVBO();
}
// GPU data
public int size;
protected uint vertexSize;
// Rendering settings
public PrimitiveType primitiveType = PrimitiveType.Triangles;
// Buffer handles
uint vaoHandle;
uint vboHandle;
// Abstracts
protected abstract void SetupVAO();
// Shortcut functions
public void BindVAO() => Gl.BindVertexArray(vaoHandle);
public void BindVBO() => Gl.BindBuffer(BufferTargetARB.ArrayBuffer, vboHandle);
public static void UnbindVAO() => Gl.BindVertexArray(0);
public static void UnbindVBO() => Gl.BindBuffer(BufferTargetARB.ArrayBuffer, 0);
}