busted text rendering

This commit is contained in:
Mitchell Hashimoto
2022-04-04 09:43:46 -07:00
parent 670af17a1b
commit fc28b8c032
7 changed files with 167 additions and 73 deletions

View File

@@ -8,12 +8,15 @@ alloc: std.mem.Allocator,
ft: ftc.FT_Library,
face: ftc.FT_Face,
chars: CharList,
vao: gl.VertexArray = undefined,
vbo: gl.Buffer = undefined,
program: gl.Program = undefined,
const CharList = std.ArrayListUnmanaged(Char);
const Char = struct {
tex: gl.Texture,
size: @Vector(2, c_uint),
bearing: @Vector(2, c_int),
size: @Vector(2, f32),
bearing: @Vector(2, f32),
advance: c_uint,
};
@@ -82,22 +85,42 @@ pub fn init(alloc: std.mem.Allocator) !TextRenderer {
chars.appendAssumeCapacity(.{
.tex = tex,
.size = .{
face.*.glyph.*.bitmap.width,
face.*.glyph.*.bitmap.rows,
@intToFloat(f32, face.*.glyph.*.bitmap.width),
@intToFloat(f32, face.*.glyph.*.bitmap.rows),
},
.bearing = .{
face.*.glyph.*.bitmap_left,
face.*.glyph.*.bitmap_top,
@intToFloat(f32, face.*.glyph.*.bitmap_left),
@intToFloat(f32, face.*.glyph.*.bitmap_top),
},
.advance = @intCast(c_uint, face.*.glyph.*.advance.x),
});
}
// Configure VAO/VBO for glyph rendering
const vao = try gl.VertexArray.create();
const vbo = try gl.Buffer.create();
try vao.bind();
var binding = try vbo.bind(gl.c.GL_ARRAY_BUFFER);
try binding.setDataType([6 * 4]f32, gl.c.GL_DYNAMIC_DRAW);
try binding.enableVertexAttribArray(0);
try binding.vertexAttribPointer(0, 4, gl.c.GL_FLOAT, false, 4 * @sizeOf(f32), null);
binding.unbind();
try gl.VertexArray.unbind();
// Create our shader
const program = try gl.Program.createVF(
@embedFile("../shaders/text.v.glsl"),
@embedFile("../shaders/text.f.glsl"),
);
return TextRenderer{
.alloc = alloc,
.ft = ft,
.face = face,
.chars = chars,
.program = program,
.vao = vao,
.vbo = vbo,
};
}
@@ -113,4 +136,59 @@ pub fn deinit(self: *TextRenderer) void {
self.* = undefined;
}
pub fn render(
self: TextRenderer,
text: []const u8,
x: f32,
y: f32,
scale: f32,
color: @Vector(3, f32),
) !void {
try self.program.use();
try self.program.setUniform("textColor", color);
try gl.Texture.active(gl.c.GL_TEXTURE0);
try self.vao.bind();
std.log.info("---", .{});
var curx: f32 = x;
for (text) |c| {
const char = self.chars.items[c];
const xpos = curx + (char.bearing[0] * scale);
const ypos = y + (char.bearing[1] * scale);
const w = char.size[0] * scale;
const h = char.size[1] * scale;
std.log.info("CHARACTER INFO ch={} xpos={} ypos={} w={} h={}", .{
c,
xpos,
ypos,
w,
h,
});
const vert = [6][4]f32{
.{ xpos, ypos + h, 0.0, 0.0 },
.{ xpos, ypos, 0.0, 1.0 },
.{ xpos + w, ypos, 1.0, 1.0 },
.{ xpos, ypos + h, 0.0, 0.0 },
.{ xpos + w, ypos, 1.0, 1.0 },
.{ xpos + w, ypos + h, 1.0, 0.0 },
};
var texbind = try char.tex.bind(gl.c.GL_TEXTURE_2D);
defer texbind.unbind();
var bind = try self.vbo.bind(gl.c.GL_ARRAY_BUFFER);
try bind.setSubData(0, vert);
bind.unbind();
try gl.drawArrays(gl.c.GL_TRIANGLES, 0, 6);
curx += @intToFloat(f32, char.advance >> 6) * scale;
}
try gl.VertexArray.unbind();
}
const face_ttf = @embedFile("../fonts/Inconsolata-Regular.ttf");