Skip to content

Commit

Permalink
simple_meshes
Browse files Browse the repository at this point in the history
  • Loading branch information
ousttrue committed Oct 5, 2024
1 parent 667e3f6 commit 838755b
Show file tree
Hide file tree
Showing 5 changed files with 333 additions and 69 deletions.
4 changes: 4 additions & 0 deletions deps/sokol_samples/build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,10 @@ pub const samples = [_]Sample{
.name = "animation",
.root_source_file = "tutorials/animation/main.zig",
},
.{
.name = "simple_meshes",
.root_source_file = "tutorials/simple_meshes/main.zig",
},
//
.{
.name = "glb",
Expand Down
225 changes: 225 additions & 0 deletions deps/sokol_samples/tutorials/simple_meshes/main.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,225 @@
const std = @import("std");
const sokol = @import("sokol");
const sg = sokol.gfx;

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

const minimal_gltf =
\\{
\\ "scene": 0,
\\ "scenes" : [
\\ {
\\ "nodes" : [ 0, 1]
\\ }
\\ ],
\\ "nodes" : [
\\ {
\\ "mesh" : 0
\\ },
\\ {
\\ "mesh" : 0,
\\ "translation" : [ 1.0, 0.0, 0.0 ]
\\ }
\\ ],
\\
\\ "meshes" : [
\\ {
\\ "primitives" : [ {
\\ "attributes" : {
\\ "POSITION" : 1,
\\ "NORMAL" : 2
\\ },
\\ "indices" : 0
\\ } ]
\\ }
\\ ],
\\
\\ "buffers" : [
\\ {
\\ "uri" : "data:application/octet-stream;base64,AAABAAIAAAAAAAAAAAAAAAAAAAAAAIA/AAAAAAAAAAAAAAAAAACAPwAAAAAAAAAAAAAAAAAAgD8AAAAAAAAAAAAAgD8AAAAAAAAAAAAAgD8=",
\\ "byteLength" : 80
\\ }
\\ ],
\\ "bufferViews" : [
\\ {
\\ "buffer" : 0,
\\ "byteOffset" : 0,
\\ "byteLength" : 6,
\\ "target" : 34963
\\ },
\\ {
\\ "buffer" : 0,
\\ "byteOffset" : 8,
\\ "byteLength" : 72,
\\ "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 ]
\\ },
\\ {
\\ "bufferView" : 1,
\\ "byteOffset" : 36,
\\ "componentType" : 5126,
\\ "count" : 3,
\\ "type" : "VEC3",
\\ "max" : [ 0.0, 0.0, 1.0 ],
\\ "min" : [ 0.0, 0.0, 1.0 ]
\\ }
\\ ],
\\
\\ "asset" : {
\\ "version" : "2.0"
\\ }
\\}
;

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{};
};

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);

// 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");
};

// build
state.scene.load(parsed, &.{}) catch |e| {
std.debug.print("{s}\n", .{@errorName(e)});
@panic("Scene.load");
};
}

export fn frame() void {
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(title);

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 = title,
.icon = .{ .sokol_default = true },
.logger = .{ .func = sokol.log.func },
});
}
44 changes: 23 additions & 21 deletions screenshot.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { chromium } from '@playwright/test';
import {
SAMPLES,
GROUPS,
} from './src/data';
import fs from 'node:fs';
import config from './vite.config.ts';
Expand All @@ -18,36 +18,38 @@ function resolve(src: string): string {
}

let i = 1;
for (const sample of SAMPLES) {
const url = host + resolve(`wasm/${sample.name}.html`)
++i;
for (const group of GROUPS) {
for (const sample of group.items) {
const url = host + resolve(`wasm/${sample.name}.html`)
++i;

const browser = await chromium.launch(); // or 'chromium', 'firefox'
const context = await browser.newContext();
const page = await context.newPage();
page.setViewportSize({ "width": 300, "height": 157 });
const browser = await chromium.launch(); // or 'chromium', 'firefox'
const context = await browser.newContext();
const page = await context.newPage();
page.setViewportSize({ "width": 300, "height": 157 });

try {
await page.goto(url);
await page.waitForLoadState('networkidle')
await page.screenshot({ path: `${dst}/wasm/${sample.name}.jpg` });
await browser.close();
} catch (ex) {
console.error(ex);
}
try {
await page.goto(url);
await page.waitForLoadState('networkidle')
await page.screenshot({ path: `${dst}/wasm/${sample.name}.jpg` });
await browser.close();
} catch (ex) {
console.error(ex);
}

// inject html to ogp
const path = `${dst}/wasm/${sample.name}.html`
if (fs.existsSync(path)) {
let src = fs.readFileSync(path, 'utf8');
fs.writeFileSync(path, src.replace('<meta charset=utf-8>', `<meta charset=utf-8>
// inject html to ogp
const path = `${dst}/wasm/${sample.name}.html`
if (fs.existsSync(path)) {
let src = fs.readFileSync(path, 'utf8');
fs.writeFileSync(path, src.replace('<meta charset=utf-8>', `<meta charset=utf-8>
<meta property="og:title" content="${sample.name}">
<meta property="og:type" content="website">
<meta property="og:url" content="https://ousttrue.github.io/rowmath/wasm/${sample.name}.html">
<meta property="og:image" content="https://ousttrue.github.io/rowmath/wasm/${sample.name}.jpg">
<meta property="og:site_name" content="rowmath wasm examples">
<meta property="og:description" content="${sample.name}">
`));
}
}
}

Expand Down
15 changes: 12 additions & 3 deletions src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { BrowserRouter, Routes, Route } from 'react-router-dom'
import { SAMPLES, type SampleType } from './data';
import { GROUPS, type ItemGroupType, type ItemType } from './data';
import "./App.css";
const BASE_URL = import.meta.env.BASE_URL;

function Sample(props: SampleType) {
function Item(props: ItemType) {
return (<div className="item">
<a href={`${BASE_URL}wasm/${props.name}.html`}>
{props.name}
Expand All @@ -23,13 +23,22 @@ function Sample(props: SampleType) {
</div>);
}

function Group(props: ItemGroupType) {
return (<>
<div className="item">
<a href={props.url}>{props.name}</a>
</div>
{props.items.map((props, i) => <Item key={i} {...props} />)}
</>);
}

function Home() {
return (<>
<div className="container">
<div className="item">
<a href="https://github.com/ousttrue/zigltf">github</a>
</div>
{SAMPLES.map((props, i) => <Sample key={i} {...props} />)}
{GROUPS.map((props, i) => <Group key={i} {...props} />)}
</div>
</>);
}
Expand Down
Loading

0 comments on commit 838755b

Please sign in to comment.