draw text using ebo, some work to be done to clean that up

This commit is contained in:
Mitchell Hashimoto
2022-04-05 08:57:44 -07:00
parent 6ba0a13a9b
commit 14e510fe4f
3 changed files with 31 additions and 9 deletions

View File

@ -115,12 +115,16 @@ pub fn render(
const b = color[2]; const b = color[2];
const a: f32 = 1.0; const a: f32 = 1.0;
var vertices: std.ArrayListUnmanaged([6][9]f32) = .{}; var vertices: std.ArrayListUnmanaged([4][9]f32) = .{};
try vertices.ensureUnusedCapacity(self.alloc, text.len); try vertices.ensureUnusedCapacity(self.alloc, text.len);
defer vertices.deinit(self.alloc); defer vertices.deinit(self.alloc);
var indices: std.ArrayListUnmanaged([6]u32) = .{};
try indices.ensureUnusedCapacity(self.alloc, text.len);
defer indices.deinit(self.alloc);
var curx: f32 = x; var curx: f32 = x;
for (text) |c| { for (text) |c, i| {
if (ftgl.texture_font_get_glyph(self.font, &c)) |glyph_ptr| { if (ftgl.texture_font_get_glyph(self.font, &c)) |glyph_ptr| {
const glyph = glyph_ptr.*; const glyph = glyph_ptr.*;
const kerning = 0; // for now const kerning = 0; // for now
@ -137,17 +141,21 @@ pub fn render(
std.log.info("CHAR ch={} x0={} y0={} x1={} y1={}", .{ c, x0, y0, x1, y1 }); std.log.info("CHAR ch={} x0={} y0={} x1={} y1={}", .{ c, x0, y0, x1, y1 });
const vert = [6][9]f32{ const vert = [4][9]f32{
.{ x0, y0, 0, s0, t0, r, g, b, a }, .{ x0, y0, 0, s0, t0, r, g, b, a },
.{ x0, y1, 0, s0, t1, r, g, b, a }, .{ x0, y1, 0, s0, t1, r, g, b, a },
.{ x1, y1, 0, s1, t1, r, g, b, a }, .{ x1, y1, 0, s1, t1, r, g, b, a },
.{ x0, y0, 0, s0, t0, r, g, b, a },
.{ x1, y1, 0, s1, t1, r, g, b, a },
.{ x1, y0, 0, s1, t0, r, g, b, a }, .{ x1, y0, 0, s1, t0, r, g, b, a },
}; };
vertices.appendAssumeCapacity(vert); vertices.appendAssumeCapacity(vert);
const idx = @intCast(u32, 4 * i);
indices.appendAssumeCapacity([6]u32{
idx, idx + 1, idx + 2, // 0, 1, 2
idx, idx + 2, idx + 3, // 0, 2, 3
});
curx += glyph.advance_x; curx += glyph.advance_x;
} }
} }
@ -163,6 +171,8 @@ pub fn render(
const vao = try gl.VertexArray.create(); const vao = try gl.VertexArray.create();
defer vao.destroy(); defer vao.destroy();
try vao.bind(); try vao.bind();
// Array buffer
const vbo = try gl.Buffer.create(); const vbo = try gl.Buffer.create();
defer vbo.destroy(); defer vbo.destroy();
var binding = try vbo.bind(.ArrayBuffer); var binding = try vbo.bind(.ArrayBuffer);
@ -172,7 +182,15 @@ pub fn render(
try binding.attribute(1, 2, [9]f32, 3); try binding.attribute(1, 2, [9]f32, 3);
try binding.attribute(2, 4, [9]f32, 5); try binding.attribute(2, 4, [9]f32, 5);
try gl.drawArrays(gl.c.GL_TRIANGLES, 0, @intCast(c_int, vertices.items.len * 6)); // Element buffer
const ebo = try gl.Buffer.create();
defer ebo.destroy();
var ebobinding = try ebo.bind(.ElementArrayBuffer);
defer ebobinding.unbind();
try ebobinding.setData(indices.items, .DynamicDraw);
//try gl.drawArrays(gl.c.GL_TRIANGLES, 0, @intCast(c_int, vertices.items.len * 6));
try gl.drawElements(gl.c.GL_TRIANGLES, @intCast(c_int, indices.items.len * 6), gl.c.GL_UNSIGNED_INT, 0);
try gl.VertexArray.unbind(); try gl.VertexArray.unbind();
} }

View File

@ -9,6 +9,7 @@ id: c.GLuint,
/// Enum for possible binding targets. /// Enum for possible binding targets.
pub const Target = enum(c_uint) { pub const Target = enum(c_uint) {
ArrayBuffer = c.GL_ARRAY_BUFFER, ArrayBuffer = c.GL_ARRAY_BUFFER,
ElementArrayBuffer = c.GL_ELEMENT_ARRAY_BUFFER,
_, _,
}; };
@ -40,9 +41,6 @@ pub const Binding = struct {
usage: Usage, usage: Usage,
) !void { ) !void {
const info = dataInfo(data); const info = dataInfo(data);
std.log.info("SET DATA {}", .{
info.size,
});
c.glBufferData(@enumToInt(b.target), info.size, info.ptr, @enumToInt(usage)); c.glBufferData(@enumToInt(b.target), info.size, info.ptr, @enumToInt(usage));
try errors.getError(); try errors.getError();
} }

View File

@ -14,6 +14,12 @@ pub fn drawArrays(mode: c.GLenum, first: c.GLint, count: c.GLsizei) !void {
try errors.getError(); try errors.getError();
} }
pub fn drawElements(mode: c.GLenum, count: c.GLsizei, typ: c.GLenum, offset: usize) !void {
const offsetPtr = if (offset == 0) null else @intToPtr(*const anyopaque, offset);
c.glDrawElements(mode, count, typ, offsetPtr);
try errors.getError();
}
pub fn viewport(x: c.GLint, y: c.GLint, width: c.GLsizei, height: c.GLsizei) !void { pub fn viewport(x: c.GLint, y: c.GLint, width: c.GLsizei, height: c.GLsizei) !void {
c.glViewport(x, y, width, height); c.glViewport(x, y, width, height);
} }