font.frag 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. #version 330 core
  2. #define FONT_WIDTH 128
  3. #define FONT_HEIGHT 64
  4. #define FONT_COLS 18
  5. #define FONT_ROWS 7
  6. #define FONT_CH_W (FONT_WIDTH / FONT_COLS)
  7. #define FONT_CH_H (FONT_HEIGHT / FONT_ROWS)
  8. #define FONT_CH_W_UV (float(FONT_CH_W) / float(FONT_WIDTH))
  9. #define FONT_CH_H_UV (float(FONT_CH_H) / float(FONT_HEIGHT))
  10. uniform sampler2D font;
  11. uniform float time;
  12. flat in int glyph_c;
  13. in vec4 glyph_fg_color;
  14. in vec4 glyph_bg_color;
  15. in vec2 uv;
  16. float
  17. map01(float x)
  18. {
  19. return (x + 1) / 2;
  20. }
  21. void main() {
  22. int c = glyph_c;
  23. if ( ! (c >= 32 && c <= 126) ) {
  24. c = 63;
  25. }
  26. int index = c - 32;
  27. float x = float(index % FONT_COLS) * FONT_CH_W_UV;
  28. float y = float(index / FONT_COLS) * FONT_CH_H_UV;
  29. vec2 ch_size = vec2(FONT_CH_W_UV, -FONT_CH_H_UV);
  30. vec2 pos = vec2(x, y + FONT_CH_H_UV);
  31. vec2 uv_ = vec2(uv.x, uv.y);
  32. vec4 t = texture(font, pos + ch_size * uv_);
  33. gl_FragColor = glyph_bg_color * (1.0 - t.x) + t.x * glyph_fg_color
  34. * vec4(map01(sin(time)),
  35. map01(cos(time)),
  36. map01(sin(time)),
  37. 1);
  38. }