getting text rendering working with stb_truetype
This commit is contained in:
BIN
assets/fonts/KodeMono.ttf
Normal file
BIN
assets/fonts/KodeMono.ttf
Normal file
Binary file not shown.
11
assets/shaders/text.fragment.glsl
Normal file
11
assets/shaders/text.fragment.glsl
Normal file
@@ -0,0 +1,11 @@
|
||||
#version 330 core
|
||||
out vec4 pixel_color;
|
||||
|
||||
in vec4 frag_color;
|
||||
in vec2 frag_uv_position;
|
||||
|
||||
uniform sampler2D glyph_atlas;
|
||||
|
||||
void main() {
|
||||
pixel_color = vec4(1,1,1,texture(glyph_atlas, frag_uv_position).r);
|
||||
};
|
||||
73
assets/shaders/text.vertex.glsl
Normal file
73
assets/shaders/text.vertex.glsl
Normal file
@@ -0,0 +1,73 @@
|
||||
#version 330 core
|
||||
layout (location = 0) in vec2 begin;
|
||||
layout (location = 1) in int glyph;
|
||||
layout (location = 2) in float lineHeight;
|
||||
layout (location = 3) in vec4 color;
|
||||
|
||||
/*
|
||||
typedef struct GlyphMeta GlyphMeta;
|
||||
struct GlyphMeta {
|
||||
// chunk 1
|
||||
RLVector2 uv0;
|
||||
RLVector2 uv1;
|
||||
|
||||
// chunk 2
|
||||
real32 xOffset;
|
||||
real32 yOffset;
|
||||
real32 width;
|
||||
real32 height;
|
||||
};
|
||||
*/
|
||||
|
||||
uniform samplerBuffer glyph_table;
|
||||
|
||||
uniform mat4 projection;
|
||||
uniform sampler2D font;
|
||||
|
||||
out vec4 frag_color;
|
||||
out vec2 frag_uv_position;
|
||||
|
||||
const vec2 rectangle_vertices[4] = vec2[](
|
||||
vec2(-1, -1), // bl
|
||||
vec2(-1, 1), // tl
|
||||
vec2( 1, -1), // br
|
||||
vec2( 1, 1) // tr
|
||||
);
|
||||
|
||||
const vec2 uv0_vertices[4] = vec2[](
|
||||
vec2(1, 0), // bl
|
||||
vec2(1, 1), // tl
|
||||
vec2(0, 0), // br
|
||||
vec2(0, 1) // tr
|
||||
);
|
||||
|
||||
const vec2 uv1_vertices[4] = vec2[](
|
||||
vec2(0, 1),
|
||||
vec2(0, 0),
|
||||
vec2(1, 1),
|
||||
vec2(1, 0)
|
||||
);
|
||||
|
||||
void main() {
|
||||
vec4 chunk1 = texelFetch(glyph_table, glyph * 2 + 0);
|
||||
vec4 chunk2 = texelFetch(glyph_table, glyph * 2 + 1);
|
||||
|
||||
vec2 uv0 = chunk1.xy;
|
||||
vec2 uv1 = chunk1.zw;
|
||||
vec2 offset = chunk2.xy;
|
||||
vec2 dims = chunk2.zw;
|
||||
|
||||
vec2 p0 = begin + offset*lineHeight;
|
||||
vec2 p1 = begin + (offset + dims)*lineHeight;
|
||||
|
||||
vec2 dest_half_size = (p1 - p0) / 2;
|
||||
vec2 dest_center = (p1 + p0) / 2;
|
||||
vec2 dest_position = rectangle_vertices[gl_VertexID] * dest_half_size + dest_center;
|
||||
|
||||
vec2 uv_position = uv0 * uv0_vertices[gl_VertexID] + uv1 * uv1_vertices[gl_VertexID];
|
||||
|
||||
gl_Position = projection * vec4(dest_position, 0, 1);
|
||||
|
||||
frag_color = color;
|
||||
frag_uv_position = vec2(uv_position.x, uv_position.y);
|
||||
}
|
||||
Reference in New Issue
Block a user