usg/src/Renderer.zig

146 lines
3.7 KiB
Zig
Raw Normal View History

const std = @import("std");
2022-07-22 15:23:12 +00:00
const sdl = @import("sdl2");
2022-07-25 13:32:25 +00:00
const zlm = @import("zlm");
2022-07-22 15:23:12 +00:00
const gl = @import("zgl");
2022-07-25 13:32:25 +00:00
const m = zlm.SpecializeOn(f32);
2022-07-22 15:23:12 +00:00
const Self = @This();
window: sdl.Window,
context: sdl.gl.Context,
color_loc: u32,
buffer: gl.Buffer,
2022-07-22 15:57:51 +00:00
color: [4]f32,
2022-07-22 15:23:12 +00:00
pub fn init() !Self {
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_minor_version = 3 });
2022-07-25 13:32:25 +00:00
try sdl.gl.setAttribute(.{ .multisamplebuffers = true });
try sdl.gl.setAttribute(.{ .multisamplesamples = 4 });
2022-07-22 15:23:12 +00:00
const window = try sdl.createWindow(
"USG",
.{ .centered = {} },
.{ .centered = {} },
1280,
720,
.{ .opengl = true, .shown = true }
);
const ctx = try sdl.gl.createContext(window);
2022-07-25 13:32:25 +00:00
var mvp_loc: u32 = undefined;
2022-07-22 15:23:12 +00:00
var color_loc: u32 = undefined;
const program = gl.Program.create();
{
const vs = gl.Shader.create(.vertex);
defer vs.delete();
vs.source(1, &.{@embedFile("shaders/vector.vs")});
vs.compile();
const fs = gl.Shader.create(.fragment);
defer fs.delete();
fs.source(1, &.{@embedFile("shaders/fragment.fs")});
fs.compile();
program.attach(vs);
defer program.detach(vs);
program.attach(fs);
defer program.detach(fs);
program.link();
2022-07-25 13:32:25 +00:00
mvp_loc = program.uniformLocation("mvp").?;
2022-07-22 15:23:12 +00:00
color_loc = program.uniformLocation("color").?;
}
program.use();
2022-07-25 13:32:25 +00:00
var wsize = window.getSize();
var xunit = @intToFloat(f32, wsize.width);
var yunit = @intToFloat(f32, wsize.height);
const ortho = m.Mat4.createOrthogonal(0.0, xunit, yunit, 0.0, 0.0, 100.0);
gl.uniformMatrix4fv(mvp_loc, false, &.{ ortho.fields });
2022-07-22 15:23:12 +00:00
var vertex_array = gl.VertexArray.gen();
vertex_array.bind();
const buf = gl.Buffer.gen();
buf.bind(.array_buffer);
gl.clearColor(0.91, 0.97, 1.00, 1.00);
return Self{
.window = window,
.context = ctx,
.color_loc = color_loc,
.buffer = buf,
2022-07-22 15:57:51 +00:00
.color = .{0,0,0,0}
2022-07-22 15:23:12 +00:00
};
}
pub fn render(self: Self) void {
2022-07-22 15:57:51 +00:00
sdl.gl.swapWindow(self.window);
2022-07-22 15:23:12 +00:00
gl.clear(.{ .color = true });
2022-07-22 15:57:51 +00:00
}
pub fn deinit(self: Self) void {
sdl.quit();
self.window.destroy();
sdl.gl.deleteContext(self.context);
}
2022-07-25 13:32:25 +00:00
pub fn setColor(self: *Self, r: u8, g: i32, b: i32, a: i32) void {
2022-07-22 15:57:51 +00:00
self.*.color = .{
@intToFloat(f32, r)/255.0,
@intToFloat(f32, g)/255.0,
@intToFloat(f32, b)/255.0,
@intToFloat(f32, a)/255.0,
};
}
2022-07-22 15:23:12 +00:00
2022-07-25 13:32:25 +00:00
pub fn drawRectangle(self: *Self, x: i32, y: i32, w: i32, h: i32) void {
_ = self;
_ = x;
_ = y;
_ = w;
_ = h;
}
2022-07-25 13:32:25 +00:00
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);
2022-07-22 15:23:12 +00:00
gl.enableVertexAttribArray(0);
gl.vertexAttribPointer(0, 3, .float, false, 0, 0);
2022-07-22 15:57:51 +00:00
const vertex_buffer = [_]f32{
2022-07-25 13:32:25 +00:00
xf, yf , 0.0,
xf + wf, yf , 0.0,
xf, yf + hf, 0.0,
xf + wf, yf + hf, 0.0,
xf + wf, yf , 0.0,
xf, yf + hf, 0.0,
2022-07-22 15:57:51 +00:00
};
2022-07-22 15:57:51 +00:00
self.buffer.data(f32, &vertex_buffer, .static_draw);
2022-07-22 15:23:12 +00:00
2022-07-22 15:57:51 +00:00
gl.uniform4fv(self.color_loc, &.{ self.color });
gl.drawArrays(.triangles, 0, 3);
2022-07-25 13:32:25 +00:00
gl.drawArrays(.triangles, 3, 6);
2022-07-22 15:23:12 +00:00
gl.disableVertexAttribArray(0);
2022-07-25 13:32:25 +00:00
}
2022-07-22 15:23:12 +00:00
2022-07-25 13:32:25 +00:00
pub const OutputSize = struct { width: c_int, height: c_int };
pub fn getOutputSize(self: Self) OutputSize {
var wsize = self.window.getSize();
return OutputSize{
.width = wsize.width,
.height = wsize.height,
};
2022-07-22 15:23:12 +00:00
}