瀏覽代碼

Distance field font rendering

Máté Homolya 2 年之前
父節點
當前提交
99e9318086
共有 2 個文件被更改,包括 7 次插入3 次删除
  1. 4 1
      shaders/simple_text.frag
  2. 3 2
      src/free_glyph.c

+ 4 - 1
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);
 }

+ 3 - 2
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);
         }