Skip to content

Commit

Permalink
refactor(examples): update zig-webgl demo
Browse files Browse the repository at this point in the history
  • Loading branch information
postspectacular committed Aug 17, 2024
1 parent 9dcc599 commit 9fdb61b
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 39 deletions.
3 changes: 0 additions & 3 deletions examples/zig-webgl/zig/fs.glsl

This file was deleted.

68 changes: 37 additions & 31 deletions examples/zig-webgl/zig/main.zig
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,12 @@ pub const WASM_ALLOCATOR = alloc.allocator();
var ctx: i32 = -1;
var model: i32 = -1;

/// Since various initialization functions can return errors
/// Main entry point
export fn start() void {
init() catch |e| @panic(@errorName(e));
}

/// Since (in general) various initialization tasks can return errors
/// we're bundling them all in a single fn, which is then called by start()
/// and so only needs one code site for error handling
fn init() !void {
Expand All @@ -41,19 +46,19 @@ fn init() !void {
});

const tex = gl.createTexture(ctx, &.{
.img = gl.textureDataU32(
&[_]u32{ 0xffffffff, 0xff808080, 0xff808080, 0xffffffff },
),
.imgType = .u8,
.width = 2,
.height = 2,
.wrap = .repeat,
.filter = .linear,
.imgType = .u8,
.img = .{
.u8 = gl.ConstU8Slice.wrap(std.mem.sliceAsBytes(&[_]u32{ 0xffffffff, 0xff808080, 0xff808080, 0xffffffff })),
},
});

const shader = gl.createShader(ctx, &.{
.vs = @embedFile("vs.glsl"),
.fs = @embedFile("fs.glsl"),
.vs = @embedFile("shaders/vs.glsl"),
.fs = @embedFile("shaders/fs.glsl"),
.attribs = gl.shaderAttribs(&.{
.{ .name = "position", .type = .vec2 },
.{ .name = "uv", .type = .vec2 },
Expand All @@ -74,42 +79,42 @@ fn init() !void {
model = gl.createModel(ctx, &.{
.shader = shader,
.attribs = gl.modelAttribs(&.{
gl.modelAttribFloat(
gl.modelAttribF32(
"position",
&[_]f32{ -1, -1, 1, -1, 0, 1 },
2,
0,
0,
&[_]f32{ -1, -1, 1, -1, 0, 1 },
),
gl.modelAttribFloat(
gl.modelAttribF32(
"uv",
&[_]f32{ 0, 0, 10, 0, 5, 10 },
2,
0,
0,
&[_]f32{ 0, 0, 10, 0, 5, 10 },
),
gl.modelAttribFloat(
gl.modelAttribF32(
"color",
&[_]f32{ 1, 0, 0, 0, 1, 0, 0, 0, 1 },
3,
0,
0,
&[_]f32{ 1, 0, 0, 0, 1, 0, 0, 0, 1 },
),
}),
.instances = gl.modelAttribs(&.{
gl.modelAttribFloat(
gl.modelAttribF32(
"scale",
&[_]f32{ 0.9, 0.3, 0.3 },
1,
0,
0,
&[_]f32{ 0.9, 0.3, 0.3 },
),
gl.modelAttribFloat(
gl.modelAttribF32(
"offset",
&[_]f32{ 0, 0, -0.6, 0.6, 0.6, 0.6 },
2,
0,
0,
&[_]f32{ 0, 0, -0.6, 0.6, 0.6, 0.6 },
),
}),
.uniforms = gl.modelUniforms(&.{
Expand All @@ -130,15 +135,21 @@ fn init() !void {

fn draw(_: ?*anyopaque) void {
const t: f32 = @floatCast(schedule.now() * 0.001);
gl.updateAttrib(model, "position", &.{
.data = .{ .f32 = gl.ConstF32Slice.wrap(&[_]f32{@sin(t * 2.0)}) },
.offset = 4,
});
gl.uniformVec3(model, "baseColor", &[_]f32{
@sin(t),
@sin(t + std.math.tau / 3.0),
@sin(t + std.math.tau * 2.0 / 3.0),
});
gl.updateAttribF32(
model,
"position",
&[_]f32{@sin(t * 3.0) * 0.5 + 0.5},
5 * 4,
);
gl.uniformVec3(
model,
"baseColor",
&[_]f32{
@sin(t),
@sin(t + std.math.tau / 3.0),
@sin(t + std.math.tau * 2.0 / 3.0),
},
);
gl.clear(ctx, 0.5, 0.5, 0.5, 1);
gl.draw(model);
requestRedraw();
Expand All @@ -147,8 +158,3 @@ fn draw(_: ?*anyopaque) void {
fn requestRedraw() void {
_ = schedule.schedule(.raf, 0, draw, null) catch return;
}

/// Main entry point
export fn start() void {
init() catch |e| @panic(@errorName(e));
}
5 changes: 5 additions & 0 deletions examples/zig-webgl/zig/shaders/fs.glsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// prelude, attributes, varyings & uniforms will be code generated
// see browser console for full generated shader
void main() {
fragColor = vec4(vcol + baseColor, 1.0) * texture(tex, vuv);
}
7 changes: 7 additions & 0 deletions examples/zig-webgl/zig/shaders/vs.glsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// prelude, attributes, varyings & uniforms will be code generated
// see browser console for full generated shader
void main() {
vcol = color;
vuv = uv;
gl_Position = vec4(position * scale + offset, 0.0, 1.0);
}
5 changes: 0 additions & 5 deletions examples/zig-webgl/zig/vs.glsl

This file was deleted.

0 comments on commit 9fdb61b

Please sign in to comment.