103 lines
2.5 KiB
Zig
103 lines
2.5 KiB
Zig
const std = @import("std");
|
|
const sokol = @import("sokol");
|
|
|
|
const sg = sokol.gfx;
|
|
|
|
const shader = @import("shaders/triangle2.glsl.zig");
|
|
|
|
var pass_action: sg.PassAction = .{};
|
|
|
|
const state = struct {
|
|
var bind: sg.Bindings = .{};
|
|
var pip: sg.Pipeline = .{};
|
|
var vsParams: shader.FsParams = .{ .offset = .{ 0, 0, 0 } };
|
|
var indexCount: u16 = 0;
|
|
};
|
|
|
|
export fn init() void {
|
|
sg.setup(.{
|
|
.logger = .{ .func = sokol.log.func },
|
|
.environment = sokol.glue.environment(),
|
|
});
|
|
|
|
sokol.time.setup();
|
|
|
|
std.log.info("Vertex buffers len: {}", .{state.bind.vertex_buffers.len});
|
|
|
|
var triangleVerts = [_]f32{
|
|
// positions colors
|
|
0.0, 0.5, 0.5, 1.0, 0.0, 0.0, 1.0,
|
|
0.5, -0.5, 0.5, 0.0, 1.0, 0.0, 1.0,
|
|
-0.5, -0.5, 0.5, 0.0, 0.0, 1.0, 1.0,
|
|
0.7, 0.5, 0.5, 0.0, 1.0, 0.0, 1.0,
|
|
};
|
|
|
|
state.bind.vertex_buffers[0] = sg.makeBuffer(.{
|
|
.data = sg.asRange(&triangleVerts),
|
|
});
|
|
|
|
const indexBuffer = [_]u16{
|
|
0, 1, 2,
|
|
0, 3, 1,
|
|
};
|
|
|
|
state.indexCount = indexBuffer.len;
|
|
|
|
state.bind.index_buffer = sg.makeBuffer(.{
|
|
.type = .INDEXBUFFER,
|
|
.data = sg.asRange(&indexBuffer),
|
|
});
|
|
|
|
var pip_desc: sg.PipelineDesc = .{
|
|
.index_type = .UINT16,
|
|
.shader = sg.makeShader(shader.triangleShaderDesc(sg.queryBackend())),
|
|
};
|
|
|
|
pip_desc.layout.attrs[0].format = .FLOAT3;
|
|
pip_desc.layout.attrs[1].format = .FLOAT4;
|
|
|
|
state.pip = sg.makePipeline(pip_desc);
|
|
|
|
pass_action.colors[0] = .{
|
|
.load_action = .CLEAR,
|
|
.clear_value = .{ .r = 0, .g = 1, .b = 0, .a = 1 },
|
|
};
|
|
|
|
std.log.info("Backend: {}\n", .{sg.queryBackend()});
|
|
}
|
|
|
|
fn timef() f32 {
|
|
return @floatCast(sokol.time.sec(sokol.time.now()));
|
|
}
|
|
|
|
export fn frame() void {
|
|
const col = &pass_action.colors[0].clear_value;
|
|
col.g = @abs(@sin(timef()));
|
|
col.r = @abs(@cos(timef()));
|
|
|
|
sg.beginPass(.{ .action = pass_action, .swapchain = sokol.glue.swapchain() });
|
|
sg.applyPipeline(state.pip);
|
|
state.vsParams.offset[0] = col.r;
|
|
sg.applyUniforms(.FS, shader.SLOT_fs_params, sg.asRange(&state.vsParams));
|
|
sg.applyBindings(state.bind);
|
|
sg.draw(0, state.indexCount, 1);
|
|
sg.endPass();
|
|
|
|
sg.commit();
|
|
}
|
|
|
|
export fn cleanup() void {}
|
|
|
|
pub fn main() !void {
|
|
sokol.app.run(.{
|
|
.init_cb = init,
|
|
.frame_cb = frame,
|
|
.cleanup_cb = cleanup,
|
|
.width = 800,
|
|
.height = 600,
|
|
.icon = .{ .sokol_default = true },
|
|
.window_title = "sokol hello",
|
|
.logger = .{ .func = sokol.log.func },
|
|
});
|
|
}
|