WIP: Implement text rendering

This commit is contained in:
Dendy 2022-12-31 04:29:50 +01:00
parent 81d24ea62d
commit ebe6aa8d9a
5 changed files with 92 additions and 27 deletions

@ -1 +1 @@
Subproject commit 9fc2524bbf2e1172a5cb218eca37dc99930a31db
Subproject commit cac42e0692374742036f14d44379dd085c084caa

View File

@ -80,10 +80,10 @@ pub fn getSelIdx(self: Self, offset: i32) usize {
}
//ret = len - @mod(-offset - sel, len - 1);
//ret = @mod(0, 2);
std.debug.print("len {},offset {},ret {}, sel {}\n", .{ len, offset, ret, sel });
//std.debug.print("len {},offset {},ret {}, sel {}\n", .{ len, offset, ret, sel });
} else {
ret = @mod(offset + sel, len);
std.debug.print("len {},offset {},ret {}, sel {}\n", .{ len, offset, ret, sel });
//std.debug.print("len {},offset {},ret {}, sel {}\n", .{ len, offset, ret, sel });
}
return @intCast(usize, ret);

View File

@ -15,7 +15,7 @@ color: [4]f32,
test_counter: usize = 0,
vbo: [max_objects]f32 = .{0.0} ** max_objects,
colors: [max_objects][4]f32 = .{[4]f32{ 0.0, 0.0, 0.0, 1.0 }} ** max_objects,
//colors: [max_objects][4]f32 = .{[4]f32{ 0.0, 0.0, 0.0, 1.0 }} ** max_objects,
vbo_index: usize = 0,
const max_objects: usize = 16384;
@ -27,6 +27,7 @@ fn glGetProcAddress(p: []const u8, proc: [:0]const u8) ?*const anyopaque {
pub fn init() !Self {
try sdl.init(.{ .video = true, .audio = true, .events = true });
try sdl.ttf.init();
try sdl.gl.setAttribute(.{ .context_major_version = 3 });
try sdl.gl.setAttribute(.{ .context_minor_version = 3 });
@ -52,7 +53,6 @@ pub fn init() !Self {
// Shader stuff
var mvp_loc: u32 = undefined;
//var color_loc: u32 = undefined;
const program = gl.Program.create();
{
const vs = gl.Shader.create(.vertex);
@ -74,7 +74,6 @@ pub fn init() !Self {
program.link();
mvp_loc = program.uniformLocation("mvp").?;
//color_loc = program.uniformLocation("color").?;
}
program.use();
@ -95,18 +94,24 @@ pub fn init() !Self {
const buf = gl.Buffer.gen();
buf.bind(.array_buffer);
gl.vertexAttribPointer(0, 2, .float, false, @sizeOf(f32) * 6, @sizeOf(f32) * 0);
gl.vertexAttribPointer(0, 2, .float, false, @sizeOf(f32) * 8, @sizeOf(f32) * 0);
gl.enableVertexAttribArray(0);
gl.vertexAttribPointer(1, 4, .float, false, @sizeOf(f32) * 6, @sizeOf(f32) * 2);
gl.vertexAttribPointer(1, 4, .float, false, @sizeOf(f32) * 8, @sizeOf(f32) * 2);
gl.enableVertexAttribArray(1);
gl.vertexAttribPointer(2, 2, .float, false, @sizeOf(f32) * 8, @sizeOf(f32) * 6);
gl.enableVertexAttribArray(2);
gl.clearColor(0.91, 0.85, 0.65, 1.00);
// TODO: See if there's a more optimal solution to this, maybe activating only when necessary
gl.enable(.blend);
gl.blendFunc(.src_alpha, .one_minus_src_alpha);
var text = Text.init("testing123");
text.deinit();
return Self{ .window = window, .context = ctx, .buffer = buf, .color = .{ 0, 0, 0, 0 } };
}
@ -130,6 +135,7 @@ pub fn render(self: *Self) void {
pub fn deinit(self: *Self) void {
gl.disableVertexAttribArray(0);
sdl.quit();
sdl.ttf.quit();
self.window.destroy();
sdl.gl.deleteContext(self.context);
}
@ -158,7 +164,6 @@ pub fn drawRectangle(self: *Self, x: i32, y: i32, w: i32, h: i32) void {
}
pub fn fillRectangle(self: *Self, x: i32, y: i32, w: i32, h: i32) void {
//const indices = [_]u8{ 1, 3, 0, 1, 3, 2 };
self.fillRectangleEx(x, y, w, h, 0);
}
@ -166,8 +171,6 @@ pub fn fillRectangleEx(self: *Self, x: i32, y: i32, w: i32, h: i32, skew_x: i32)
// TODO: Is this still necessary?
self.test_counter += 1;
//gl.uniform4fv(self.color_loc, &.{self.color});
var xf = @intToFloat(f32, x);
var yf = @intToFloat(f32, y);
var wf = @intToFloat(f32, w);
@ -176,28 +179,28 @@ pub fn fillRectangleEx(self: *Self, x: i32, y: i32, w: i32, h: i32, skew_x: i32)
const skew_x_offset = @intToFloat(f32, skew_x) * hf / 100;
const i = self.vbo_index;
const vertex_data = [36]f32{
const vertex_data = [_]f32{
xf + skew_x_offset, yf, // up-left
self.color[0], self.color[1],
self.color[2], self.color[3],
self.color[0], self.color[1], self.color[2], self.color[3],
0.0, 0.0,
xf + wf + skew_x_offset, yf, // up-right
self.color[0], self.color[1],
self.color[2], self.color[3],
self.color[0], self.color[1], self.color[2], self.color[3],
1.0, 0.0,
xf - skew_x_offset, yf + hf, // down-left
self.color[0], self.color[1],
self.color[2], self.color[3],
self.color[0], self.color[1], self.color[2], self.color[3],
0.0, 1.0,
xf + wf - skew_x_offset, yf + hf, // down-right
self.color[0], self.color[1],
self.color[2], self.color[3],
self.color[0], self.color[1], self.color[2], self.color[3],
1.0, 1.0,
xf + wf + skew_x_offset, yf, // up-right
self.color[0], self.color[1],
self.color[2], self.color[3],
self.color[0], self.color[1], self.color[2], self.color[3],
1.0, 0.0,
xf - skew_x_offset, yf + hf, // down-left
self.color[0], self.color[1],
self.color[2], self.color[3],
self.color[0], self.color[1], self.color[2], self.color[3],
0.0, 1.0,
};
std.mem.copy(f32, self.vbo[i .. i + 36], &vertex_data);
self.vbo_index += 36;
std.mem.copy(f32, self.vbo[i .. i + vertex_data.len], &vertex_data);
self.vbo_index += vertex_data.len;
}
fn renderRectangle(self: *Self, x: i32, y: i32, w: i32, h: i32) void {
@ -212,3 +215,60 @@ pub fn getOutputSize(self: Self) OutputSize {
.height = wsize.height,
};
}
const image = [_]u8{
// 1
0,0,0,
255,255,255,
0,0,0,
255,255,255,
// 2
255,255,255,
0,0,0,
255,255,255,
0,0,0,
// 1
0,0,0,
255,255,255,
0,0,0,
255,255,255,
// 2
255,255,255,
0,0,0,
255,255,255,
0,0,0,
};
const Text = struct {
texture: gl.Texture,
pub fn init(text: [:0]const u8) Text {
var tex = gl.genTexture();
//defer gl.Texture.delete(tex);
gl.bindTexture(tex, .@"2d");
var font = sdl.ttf.openFont("res/fonts/MarginaliaRegular-8XlZ.ttf", 200) catch unreachable;
//defer font.close();
var surface = font.renderTextBlended(text, sdl.Color.white) catch unreachable;
const width = @intCast(usize, surface.ptr.w);
const height = @intCast(usize, surface.ptr.h);
var newsurf = sdl.c.SDL_ConvertSurfaceFormat(surface.ptr, @enumToInt(sdl.PixelFormatEnum.argb8888), 0) orelse unreachable;
const data = @ptrCast([*]const u8, newsurf.pixels);
//defer surface.destroy();
gl.textureImage2D(.@"2d", 0, .rgba, width, height, .rgba, .unsigned_byte, data);
gl.texParameter(.@"2d", .wrap_s, .clamp_to_edge);
gl.texParameter(.@"2d", .wrap_r, .clamp_to_edge);
gl.texParameter(.@"2d", .min_filter, .linear);
gl.texParameter(.@"2d", .mag_filter, .linear);
return Text {
.texture = tex,
};
}
pub fn deinit(self: *Text) void {
_ = self;
}
};

View File

@ -1,7 +1,9 @@
#version 330 core
in vec4 color;
in vec2 Texcoord;
uniform sampler2D tex;
void main(){
gl_FragColor = color;
gl_FragColor = texture(tex, Texcoord) * color;
}

View File

@ -2,12 +2,15 @@
layout (location = 0) in vec2 pos;
layout (location = 1) in vec4 v_color;
layout (location = 2) in vec2 texcoord;
out vec2 Texcoord;
uniform mat4 mvp;
out vec4 color;
void main() {
Texcoord = texcoord;
color = v_color;
gl_Position = mvp * vec4((pos + vec2(0.5, 0.5)), 0, 1);
}