Skip to content

Commit

Permalink
add samples
Browse files Browse the repository at this point in the history
  • Loading branch information
ousttrue committed Sep 24, 2024
1 parent 5d6d509 commit ed14696
Show file tree
Hide file tree
Showing 7 changed files with 200 additions and 100 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ wasm build.
- [x] node
- [x] texture
- [ ] animation
- [ ] glTF
- [ ] draco
- [ ] basisu
- [ ] vrm-0.x
Expand Down
8 changes: 4 additions & 4 deletions deps/sokol_samples/build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -152,11 +152,11 @@ pub const samples = [_]Sample{
.root_source_file = "minimal/main.zig",
},
.{
.name = "CesiumMilkTruck",
.root_source_file = "CesiumMilkTruck/main.zig",
.name = "glb",
.root_source_file = "glb/main.zig",
},
.{
.name = "draco_bunny",
.root_source_file = "draco_bunny//main.zig",
.name = "draco",
.root_source_file = "draco/main.zig",
},
};
Original file line number Diff line number Diff line change
Expand Up @@ -5,83 +5,8 @@ const sg = sokol.gfx;
const zigltf = @import("zigltf");
const rowmath = @import("rowmath");
const framework = @import("framework");
// const utils = @import("utils");
const Scene = framework.Scene;
// const gltf_fetcher = @import("gltf_fetcher.zig");

// https://github.khronos.org/glTF-Tutorials/gltfTutorial/gltfTutorial_003_MinimalGltfFile.html
const minimal_gltf =
\\{
\\ "scene": 0,
\\ "scenes" : [
\\ {
\\ "nodes" : [ 0 ]
\\ }
\\ ],
\\
\\ "nodes" : [
\\ {
\\ "mesh" : 0
\\ }
\\ ],
\\
\\ "meshes" : [
\\ {
\\ "primitives" : [ {
\\ "attributes" : {
\\ "POSITION" : 1
\\ },
\\ "indices" : 0
\\ } ]
\\ }
\\ ],
\\
\\ "buffers" : [
\\ {
\\ "uri" : "data:application/octet-stream;base64,AAABAAIAAAAAAAAAAAAAAAAAAAAAAIA/AAAAAAAAAAAAAAAAAACAPwAAAAA=",
\\ "byteLength" : 44
\\ }
\\ ],
\\ "bufferViews" : [
\\ {
\\ "buffer" : 0,
\\ "byteOffset" : 0,
\\ "byteLength" : 6,
\\ "target" : 34963
\\ },
\\ {
\\ "buffer" : 0,
\\ "byteOffset" : 8,
\\ "byteLength" : 36,
\\ "target" : 34962
\\ }
\\ ],
\\ "accessors" : [
\\ {
\\ "bufferView" : 0,
\\ "byteOffset" : 0,
\\ "componentType" : 5123,
\\ "count" : 3,
\\ "type" : "SCALAR",
\\ "max" : [ 2 ],
\\ "min" : [ 0 ]
\\ },
\\ {
\\ "bufferView" : 1,
\\ "byteOffset" : 0,
\\ "componentType" : 5126,
\\ "count" : 3,
\\ "type" : "VEC3",
\\ "max" : [ 1.0, 1.0, 0.0 ],
\\ "min" : [ 0.0, 0.0, 0.0 ]
\\ }
\\ ],
\\
\\ "asset" : {
\\ "version" : "2.0"
\\ }
\\}
;
const gltf_fetcher = framework.gltf_fetcher;

const state = struct {
var pass_action = sg.PassAction{};
Expand All @@ -91,6 +16,8 @@ const state = struct {
var scene = Scene{};
};

const load_file = "glTF-Sample-Assets/Models/CesiumMilkTruck/glTF-Draco/CesiumMilkTruck.gltf";

export fn init() void {
sg.setup(.{
.environment = sokol.glue.environment(),
Expand All @@ -113,36 +40,31 @@ export fn init() void {

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

// parse gltf
const allocator = std.heap.c_allocator;
const parsed = std.json.parseFromSlice(
zigltf.Gltf,
allocator,
minimal_gltf,
.{
.ignore_unknown_fields = true,
},
) catch |e| {
std.debug.print("{s}\n", .{@errorName(e)});
@panic("parseFromSlice");
};
gltf_fetcher.init(std.heap.c_allocator);
gltf_fetcher.fetch_gltf(load_file, &on_gltf) catch @panic("fetch_gltf");
}

// build
state.scene.load(parsed, &.{}) catch |e| {
fn on_gltf(gltf: std.json.Parsed(zigltf.Gltf), bin:?[]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("minimal_gltf");
sokol.debugtext.puts(gltf_fetcher.state.status);

sg.beginPass(.{
.action = state.pass_action,
Expand Down
File renamed without changes.
136 changes: 136 additions & 0 deletions deps/sokol_samples/gltf/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/CesiumMilkTruck/glTF/CesiumMilkTruck.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:?[]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 },
});
}
9 changes: 9 additions & 0 deletions src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,15 @@ function Sample(props: SampleType) {
<img width={150} height={78} src={`${BASE_URL}wasm/${props.name}.jpg`} />
</figure>
</a>

<ul>
{props.links.map((link, i) => (
<li key={i}>
<a href={link.url}>
{link.name}
</a>
</li>))}
</ul>
</div>);
}

Expand Down
40 changes: 36 additions & 4 deletions src/data.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,48 @@
export type SampleType = {
export type LinkType = {
name: string,
url: string,
};
export type SampleType = {
name: string,
links: LinkType[],
};

export const SAMPLES: SampleType[] = [
{
name: "minimal",
url: "https://github.khronos.org/glTF-Tutorials/gltfTutorial/gltfTutorial_003_MinimalGltfFile.html",
links: [
{
name: 'A Minimal glTF File - glTF-Tutorials',
url: "https://github.khronos.org/glTF-Tutorials/gltfTutorial/gltfTutorial_003_MinimalGltfFile.html",
},
],
},
{
name: "glb",
links: [
{
name: 'glb',
url: "https://registry.khronos.org/glTF/specs/2.0/glTF-2.0.html#glb-file-format-specification",
},
],
},
{
name: "gltf",
links: [
{
name: 'gltf',
url: "https://registry.khronos.org/glTF/specs/2.0/glTF-2.0.html",
},
],
},
{
name: "CesiumMilkTruck",
url: "https://github.com/KhronosGroup/glTF-Sample-Assets/tree/main/Models/CesiumMilkTruck/glTF-Binary",
name: "draco",
links: [
{
name: 'draco - github',
url: "https://github.com/KhronosGroup/glTF/blob/main/extensions/2.0/Khronos/KHR_draco_mesh_compression/README.md",
},
],
},
];

0 comments on commit ed14696

Please sign in to comment.