annotate semiconginev2/text.nim @ 1241:a0ed1a918fda

fix: letters sometimes overlapping other letters quad
author sam <sam@basx.dev>
date Mon, 22 Jul 2024 12:51:50 +0700
parents 176383220123
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1234
841e12f33c47 add: text & font rendering, not tested yet
sam <sam@basx.dev>
parents:
diff changeset
1 const
841e12f33c47 add: text & font rendering, not tested yet
sam <sam@basx.dev>
parents:
diff changeset
2 NEWLINE = Rune('\n')
841e12f33c47 add: text & font rendering, not tested yet
sam <sam@basx.dev>
parents:
diff changeset
3 SPACE = Rune(' ')
841e12f33c47 add: text & font rendering, not tested yet
sam <sam@basx.dev>
parents:
diff changeset
4
841e12f33c47 add: text & font rendering, not tested yet
sam <sam@basx.dev>
parents:
diff changeset
5 type
841e12f33c47 add: text & font rendering, not tested yet
sam <sam@basx.dev>
parents:
diff changeset
6 GlyphInfo* = object
841e12f33c47 add: text & font rendering, not tested yet
sam <sam@basx.dev>
parents:
diff changeset
7 uvs*: array[4, Vec2f]
841e12f33c47 add: text & font rendering, not tested yet
sam <sam@basx.dev>
parents:
diff changeset
8 dimension*: Vec2f
841e12f33c47 add: text & font rendering, not tested yet
sam <sam@basx.dev>
parents:
diff changeset
9 topOffset*: float32
841e12f33c47 add: text & font rendering, not tested yet
sam <sam@basx.dev>
parents:
diff changeset
10 leftOffset*: float32
841e12f33c47 add: text & font rendering, not tested yet
sam <sam@basx.dev>
parents:
diff changeset
11 advance*: float32
841e12f33c47 add: text & font rendering, not tested yet
sam <sam@basx.dev>
parents:
diff changeset
12 FontObj* = object
841e12f33c47 add: text & font rendering, not tested yet
sam <sam@basx.dev>
parents:
diff changeset
13 name*: string
841e12f33c47 add: text & font rendering, not tested yet
sam <sam@basx.dev>
parents:
diff changeset
14 glyphs*: Table[Rune, GlyphInfo]
841e12f33c47 add: text & font rendering, not tested yet
sam <sam@basx.dev>
parents:
diff changeset
15 fontAtlas*: Image[Gray]
841e12f33c47 add: text & font rendering, not tested yet
sam <sam@basx.dev>
parents:
diff changeset
16 maxHeight*: int
841e12f33c47 add: text & font rendering, not tested yet
sam <sam@basx.dev>
parents:
diff changeset
17 kerning*: Table[(Rune, Rune), float32]
841e12f33c47 add: text & font rendering, not tested yet
sam <sam@basx.dev>
parents:
diff changeset
18 fontscale*: float32
841e12f33c47 add: text & font rendering, not tested yet
sam <sam@basx.dev>
parents:
diff changeset
19 lineHeight*: float32
841e12f33c47 add: text & font rendering, not tested yet
sam <sam@basx.dev>
parents:
diff changeset
20 lineAdvance*: float32
841e12f33c47 add: text & font rendering, not tested yet
sam <sam@basx.dev>
parents:
diff changeset
21 capHeight*: float32
841e12f33c47 add: text & font rendering, not tested yet
sam <sam@basx.dev>
parents:
diff changeset
22 xHeight*: float32
841e12f33c47 add: text & font rendering, not tested yet
sam <sam@basx.dev>
parents:
diff changeset
23 Font = ref FontObj
841e12f33c47 add: text & font rendering, not tested yet
sam <sam@basx.dev>
parents:
diff changeset
24
841e12f33c47 add: text & font rendering, not tested yet
sam <sam@basx.dev>
parents:
diff changeset
25 TextboxData = object
841e12f33c47 add: text & font rendering, not tested yet
sam <sam@basx.dev>
parents:
diff changeset
26 color: Vec4f
841e12f33c47 add: text & font rendering, not tested yet
sam <sam@basx.dev>
parents:
diff changeset
27 position: Vec3f
841e12f33c47 add: text & font rendering, not tested yet
sam <sam@basx.dev>
parents:
diff changeset
28 scale: float32
1236
176383220123 add: first font-rendering test
sam <sam@basx.dev>
parents: 1234
diff changeset
29 aspectratio: float32
1234
841e12f33c47 add: text & font rendering, not tested yet
sam <sam@basx.dev>
parents:
diff changeset
30 TextboxDescriptorSet = object
841e12f33c47 add: text & font rendering, not tested yet
sam <sam@basx.dev>
parents:
diff changeset
31 textbox: GPUValue[TextboxData, UniformBufferMapped]
841e12f33c47 add: text & font rendering, not tested yet
sam <sam@basx.dev>
parents:
diff changeset
32 fontAtlas: Image[Gray]
841e12f33c47 add: text & font rendering, not tested yet
sam <sam@basx.dev>
parents:
diff changeset
33
841e12f33c47 add: text & font rendering, not tested yet
sam <sam@basx.dev>
parents:
diff changeset
34 DefaultFontShader* = object
841e12f33c47 add: text & font rendering, not tested yet
sam <sam@basx.dev>
parents:
diff changeset
35 position {.VertexAttribute.}: Vec3f
841e12f33c47 add: text & font rendering, not tested yet
sam <sam@basx.dev>
parents:
diff changeset
36 uv {.VertexAttribute.}: Vec2f # TODO: maybe we can keep the uvs in a uniform buffer and just pass an index
841e12f33c47 add: text & font rendering, not tested yet
sam <sam@basx.dev>
parents:
diff changeset
37 fragmentUv {.Pass.}: Vec2f
841e12f33c47 add: text & font rendering, not tested yet
sam <sam@basx.dev>
parents:
diff changeset
38 color {.ShaderOutput.}: Vec4f
841e12f33c47 add: text & font rendering, not tested yet
sam <sam@basx.dev>
parents:
diff changeset
39 descriptorSets {.DescriptorSets.}: (TextboxDescriptorSet, )
1236
176383220123 add: first font-rendering test
sam <sam@basx.dev>
parents: 1234
diff changeset
40 vertexCode = """void main() {
176383220123 add: first font-rendering test
sam <sam@basx.dev>
parents: 1234
diff changeset
41 gl_Position = vec4(position * vec3(1, textbox.aspectratio, 1) * textbox.scale + textbox.position, 1.0);
1234
841e12f33c47 add: text & font rendering, not tested yet
sam <sam@basx.dev>
parents:
diff changeset
42 fragmentUv = uv;
1236
176383220123 add: first font-rendering test
sam <sam@basx.dev>
parents: 1234
diff changeset
43 } """
176383220123 add: first font-rendering test
sam <sam@basx.dev>
parents: 1234
diff changeset
44 fragmentCode = """void main() {
1241
a0ed1a918fda fix: letters sometimes overlapping other letters quad
sam <sam@basx.dev>
parents: 1236
diff changeset
45 float v = texture(fontAtlas, fragmentUv).r;
a0ed1a918fda fix: letters sometimes overlapping other letters quad
sam <sam@basx.dev>
parents: 1236
diff changeset
46 if(v == 0) {
a0ed1a918fda fix: letters sometimes overlapping other letters quad
sam <sam@basx.dev>
parents: 1236
diff changeset
47 discard;
a0ed1a918fda fix: letters sometimes overlapping other letters quad
sam <sam@basx.dev>
parents: 1236
diff changeset
48 }
a0ed1a918fda fix: letters sometimes overlapping other letters quad
sam <sam@basx.dev>
parents: 1236
diff changeset
49 color = vec4(textbox.color.rgb, textbox.color.a * v);
1236
176383220123 add: first font-rendering test
sam <sam@basx.dev>
parents: 1234
diff changeset
50 }"""
1234
841e12f33c47 add: text & font rendering, not tested yet
sam <sam@basx.dev>
parents:
diff changeset
51
841e12f33c47 add: text & font rendering, not tested yet
sam <sam@basx.dev>
parents:
diff changeset
52
841e12f33c47 add: text & font rendering, not tested yet
sam <sam@basx.dev>
parents:
diff changeset
53 include ./text/font
841e12f33c47 add: text & font rendering, not tested yet
sam <sam@basx.dev>
parents:
diff changeset
54 include ./text/textbox