commit 02b344798c6a4ae66f72c71d6d591264d962bc4b
parent 7e614994b24eb29be18bcf0af2880f353012d587
Author: Alexey Kutepov <reximkut@gmail.com>
Date: Sat, 11 Feb 2023 00:12:03 +0700
Merge pull request #60 from mate-h/mate-h
Distance field font rendering
Diffstat:
4 files changed, 17 insertions(+), 4 deletions(-)
diff --git a/README.md b/README.md
@@ -4,6 +4,12 @@
# Quick Start
+## Dependencies
+
+- [SDL2 2.0.9+](https://www.libsdl.org/)
+- [FreeType 2.13.0+](https://freetype.org/)
+- [GLEW 2.1.0+](https://glew.sourceforge.net/)
+
## POSIX
```console
diff --git a/shaders/simple_epic.frag b/shaders/simple_epic.frag
@@ -13,7 +13,10 @@ vec3 hsl2rgb(vec3 c) {
void main() {
vec4 tc = texture(image, out_uv);
+ float d = tc.r;
+ float aaf = fwidth(d);
+ float alpha = smoothstep(0.5 - aaf, 0.5 + aaf, d);
vec2 frag_uv = gl_FragCoord.xy / resolution;
vec4 rainbow = vec4(hsl2rgb(vec3((time + frag_uv.x + frag_uv.y), 0.5, 0.5)), 1.0);
- gl_FragColor = tc.x * rainbow;
+ gl_FragColor = vec4(rainbow.rgb, alpha);
}
diff --git a/shaders/simple_text.frag b/shaders/simple_text.frag
@@ -6,5 +6,8 @@ in vec4 out_color;
in vec2 out_uv;
void main() {
- gl_FragColor = texture(image, out_uv).x*out_color;
+ float d = texture(image, out_uv).r;
+ float aaf = fwidth(d);
+ float alpha = smoothstep(0.5 - aaf, 0.5 + aaf, d);
+ gl_FragColor = vec4(out_color.rgb, alpha);
}
diff --git a/src/free_glyph.c b/src/free_glyph.c
@@ -4,8 +4,9 @@
void free_glyph_atlas_init(Free_Glyph_Atlas *atlas, FT_Face face)
{
+ FT_Int32 load_flags = FT_LOAD_RENDER | FT_LOAD_TARGET_(FT_RENDER_MODE_SDF);
for (int i = 32; i < 128; ++i) {
- if (FT_Load_Char(face, i, FT_LOAD_RENDER)) {
+ if (FT_Load_Char(face, i, load_flags)) {
fprintf(stderr, "ERROR: could not load glyph of a character with code %d\n", i);
exit(1);
}
@@ -39,7 +40,7 @@ void free_glyph_atlas_init(Free_Glyph_Atlas *atlas, FT_Face face)
int x = 0;
for (int i = 32; i < 128; ++i) {
- if (FT_Load_Char(face, i, FT_LOAD_RENDER)) {
+ if (FT_Load_Char(face, i, load_flags)) {
fprintf(stderr, "ERROR: could not load glyph of a character with code %d\n", i);
exit(1);
}