forked from ppy/osu-framework
-
Notifications
You must be signed in to change notification settings - Fork 1
/
GLQuadBuffer.cs
68 lines (54 loc) · 2.4 KB
/
GLQuadBuffer.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
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text.
using System;
using System.Diagnostics;
using osu.Framework.Graphics.Rendering;
using osu.Framework.Graphics.Rendering.Vertices;
using osuTK.Graphics.ES30;
namespace osu.Framework.Graphics.OpenGL.Buffers
{
internal static class GLQuadIndexData
{
static GLQuadIndexData()
{
GL.GenBuffers(1, out EBO_ID);
}
public static readonly int EBO_ID;
public static int MaxAmountIndices;
}
internal class GLQuadBuffer<T> : GLVertexBuffer<T>
where T : unmanaged, IEquatable<T>, IVertex
{
private readonly int amountIndices;
public GLQuadBuffer(GLRenderer renderer, int amountQuads, BufferUsageHint usage)
: base(renderer, amountQuads * IRenderer.VERTICES_PER_QUAD, usage)
{
amountIndices = amountQuads * IRenderer.INDICES_PER_QUAD;
Debug.Assert(amountIndices <= IRenderer.MAX_VERTICES);
}
protected override void Initialise()
{
base.Initialise();
// Must be outside the conditional below as it needs to be added to the VAO
GL.BindBuffer(BufferTarget.ElementArrayBuffer, GLQuadIndexData.EBO_ID);
if (amountIndices > GLQuadIndexData.MaxAmountIndices)
{
ushort[] indices = new ushort[amountIndices];
for (int i = 0, j = 0; j < amountIndices; i += IRenderer.VERTICES_PER_QUAD, j += IRenderer.INDICES_PER_QUAD)
{
indices[j] = (ushort)i;
indices[j + 1] = (ushort)(i + 1);
indices[j + 2] = (ushort)(i + 3);
indices[j + 3] = (ushort)(i + 2);
indices[j + 4] = (ushort)(i + 3);
indices[j + 5] = (ushort)(i + 1);
}
GL.BufferData(BufferTarget.ElementArrayBuffer, (IntPtr)(amountIndices * sizeof(ushort)), indices, BufferUsageHint.StaticDraw);
GLQuadIndexData.MaxAmountIndices = amountIndices;
}
}
protected override int ToElements(int vertices) => 3 * vertices / 2;
protected override int ToElementIndex(int vertexIndex) => 3 * vertexIndex / 2;
protected override PrimitiveTopology Topology => PrimitiveTopology.Triangles;
}
}