Parcourir la source

Merge pull request #60 from mate-h/mate-h

Distance field font rendering
Alexey Kutepov il y a 2 ans
Parent
commit
02b344798c
4 fichiers modifiés avec 17 ajouts et 4 suppressions
  1. 6 0
      README.md
  2. 4 1
      shaders/simple_epic.frag
  3. 4 1
      shaders/simple_text.frag
  4. 3 2
      src/free_glyph.c

+ 6 - 0
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

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

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