Renderer general cleanup

This commit is contained in:
Dendy 2022-07-29 10:31:24 +02:00
parent a4cf3c6581
commit bdc73aad4b
1 changed files with 42 additions and 61 deletions

View File

@ -15,7 +15,6 @@ color: [4]f32,
pub fn init() !Self { pub fn init() !Self {
try sdl.init(.{ .video = true, .audio = true, .events = true }); try sdl.init(.{ .video = true, .audio = true, .events = true });
// Set OpenGL version
try sdl.gl.setAttribute(.{ .context_major_version = 3 }); try sdl.gl.setAttribute(.{ .context_major_version = 3 });
try sdl.gl.setAttribute(.{ .context_minor_version = 3 }); try sdl.gl.setAttribute(.{ .context_minor_version = 3 });
try sdl.gl.setAttribute(.{ .multisamplebuffers = true }); try sdl.gl.setAttribute(.{ .multisamplebuffers = true });
@ -27,43 +26,52 @@ pub fn init() !Self {
.{ .centered = {} }, .{ .centered = {} },
1280, 1280,
720, 720,
.{ .context = .opengl, .vis = .shown } .{ .context = .opengl, .vis = .shown },
); );
const ctx = try sdl.gl.createContext(window); const ctx = try sdl.gl.createContext(window);
sdl.gl.setSwapInterval( .immediate ) catch { sdl.gl.setSwapInterval(.immediate) catch {
std.debug.print("WARNING: Unable to configure the swap interval.\n", .{}); std.debug.print("WARNING: Unable to configure the swap interval.\n", .{});
}; };
// Shader stuff
var mvp_loc: u32 = undefined; var mvp_loc: u32 = undefined;
var color_loc: u32 = undefined; var color_loc: u32 = undefined;
const program = gl.Program.create(); const program = gl.Program.create();
{ {
const vs = gl.Shader.create(.vertex); const vs = gl.Shader.create(.vertex);
defer vs.delete(); defer vs.delete();
vs.source(1, &.{@embedFile("shaders/vector.vs")});
vs.compile();
const fs = gl.Shader.create(.fragment); const fs = gl.Shader.create(.fragment);
defer fs.delete(); defer fs.delete();
vs.source(1, &.{@embedFile("shaders/vector.vs")});
fs.source(1, &.{@embedFile("shaders/fragment.fs")}); fs.source(1, &.{@embedFile("shaders/fragment.fs")});
vs.compile();
fs.compile(); fs.compile();
program.attach(vs); program.attach(vs);
defer program.detach(vs); defer program.detach(vs);
program.attach(fs); program.attach(fs);
defer program.detach(fs); defer program.detach(fs);
program.link(); program.link();
mvp_loc = program.uniformLocation("mvp").?; mvp_loc = program.uniformLocation("mvp").?;
color_loc = program.uniformLocation("color").?; color_loc = program.uniformLocation("color").?;
} }
program.use(); program.use();
var wsize = window.getSize(); // Get the matrix that converts coordinates to 1:1 on the screen from the OpenGL default
var xunit = @intToFloat(f32, wsize.width); {
var yunit = @intToFloat(f32, wsize.height); const wsize = window.getSize();
const xunit = @intToFloat(f32, wsize.width);
const yunit = @intToFloat(f32, wsize.height);
const ortho = m.Mat4.createOrthogonal(0.0, xunit, yunit, 0.0, 0.0, 100.0); const ortho = m.Mat4.createOrthogonal(0.0, xunit, yunit, 0.0, 0.0, 100.0);
gl.uniformMatrix4fv(mvp_loc, false, &.{ ortho.fields }); gl.uniformMatrix4fv(mvp_loc, false, &.{ortho.fields});
}
var vertex_array = gl.VertexArray.gen(); var vertex_array = gl.VertexArray.gen();
vertex_array.bind(); vertex_array.bind();
@ -73,21 +81,15 @@ pub fn init() !Self {
gl.clearColor(0.91, 0.97, 1.00, 1.00); gl.clearColor(0.91, 0.97, 1.00, 1.00);
// TODO: See if there's a more optimal solution to this, maybe activating only when necessary
gl.enable(.blend); gl.enable(.blend);
gl.blendFunc(.src_alpha, .one_minus_src_alpha); gl.blendFunc(.src_alpha, .one_minus_src_alpha);
return Self{ return Self{ .window = window, .context = ctx, .color_loc = color_loc, .buffer = buf, .color = .{ 0, 0, 0, 0 } };
.window = window,
.context = ctx,
.color_loc = color_loc,
.buffer = buf,
.color = .{0,0,0,0}
};
} }
pub fn render(self: Self) void { pub fn render(self: Self) void {
sdl.gl.swapWindow(self.window); sdl.gl.swapWindow(self.window);
gl.clear(.{ .color = true }); gl.clear(.{ .color = true });
} }
@ -99,65 +101,44 @@ pub fn deinit(self: Self) void {
pub fn setColor(self: *Self, r: u8, g: i32, b: i32, a: i32) void { pub fn setColor(self: *Self, r: u8, g: i32, b: i32, a: i32) void {
self.*.color = .{ self.*.color = .{
@intToFloat(f32, r)/255.0, @intToFloat(f32, r) / 255.0,
@intToFloat(f32, g)/255.0, @intToFloat(f32, g) / 255.0,
@intToFloat(f32, b)/255.0, @intToFloat(f32, b) / 255.0,
@intToFloat(f32, a)/255.0, @intToFloat(f32, a) / 255.0,
}; };
} }
pub fn drawRectangle(self: *Self, x: i32, y: i32, w: i32, h: i32) void { pub fn drawRectangle(self: Self, x: i32, y: i32, w: i32, h: i32) void {
const indices = [_]u8{ 0, 1, 1, 2, 2, 3, 3, 0 };
self.renderRectangle(x, y, w, h, .lines, &indices);
}
pub fn fillRectangle(self: Self, x: i32, y: i32, w: i32, h: i32) void {
const indices = [_]u8{ 1, 3, 0, 1, 3, 2 };
self.renderRectangle(x, y, w, h, .triangles, &indices);
}
fn renderRectangle(self: Self, x: i32, y: i32, w: i32, h: i32, primitive: gl.PrimitiveType, indices: []const u8) void {
gl.uniform4fv(self.color_loc, &.{self.color});
var xf = @intToFloat(f32, x); var xf = @intToFloat(f32, x);
var yf = @intToFloat(f32, y); var yf = @intToFloat(f32, y);
var wf = @intToFloat(f32, w); var wf = @intToFloat(f32, w);
var hf = @intToFloat(f32, h); var hf = @intToFloat(f32, h);
gl.enableVertexAttribArray(0);
gl.vertexAttribPointer(0, 2, .float, false, 0, 0); gl.vertexAttribPointer(0, 2, .float, false, 0, 0);
const vertex_buffer = [_]f32{ const vertex_buffer = [_]f32{
xf, yf , xf , yf , // up-left
xf + wf, yf , xf + wf, yf , // up-right
xf + wf, yf + hf, xf + wf, yf + hf, // down-right
xf, yf + hf, xf , yf + hf, // down-left
}; };
const indices = [_]u8{0, 1, 1, 2, 2, 3, 3, 0};
self.buffer.data(f32, &vertex_buffer, .static_draw); self.buffer.data(f32, &vertex_buffer, .static_draw);
gl.uniform4fv(self.color_loc, &.{ self.color });
_ = indices;
gl.drawElements(.lines, 8, .u8, @ptrToInt(&indices));
gl.disableVertexAttribArray(0);
}
pub fn fillRectangle(self: *Self, x: i32, y: i32, w: i32, h: i32) void {
var xf = @intToFloat(f32, x);
var yf = @intToFloat(f32, y);
var wf = @intToFloat(f32, w);
var hf = @intToFloat(f32, h);
gl.enableVertexAttribArray(0); gl.enableVertexAttribArray(0);
gl.vertexAttribPointer(0, 2, .float, false, 0, 0); gl.drawElements(primitive, indices.len, .u8, @ptrToInt(indices.ptr));
const vertex_buffer = [_]f32{
xf + wf, yf ,
xf, yf + hf,
xf, yf ,
xf + wf, yf + hf,
};
const indices = [_]u8{0, 1, 2, 0, 1, 3};
self.buffer.data(f32, &vertex_buffer, .static_draw);
gl.uniform4fv(self.color_loc, &.{ self.color });
gl.drawElements(.triangles, 6, .u8, @ptrToInt(&indices));
gl.disableVertexAttribArray(0); gl.disableVertexAttribArray(0);
} }