-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTextRenderer.cs
More file actions
41 lines (38 loc) · 1.47 KB
/
TextRenderer.cs
File metadata and controls
41 lines (38 loc) · 1.47 KB
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
using OpenTK.Graphics.OpenGL4;
using OpenTK.Mathematics;
namespace TextMeshRender;
public static class TextRenderer
{
public static void Render(string text, FontMesh fontMesh, Shader shader, Matrix4 baseMatrix, float fontSize, Vector3 color)
{
var model = Matrix4.Identity;
var offset = Vector3.Zero;
foreach (var t in text)
{
switch (t)
{
case ' ':
offset += Vector3.UnitX;
model = Matrix4.CreateTranslation(offset);
continue;
case '\n':
offset = new Vector3(0.0f, offset.Y + 3.0f, 0.0f);
model = Matrix4.CreateTranslation(offset);
continue;
}
if (!fontMesh.Glyphs.TryGetValue(t, out var character)) continue;
var mesh = character.Mesh;
if (mesh == null) continue;
shader.Use();
shader.SetUniform("color", color);
shader.SetUniform("model", model * Matrix4.CreateScale(fontSize) * baseMatrix);
offset += Vector3.UnitX * character.Advance;
model = Matrix4.CreateTranslation(offset);
GL.Disable(EnableCap.DepthTest);
GL.Disable(EnableCap.CullFace);
GL.BindVertexArray(mesh.Vao);
GL.DrawElements(PrimitiveType.Triangles, mesh.IndicesCount, DrawElementsType.UnsignedInt, 0);
GL.BindVertexArray(0);
}
}
}