-
Notifications
You must be signed in to change notification settings - Fork 0
/
WindowVertexBuffer.cs
71 lines (53 loc) · 1.78 KB
/
WindowVertexBuffer.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
70
71
namespace drawing;
[StructLayout(LayoutKind.Sequential, Pack = 1)]
public struct WindowVertex(float posX, float posY, float uvX, float uvY)
{
float PositionX = posX;
float PositionY = posY;
float UVX = uvX;
float UVY = uvY;
}
public unsafe class WindowVertexBuffer
{
public WindowVertexBuffer()
{
vertexSize = (uint)Marshal.SizeOf<WindowVertex>();
// Create a VAO
vaoHandle = Gl.GenVertexArray();
Gl.BindVertexArray(vaoHandle);
// Create a VBO
vboHandle = Gl.GenBuffer();
Gl.BindBuffer(BufferTargetARB.ArrayBuffer, vboHandle);
// Set up attribs
Gl.EnableVertexAttribArray(0);
Gl.EnableVertexAttribArray(1);
Gl.VertexAttribPointer(0, 2, VertexAttribPointerType.Float, false, vertexSize, (void*)0);
Gl.VertexAttribPointer(1, 2, VertexAttribPointerType.Float, false, vertexSize, (void*)8);
// Clean up
UnbindVAO();
UnbindVBO();
}
public void BufferData(WindowVertex* data)
{
BindVBO();
Gl.BufferData(BufferTargetARB.ArrayBuffer, VERTEX_COUNT * vertexSize, data, BufferUsageARB.StaticDraw);
UnbindVBO();
}
public void BindAndDraw()
{
BindVAO();
Gl.DrawArrays(PrimitiveType.TriangleStrip, 0, VERTEX_COUNT);
UnbindVAO();
}
// GPU data
public const int VERTEX_COUNT = 4;
protected uint vertexSize;
// Buffer handles
uint vaoHandle;
uint vboHandle;
// 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);
}