cell: convert atlas x/y to NDC in shader

This commit is contained in:
Mitchell Hashimoto
2022-04-19 09:20:55 -07:00
parent ac8120f073
commit 7a00c54f25
3 changed files with 17 additions and 18 deletions

View File

@ -71,10 +71,12 @@ void main() {
cell_pos = cell_pos + glyph_size * position + glyph_offset_calc;
gl_Position = projection * vec4(cell_pos, 0.0, 1.0);
// Calculate our texture coordinate
// We need to convert our texture position and size to normalized
// device coordinates (0 to 1.0) by dividing by the size of the texture.
ivec2 text_size = textureSize(text, 0);
vec2 glyph_tex_size = glyph_size / text_size.xy;
glyph_tex_coords = glyph_pos + glyph_tex_size * position;
vec2 glyph_tex_pos = glyph_pos / text_size;
vec2 glyph_tex_size = glyph_size / text_size;
glyph_tex_coords = glyph_tex_pos + glyph_tex_size * position;
// Set our foreground color output
color = fg_color_in / 255.;

View File

@ -41,11 +41,10 @@ pub const Glyph = struct {
/// top bearing
offset_y: i32,
/// normalized x, y (s, t) coordinates
s0: f32,
t0: f32,
s1: f32,
t1: f32,
/// coordinates in the atlas of the top-left corner. These have to
/// be normalized to be between 0 and 1 prior to use.
atlas_x: u32,
atlas_y: u32,
/// horizontal position to increase drawing position for strings
advance_x: f32,
@ -170,10 +169,8 @@ pub fn addGlyph(self: *FontAtlas, alloc: Allocator, v: anytype) !*Glyph {
.height = tgt_h,
.offset_x = glyph.*.bitmap_left,
.offset_y = glyph.*.bitmap_top,
.s0 = @intToFloat(f32, region.x) / @intToFloat(f32, self.atlas.size),
.t0 = @intToFloat(f32, region.y) / @intToFloat(f32, self.atlas.size),
.s1 = @intToFloat(f32, region.x + tgt_w) / @intToFloat(f32, self.atlas.size),
.t1 = @intToFloat(f32, region.y + tgt_h) / @intToFloat(f32, self.atlas.size),
.atlas_x = region.x,
.atlas_y = region.y,
.advance_x = f26dot6ToFloat(glyph.*.advance.x),
};

View File

@ -41,8 +41,8 @@ const GPUCell = struct {
grid_row: u16,
/// vec2 glyph_pos
glyph_x: f32,
glyph_y: f32,
glyph_x: u32,
glyph_y: u32,
/// vec2 glyph_size
glyph_width: u32,
@ -139,8 +139,8 @@ pub fn init(alloc: Allocator) !Grid {
var offset: usize = 0;
try vbobind.attributeAdvanced(0, 2, gl.c.GL_UNSIGNED_SHORT, false, @sizeOf(GPUCell), offset);
offset += 2 * @sizeOf(u16);
try vbobind.attributeAdvanced(1, 2, gl.c.GL_FLOAT, false, @sizeOf(GPUCell), offset);
offset += 2 * @sizeOf(f32);
try vbobind.attributeAdvanced(1, 2, gl.c.GL_UNSIGNED_INT, false, @sizeOf(GPUCell), offset);
offset += 2 * @sizeOf(u32);
try vbobind.attributeAdvanced(2, 2, gl.c.GL_UNSIGNED_INT, false, @sizeOf(GPUCell), offset);
offset += 2 * @sizeOf(u32);
try vbobind.attributeAdvanced(3, 2, gl.c.GL_INT, false, @sizeOf(GPUCell), offset);
@ -251,8 +251,8 @@ pub fn updateCells(self: *Grid, term: Terminal) !void {
self.cells.appendAssumeCapacity(.{
.grid_col = @intCast(u16, x),
.grid_row = @intCast(u16, y),
.glyph_x = glyph.s0,
.glyph_y = glyph.t0,
.glyph_x = glyph.atlas_x,
.glyph_y = glyph.atlas_y,
.glyph_width = glyph.width,
.glyph_height = glyph.height,
.glyph_offset_x = glyph.offset_x,