Skip to content

Commit

Permalink
WIP basisu
Browse files Browse the repository at this point in the history
  • Loading branch information
ousttrue committed Oct 7, 2024
1 parent 38ca38d commit e684c29
Show file tree
Hide file tree
Showing 3 changed files with 141 additions and 1 deletion.
2 changes: 1 addition & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"name": "(Windows) Launch",
"type": "cppvsdbg",
"request": "launch",
"program": "${workspaceFolder}/zig-out/bin/vrm1.exe",
"program": "${workspaceFolder}/zig-out/bin/basisu.exe",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}/zig-out/web",
Expand Down
4 changes: 4 additions & 0 deletions deps/sokol_samples/build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,10 @@ pub const samples = [_]Sample{
.name = "draco",
.root_source_file = "extensions/draco/main.zig",
},
.{
.name = "basisu",
.root_source_file = "extensions/basisu/main.zig",
},
.{
.name = "vrm0",
.root_source_file = "extensions/vrm0/main.zig",
Expand Down
136 changes: 136 additions & 0 deletions deps/sokol_samples/extensions/basisu/main.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
const std = @import("std");
const sokol = @import("sokol");
const sg = sokol.gfx;

const zigltf = @import("zigltf");
const rowmath = @import("rowmath");
const framework = @import("framework");
const Scene = framework.Scene;
const gltf_fetcher = framework.gltf_fetcher;

const state = struct {
var pass_action = sg.PassAction{};
var input = rowmath.InputState{};
var orbit = rowmath.OrbitCamera{};
var gltf: ?std.json.Parsed(zigltf.Gltf) = null;
var scene = Scene{};
};

const load_file = "glTF-Sample-Assets/Models/StainedGlassLamp/glTF-KTX-BasisU/StainedGlassLamp.gltf";

export fn init() void {
sg.setup(.{
.environment = sokol.glue.environment(),
.logger = .{ .func = sokol.log.func },
});
sokol.gl.setup(.{
.logger = .{ .func = sokol.log.func },
});

var debugtext_desc = sokol.debugtext.Desc{
.logger = .{ .func = sokol.log.func },
};
debugtext_desc.fonts[0] = sokol.debugtext.fontOric();
sokol.debugtext.setup(debugtext_desc);

state.pass_action.colors[0] = .{
.load_action = .CLEAR,
.clear_value = .{ .r = 0.1, .g = 0.1, .b = 0.1, .a = 1.0 },
};

state.scene.init(std.heap.c_allocator);

gltf_fetcher.init(std.heap.c_allocator);
gltf_fetcher.fetch_gltf(load_file, &on_gltf) catch @panic("fetch_gltf");
}

fn on_gltf(gltf: std.json.Parsed(zigltf.Gltf), bin: std.StringHashMap([]const u8)) void {
state.gltf = gltf;
std.debug.print("{s}\n", .{gltf.value});
state.scene.load(gltf, bin) catch |e| {
std.debug.print("{s}\n", .{@errorName(e)});
@panic("Scene.load");
};
gltf_fetcher.state.status = "loaded";
}

export fn frame() void {
sokol.fetch.dowork();

state.input.screen_width = sokol.app.widthf();
state.input.screen_height = sokol.app.heightf();
state.orbit.frame(state.input);
state.input.mouse_wheel = 0;

sokol.debugtext.canvas(sokol.app.widthf() * 0.5, sokol.app.heightf() * 0.5);
sokol.debugtext.pos(0.5, 0.5);
sokol.debugtext.puts(gltf_fetcher.state.status);

sg.beginPass(.{
.action = state.pass_action,
.swapchain = sokol.glue.swapchain(),
});
state.scene.draw(state.orbit.camera);
sokol.debugtext.draw();
sg.endPass();
sg.commit();
}

export fn event(e: [*c]const sokol.app.Event) void {
switch (e.*.type) {
.MOUSE_DOWN => {
switch (e.*.mouse_button) {
.LEFT => {
state.input.mouse_left = true;
},
.RIGHT => {
state.input.mouse_right = true;
},
.MIDDLE => {
state.input.mouse_middle = true;
},
.INVALID => {},
}
},
.MOUSE_UP => {
switch (e.*.mouse_button) {
.LEFT => {
state.input.mouse_left = false;
},
.RIGHT => {
state.input.mouse_right = false;
},
.MIDDLE => {
state.input.mouse_middle = false;
},
.INVALID => {},
}
},
.MOUSE_MOVE => {
state.input.mouse_x = e.*.mouse_x;
state.input.mouse_y = e.*.mouse_y;
},
.MOUSE_SCROLL => {
state.input.mouse_wheel = e.*.scroll_y;
},
else => {},
}
}

export fn cleanup() void {
sg.shutdown();
}

pub fn main() void {
sokol.app.run(.{
.init_cb = init,
.frame_cb = frame,
.cleanup_cb = cleanup,
.event_cb = event,
.width = 800,
.height = 600,
.window_title = "rowmath: examples/sokol/camera_simple",
.icon = .{ .sokol_default = true },
.logger = .{ .func = sokol.log.func },
});
}

0 comments on commit e684c29

Please sign in to comment.