diff semiconginev2/text.nim @ 1234:841e12f33c47

add: text & font rendering, not tested yet
author sam <sam@basx.dev>
date Sat, 20 Jul 2024 00:03:57 +0700
parents
children 176383220123
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/semiconginev2/text.nim	Sat Jul 20 00:03:57 2024 +0700
@@ -0,0 +1,47 @@
+const
+  NEWLINE = Rune('\n')
+  SPACE = Rune(' ')
+
+type
+  GlyphInfo* = object
+    uvs*: array[4, Vec2f]
+    dimension*: Vec2f
+    topOffset*: float32
+    leftOffset*: float32
+    advance*: float32
+  FontObj* = object
+    name*: string
+    glyphs*: Table[Rune, GlyphInfo]
+    fontAtlas*: Image[Gray]
+    maxHeight*: int
+    kerning*: Table[(Rune, Rune), float32]
+    fontscale*: float32
+    lineHeight*: float32
+    lineAdvance*: float32
+    capHeight*: float32
+    xHeight*: float32
+  Font = ref FontObj
+
+  TextboxData = object
+    color: Vec4f
+    position: Vec3f
+    scale: float32
+  TextboxDescriptorSet = object
+    textbox: GPUValue[TextboxData, UniformBufferMapped]
+    fontAtlas: Image[Gray]
+
+  DefaultFontShader* = object
+    position {.VertexAttribute.}: Vec3f
+    uv {.VertexAttribute.}: Vec2f # TODO: maybe we can keep the uvs in a uniform buffer and just pass an index
+    fragmentUv {.Pass.}: Vec2f
+    color {.ShaderOutput.}: Vec4f
+    descriptorSets {.DescriptorSets.}: (TextboxDescriptorSet, )
+    vertexCode = &"""
+  gl_Position = vec4(position * textbox.scale + textbox.position, 1.0);
+  fragmentUv = uv;
+  """
+    fragmentCode = &"""color = vec4(textbox.color.rgb, textbox.color.rgb.a * texture(fontAtlas, fragmentUv).r);"""
+
+
+include ./text/font
+include ./text/textbox