From 7a00c54f25d0a0946ab0a7564e63ee63750c1a5e Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Tue, 19 Apr 2022 09:20:55 -0700 Subject: [PATCH] cell: convert atlas x/y to NDC in shader --- shaders/cell.v.glsl | 8 +++++--- src/FontAtlas.zig | 15 ++++++--------- src/Grid.zig | 12 ++++++------ 3 files changed, 17 insertions(+), 18 deletions(-) diff --git a/shaders/cell.v.glsl b/shaders/cell.v.glsl index 722d2ec20..d6eb693d9 100644 --- a/shaders/cell.v.glsl +++ b/shaders/cell.v.glsl @@ -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.; diff --git a/src/FontAtlas.zig b/src/FontAtlas.zig index fdf6f691f..87680bafc 100644 --- a/src/FontAtlas.zig +++ b/src/FontAtlas.zig @@ -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), }; diff --git a/src/Grid.zig b/src/Grid.zig index 14e84001a..93a677633 100644 --- a/src/Grid.zig +++ b/src/Grid.zig @@ -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,