Initial Commit

This commit is contained in:
SoraKatadzuma committed 2026-10-30 13:52:09 -05:00
commit aa254a2a0a
16 files changed
+2405

No files matched your search

+113
View File
@@ -0,0 +1,113 @@
#! Contains the tests of reading accessors: integers of each size, normalized
#! or not, interleaved in a view by its stride; a matrix of bytes, each column
#! begun at a multiple of 4; and sparse values, over a view's or over zeros.
import std::io;
import std::testing;
import gltf;
# A buffer of 68 bytes, its views as `accessors_json` names them.
def accessors_buffer(): vector<u8> {
var made := packed::new();
# View 0, interleaved by 8: a color of normalized bytes, and two normalized
# shorts, for each of two elements.
for each in [255u, 0, 128, 255] { made.byte(each as u8); }
made.short(32767);
made.short(0x8000);
for each in [0u, 51, 255, 0] { made.byte(each as u8); }
made.short(0);
made.short(0xC000);
# View 1: signed bytes, -3 and 7.
made.byte(253);
made.byte(7);
made.align();
# View 2: a matrix of two columns of two bytes, each column padded to 4.
for each in [1u, 2, 0, 0, 3, 4, 0, 0] { made.byte(each as u8); }
# View 3: four floats; views 4 and 5: sparse indices 3 and 1, and their values.
val four: f32[4] = [10, 20, 30, 40];
made.floats(four[:]);
made.byte(3);
made.byte(1);
made.align();
val two: f32[2] = [-1, -2];
made.floats(two[:]);
# View 6: a sparse value of two floats; view 7: its index, 2, a short.
val pair: f32[2] = [5, 6];
made.floats(pair[:]);
made.short(2);
made.align();
return move(made.bytes);
}
def accessors_json(uri: strview): string {
var made := string::from("""
{
"asset": { "version": "2.0" },
"accessors": [
{ "bufferView": 0, "componentType": 5121, "normalized": true, "count": 2, "type": "VEC4" },
{ "bufferView": 0, "byteOffset": 4, "componentType": 5122, "normalized": true, "count": 2, "type": "VEC2" },
{ "bufferView": 1, "componentType": 5120, "count": 2, "type": "SCALAR" },
{ "bufferView": 2, "componentType": 5121, "count": 1, "type": "MAT2" },
{ "bufferView": 3, "componentType": 5126, "count": 4, "type": "SCALAR",
"sparse": { "count": 2, "indices": { "bufferView": 4, "componentType": 5121 }, "values": { "bufferView": 5 } } },
{ "componentType": 5126, "count": 3, "type": "VEC2",
"sparse": { "count": 1, "indices": { "bufferView": 7, "componentType": 5123 }, "values": { "bufferView": 6 } } },
{ "bufferView": 7, "componentType": 5123, "count": 1, "type": "SCALAR" }
],
"bufferViews": [
{ "buffer": 0, "byteOffset": 0, "byteLength": 16, "byteStride": 8 },
{ "buffer": 0, "byteOffset": 16, "byteLength": 2 },
{ "buffer": 0, "byteOffset": 20, "byteLength": 8 },
{ "buffer": 0, "byteOffset": 28, "byteLength": 16 },
{ "buffer": 0, "byteOffset": 44, "byteLength": 2 },
{ "buffer": 0, "byteOffset": 48, "byteLength": 8 },
{ "buffer": 0, "byteOffset": 56, "byteLength": 8 },
{ "buffer": 0, "byteOffset": 64, "byteLength": 2 }
],
"buffers": [{ "byteLength": 68
""", nothing{});
made.append(", \"uri\": \"");
made.append(uri);
made.append("\" }] }");
return made;
}
# The accessors' asset, read.
def read_accessors(t: &mut tester): maybe<gltf::asset> {
val bytes := accessors_buffer();
t.expects(bytes.length == 68);
val uri := data_uri(bytes.view());
val json := accessors_json(uri.view());
match gltf::decode(json.view()) {
value(read) => return read;
error(e) => {
io::println(e.describe().view());
t.expects(false);
return nothing{};
}
}
}
@test def reads_numbers_of_every_size_interleaved_by_stride(t: &mut tester): void {
val read := read_accessors(t) else { return; };
val colors := read.floats(0);
t.expects(colors.length == 8 && near(colors[0], 1) && near(colors[1], 0) && near(colors[2], 128.0 / 255) && near(colors[3], 1));
t.expects(near(colors[4], 0) && near(colors[5], 0.2) && near(colors[6], 1) && near(colors[7], 0));
# A normalized short's least is -1, as -32767 is.
val shorts := read.floats(1);
t.expects(shorts.length == 4 && near(shorts[0], 1) && near(shorts[1], -1) && near(shorts[2], 0) && near(shorts[3], -16384.0 / 32767));
val signed := read.floats(2);
t.expects(signed.length == 2 && near(signed[0], -3) && near(signed[1], 7));
val matrix := read.floats(3);
t.expects(matrix.length == 4 && near(matrix[0], 1) && near(matrix[1], 2) && near(matrix[2], 3) && near(matrix[3], 4));
val index := read.integers(6);
t.expects(index.length == 1 && index[0] == 2);
}
@test def puts_sparse_values_in_place(t: &mut tester): void {
val read := read_accessors(t) else { return; };
val over_view := read.floats(4);
t.expects(over_view.length == 4 && near(over_view[0], 10) && near(over_view[1], -2) && near(over_view[2], 30) && near(over_view[3], -1));
val over_zeros := read.floats(5);
t.expects(over_zeros.length == 6 && near(over_zeros[0], 0) && near(over_zeros[3], 0) && near(over_zeros[4], 5) && near(over_zeros[5], 6));
}
+10
View File
@@ -0,0 +1,10 @@
#! Contains the `main` that runs gltf's tests, each in a copy of the program of
#! its own. `blossom test` runs them, and `-- --only TEXT` those whose name
#! holds the text.
import system;
import std::testing;
def main(args: span<strview>): void {
exit(tester::run(args));
}
+119
View File
@@ -0,0 +1,119 @@
#! Contains the tests of the tree of nodes: a matrix taken apart into a
#! translation, rotation, and scale, one mirroring, those put together, and
#! where a child is in the world; and the cameras, lights, skins, and
#! animations nodes name.
import std::io;
import std::testing;
import linear;
import gltf;
# A buffer of two identity matrices, two times, and two translations.
def nodes_buffer(): vector<u8> {
var made := packed::new();
val identity: f32[16] = [1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1];
made.floats(identity[:]);
made.floats(identity[:]);
val times: f32[2] = [0, 1];
made.floats(times[:]);
val places: f32[6] = [0, 0, 3, 0, 1, 3];
made.floats(places[:]);
return move(made.bytes);
}
# A root turned a quarter about y, scaled by 2, and moved 1 along x, as a
# matrix; a child of it 3 along z, as a translation, with a camera and a spot
# light; a node mirrored in x; and one with a skin.
def nodes_json(uri: strview): string {
var made := string::from("""
{
"asset": { "version": "2.0" },
"extensionsUsed": ["KHR_lights_punctual"],
"extensions": { "KHR_lights_punctual": { "lights": [
{ "name": "lamp", "type": "spot", "color": [1, 0.5, 0], "intensity": 20, "range": 8, "spot": { "outerConeAngle": 0.5 } }
] } },
"scenes": [{ "nodes": [0, 2, 3] }],
"nodes": [
{ "name": "Root", "children": [1], "matrix": [0, 0, -2, 0, 0, 2, 0, 0, 2, 0, 0, 0, 1, 0, 0, 1] },
{ "name": "Child", "translation": [0, 0, 3], "camera": 0, "extensions": { "KHR_lights_punctual": { "light": 0 } } },
{ "name": "Mirror", "matrix": [-1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1] },
{ "name": "Skinned", "skin": 0 }
],
"cameras": [
{ "type": "perspective", "perspective": { "yfov": 0.8, "znear": 0.1 } },
{ "type": "orthographic", "orthographic": { "xmag": 2, "ymag": 1, "znear": 0, "zfar": 10 } }
],
"skins": [{ "joints": [0, 1], "inverseBindMatrices": 0 }],
"animations": [{ "name": "rise",
"channels": [{ "sampler": 0, "target": { "node": 1, "path": "translation" } }],
"samplers": [{ "input": 1, "output": 2, "interpolation": "STEP" }]
}],
"accessors": [
{ "bufferView": 0, "componentType": 5126, "count": 2, "type": "MAT4" },
{ "bufferView": 1, "componentType": 5126, "count": 2, "type": "SCALAR" },
{ "bufferView": 2, "componentType": 5126, "count": 2, "type": "VEC3" }
],
"bufferViews": [
{ "buffer": 0, "byteOffset": 0, "byteLength": 128 },
{ "buffer": 0, "byteOffset": 128, "byteLength": 8 },
{ "buffer": 0, "byteOffset": 136, "byteLength": 24 }
],
"buffers": [{ "byteLength": 160
""", nothing{});
made.append(", \"uri\": \"");
made.append(uri);
made.append("\" }] }");
return made;
}
def read_nodes(t: &mut tester): maybe<gltf::asset> {
val bytes := nodes_buffer();
val uri := data_uri(bytes.view());
val json := nodes_json(uri.view());
match gltf::decode(json.view()) {
value(read) => return read;
error(e) => {
io::println(e.describe().view());
t.expects(false);
return nothing{};
}
}
}
@test def takes_matrices_apart_and_puts_transforms_together(t: &mut tester): void {
val read := read_nodes(t) else { return; };
val root := &read.nodes[0];
t.expects(near(root.translation.x, 1) && near(root.translation.z, 0));
t.expects(near(root.scale.x, 2) && near(root.scale.y, 2) && near(root.scale.z, 2));
# A quarter turn about y.
t.expects(near(root.rotation.x, 0) && near(root.rotation.y, 0.70710677) && near(root.rotation.z, 0) && near(root.rotation.w, 0.70710677));
val child := &read.nodes[1];
t.expects(child.parent is some(p) && p == 0 && near(child.matrix.m[14], 3) && root.parent is none);
# Its place, 3 along z, turned to x, doubled, and moved 1 along x.
val world := read.world_matrix(1);
t.expects(near(world.m[12], 7) && near(world.m[13], 0) && near(world.m[14], 0));
val mirror := &read.nodes[2];
t.expects(near(mirror.scale.x, -1) && near(mirror.scale.y, 1) && near(mirror.rotation.w, 1));
val roots := read.roots();
t.expects(roots.length == 3 && roots[0] == 0 && roots[1] == 2 && roots[2] == 3);
}
@test def reads_cameras_lights_skins_and_animations(t: &mut tester): void {
val read := read_nodes(t) else { return; };
t.expects(read.cameras.length == 2 && read.nodes[1].camera is some(c) && c == 0);
val eye := &read.cameras[0];
t.expects(eye.kind == gltf::camera_kind::perspective && near(eye.fov_y, 0.8) && eye.aspect is none && eye.far is none);
val plan := &read.cameras[1];
t.expects(plan.kind == gltf::camera_kind::orthographic && near(plan.x_mag, 2) && plan.far is some(f) && near(f, 10));
t.expects(read.lights.length == 1 && read.nodes[1].light is some(l) && l == 0);
val lamp := &read.lights[0];
t.expects(lamp.kind == gltf::light_kind::spot && lamp.name.view() == "lamp" && near(lamp.color[1], 0.5) && near(lamp.intensity, 20));
t.expects(lamp.range is some(r) && near(r, 8) && near(lamp.inner_cone, 0) && near(lamp.outer_cone, 0.5));
t.expects(read.skins.length == 1 && read.skins[0].joints.length == 2 && read.nodes[3].skin is some(s) && s == 0);
t.expects(read.floats(0).length == 32);
val rise := &read.animations[0];
t.expects(rise.name.view() == "rise" && rise.channels.length == 1 && rise.channels[0].path == gltf::target_path::translation);
t.expects(rise.channels[0].node is some(n) && n == 1 && rise.samplers[0].interpolation == gltf::interpolation::step);
val keys := read.floats(rise.samplers[0].output);
t.expects(keys.length == 6 && near(keys[4], 1));
}
+186
View File
@@ -0,0 +1,186 @@
#! Contains the tests of reading assets: from `.gltf` JSON with its buffer in a
#! `data:` URI, from a `.glb` file, and from files beside it; and what's refused,
#! each with why.
import std::io;
import std::testing;
import linear;
import gltf;
# Checks what the quad holds, read any way.
def expect_quad(t: &mut tester, read: &gltf::asset): void {
t.expects(read.version.view() == "2.0" && read.generator.view() == "gltf tests");
t.expects(read.scene is some(s) && s == 0 && read.scenes.length == 1 && read.scenes[0].name.view() == "Quad");
t.expects(read.nodes.length == 1 && read.nodes[0].name.view() == "Square" && read.nodes[0].mesh is some(m) && m == 0);
t.expects(near(read.nodes[0].translation.y, 2) && near(read.nodes[0].matrix.m[14], 3));
val part := &read.meshes[0].primitives[0];
t.expects(part.mode == gltf::topology::triangles && part.find("POSITION") is some(p) && p == 0 && part.find("TEXCOORD_0") is none);
val places := read.floats(0);
t.expects(places.length == 12 && near(places[3], 0.5) && near(places[7], 0.5));
val normals := read.floats(1);
t.expects(normals.length == 12 && near(normals[2], 1));
val indices := read.integers(2);
t.expects(indices.length == 6 && indices[4] == 2 && indices[5] == 3);
val red := &read.materials[0];
t.expects(red.name.view() == "red" && red.double_sided && near(red.base_color[0], 1) && near(red.base_color[1], 0));
t.expects(near(red.metallic, 0) && near(red.roughness, 1) && red.alpha_mode == gltf::alpha_mode::opaque && red.base_color_texture is none);
t.expects(read.accessors[0].max.length == 3 && read.accessors[0].max[0] == 0.5);
}
@test def reads_json_with_its_buffer_in_a_data_uri(t: &mut tester): void {
val bytes := quad_buffer();
val uri := data_uri(bytes.view());
val json := quad_json(uri.view());
val read := gltf::decode(json.view()) else (e) {
t.expects(false);
io::println(e.describe().view());
return;
};
expect_quad(t, &read);
}
@test def reads_glb_files(t: &mut tester): void {
val json := quad_json("");
val bytes := quad_buffer();
val glb := glb_of(json.view(), bytes.view());
put("quad.glb", glb.view());
val read := gltf::load(".blossom/gltf tests/quad.glb") else (e) {
t.expects(false);
io::println(e.describe().view());
return;
};
expect_quad(t, &read);
t.expects(read.buffers[0].uri.length == 0 && read.buffers[0].bytes.length >= 108);
}
# A buffer and an image as files beside the asset, the buffer's named with a
# space, percent-encoded, and an image in a buffer view of a `.glb` file.
@test def reads_buffers_and_images_from_files_beside_it(t: &mut tester): void {
val bytes := quad_buffer();
put("beside/quad data.bin", bytes.view());
val picture: u8[8] = [0x89, 'P', 'N', 'G', 13, 10, 26, 10];
put("beside/red.png", picture[:]);
val json := quad_json("quad%20data.bin", ", \"images\": [{ \"uri\": \"red.png\" }], \"textures\": [{ \"source\": 0 }]");
put("beside/quad.gltf", json.view());
val read := gltf::load(".blossom/gltf tests/beside/quad.gltf") else (e) {
t.expects(false);
io::println(e.describe().view());
return;
};
expect_quad(t, &read);
t.expects(read.images.length == 1 && read.images[0].bytes.length == 8 && read.images[0].bytes[1] == 'P');
t.expects(read.textures[0].source is some(s) && s == 0 && read.textures[0].sampler is none);
# The same image, in a view of a `.glb` file's buffer, after the quad's.
var packed_bytes := quad_buffer();
packed_bytes.extend(picture[:]);
val inner := quad_json("", ", \"images\": [{ \"bufferView\": 2, \"mimeType\": \"image/png\" }]");
# Its buffer is the quad's and the image's: 116 bytes, the image a third view.
val with_image := replaced(replaced(inner.view(), "\"byteLength\": 108", "\"byteLength\": 116").view(),
"{ \"buffer\": 0, \"byteOffset\": 96, \"byteLength\": 12 }",
"{ \"buffer\": 0, \"byteOffset\": 96, \"byteLength\": 12 }, { \"buffer\": 0, \"byteOffset\": 108, \"byteLength\": 8 }");
val glb := glb_of(with_image.view(), packed_bytes.view());
val bound := gltf::decode(glb.view()) else (e) {
t.expects(false);
io::println(e.describe().view());
return;
};
t.expects(bound.images[0].mime_type.view() == "image/png" && bound.images[0].bytes.length == 8 && bound.images[0].bytes[3] == 'G');
}
# Two texts, one after the other.
def joined(a: strview, b: strview): string {
var made := string::from(a, nothing{});
made.append(b);
return made;
}
# Text with the first of some text replaced.
def replaced(text: strview, what: strview, with: strview): string {
var made := string::new(nothing{});
var i: u64 = 0;
var done := false;
while i < text.length {
if !done && i + what.length <= text.length && text.substr(i, what.length) == what {
made.append(with);
i += what.length;
done = true;
continue;
}
made.push(text[i]);
i++;
}
return made;
}
# Whether what's refused is refused as `kind`, saying `says` somewhere in why.
def refuses(t: &mut tester, json: strview, kind: gltf::failure, says: strview): void {
match gltf::decode(json) {
value(_) => {
io::println($"read, but shouldn't be: {json}".view());
t.expects(false);
}
error(e) => {
val why := e.describe();
val found := holds(why.view(), says);
if e.kind != kind || !found {
io::println($"refused as `{why.view()}`, not as `{says}`".view());
}
t.expects(e.kind == kind && found);
}
}
}
def holds(text: strview, part: strview): bool {
return true if part.length == 0;
var i: u64 = 0;
while i + part.length <= text.length {
return true if text.substr(i, part.length) == part;
i++;
}
return false;
}
@test def refuses_what_breaks_gltf(t: &mut tester): void {
refuses(t, "{ not json", gltf::failure::malformed, "JSON");
refuses(t, "{ \"asset\": { \"version\": \"1.0\" } }", gltf::failure::unsupported, "1.0");
refuses(t, "{ \"scenes\": [] }", gltf::failure::malformed, "version");
refuses(t, asset_with("\"extensionsRequired\": [\"KHR_draco_mesh_compression\"]").view(), gltf::failure::unsupported, "KHR_draco_mesh_compression");
refuses(t, asset_with("\"nodes\": [{ \"mesh\": 0 }]").view(), gltf::failure::malformed, "nodes[0]: its mesh is 0, of the 0 meshes");
refuses(t, asset_with("\"nodes\": [{ \"children\": [1] }, { \"children\": [0] }]").view(), gltf::failure::malformed, "comes back to it");
refuses(t, asset_with("\"nodes\": [{ \"children\": [1] }, {}, { \"children\": [1] }]").view(), gltf::failure::malformed, "child of both");
refuses(t, asset_with("\"nodes\": [{ \"children\": [1] }, {}], \"scenes\": [{ \"nodes\": [1] }]").view(), gltf::failure::malformed, "not a root");
refuses(t, asset_with("\"buffers\": [{ \"byteLength\": 8 }]").view(), gltf::failure::malformed, "buffers[0]");
refuses(t, asset_with("\"buffers\": [{ \"byteLength\": 8, \"uri\": \"missing.bin\" }]").view(), gltf::failure::unreadable, "missing.bin");
refuses(t, asset_with("\"buffers\": [{ \"byteLength\": 4, \"uri\": \"https://example.com/a.bin\" }]").view(), gltf::failure::unsupported, "another place");
refuses(t, asset_with("\"buffers\": [{ \"byteLength\": 4, \"uri\": \"data:application/octet-stream;base64,AAAA\" }]").view(), gltf::failure::malformed, "3");
# An accessor reaching past its view, and one of a type glTF hasn't.
val one_view := "\"buffers\": [{ \"byteLength\": 12, \"uri\": \"data:application/octet-stream;base64,AAAAAAAAAAAAAAAA\" }], \"bufferViews\": [{ \"buffer\": 0, \"byteLength\": 12 }]";
refuses(t, asset_with(joined(one_view, ", \"accessors\": [{ \"bufferView\": 0, \"componentType\": 5126, \"count\": 2, \"type\": \"VEC2\" }]").view()).view(),
gltf::failure::malformed, "reach past");
refuses(t, asset_with(joined(one_view, ", \"accessors\": [{ \"bufferView\": 0, \"componentType\": 5124, \"count\": 1, \"type\": \"SCALAR\" }]").view()).view(),
gltf::failure::malformed, "componentType 5124");
val twelve := "\"buffers\": [{ \"byteLength\": 12, \"uri\": \"data:application/octet-stream;base64,AAAAAAAAAAAAAAAA\" }]";
refuses(t, asset_with(joined(twelve, ", \"bufferViews\": [{ \"buffer\": 0, \"byteOffset\": 8, \"byteLength\": 8 }]").view()).view(),
gltf::failure::malformed, "reaches byte 16");
# A `.glb` file of another version, and one cut short.
val none := vector::<u8>::new();
var glb := glb_of(asset_with("").view(), none.view());
glb[4] = 1;
refuses(t, strview::over(glb.data, glb.length), gltf::failure::unsupported, "version 1");
glb[4] = 2;
refuses(t, strview::over(glb.data, 16), gltf::failure::malformed, "cut short");
}
# What an asset uses but doesn't require, of extensions it doesn't read, is
# read without them, and named among its extensions.
@test def reads_past_extensions_it_only_uses(t: &mut tester): void {
val json := asset_with("\"extensionsUsed\": [\"KHR_texture_transform\"], \"nodes\": [{ \"extensions\": { \"KHR_texture_transform\": {} } }]");
val read := gltf::decode(json.view()) else (_) {
t.expects(false);
return;
};
t.expects(read.extensions.length == 1 && read.extensions[0].view() == "KHR_texture_transform" && read.nodes.length == 1);
}
+181
View File
@@ -0,0 +1,181 @@
#! Contains what the tests of `gltf` share: buffers packed of numbers, their
#! base64 for `data:` URIs, `.glb` files put together of JSON and a buffer, and
#! files written where the tests read them back.
import std::io;
import std::math;
import gltf;
# The folder the tests write their files into.
const written: strview = ".blossom/gltf tests";
# Bytes being packed, little-endian, as a buffer holds them.
struct packed {
var bytes: vector<u8>;
}
def packed::new(): packed {
return packed{ bytes = vector::<u8>::new() };
}
def packed::byte(&mut this, value: u8): void {
this.bytes.append(value);
}
def packed::short(&mut this, value: u16): void {
this.bytes.append((value & 0xFF) as u8);
this.bytes.append((value >> 8) as u8);
}
def packed::word(&mut this, value: u32): void {
for i in 0..4u {
this.bytes.append(((value >> (8 * i)) & 0xFF) as u8);
}
}
def packed::float(&mut this, value: f32): void {
this.word(math::bits_of(value));
}
def packed::floats(&mut this, values: span<f32>): void {
for i in 0..values.length {
this.float(values[i]);
}
}
# Pads with zeros to a multiple of 4 bytes.
def packed::align(&mut this): void {
while this.bytes.length % 4 != 0 {
this.bytes.append(0);
}
}
# Bytes as base64, padded.
def base64_of(bytes: span<u8>): string {
val digits: strview = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
var made := string::new(nothing{});
var i: u64 = 0;
while i < bytes.length {
val left := bytes.length - i;
val a := bytes[i] as u32;
val b: u32 = left > 1 ? bytes[i + 1] as u32 : 0;
val c: u32 = left > 2 ? bytes[i + 2] as u32 : 0;
val all := (a << 16) | (b << 8) | c;
made.push(digits[((all >> 18) & 63) as u64]);
made.push(digits[((all >> 12) & 63) as u64]);
made.push(left > 1 ? digits[((all >> 6) & 63) as u64] : '=');
made.push(left > 2 ? digits[(all & 63) as u64] : '=');
i += 3;
}
return made;
}
# A `data:` URI of a buffer's bytes.
def data_uri(bytes: span<u8>): string {
val digits := base64_of(bytes);
return $"data:application/octet-stream;base64,{digits.view()}";
}
# A `.glb` file of JSON and a buffer: its header, then the JSON chunk, padded
# with spaces, and the binary chunk, padded with zeros.
def glb_of(json: strview, bin: span<u8>): vector<u8> {
var text := packed::new();
text.bytes.extend(json);
while text.bytes.length % 4 != 0 {
text.bytes.append(' ');
}
var data := packed::new();
data.bytes.extend(bin);
data.align();
var made := packed::new();
made.word(0x46546C67);
made.word(2);
made.word((12 + 8 + text.bytes.length + (bin.length != 0 ? 8 + data.bytes.length : 0)) as u32);
made.word(text.bytes.length as u32);
made.word(0x4E4F534A);
made.bytes.extend(text.bytes.view());
if bin.length != 0 {
made.word(data.bytes.length as u32);
made.word(0x004E4942);
made.bytes.extend(data.bytes.view());
}
return move(made.bytes);
}
# Writes a file the tests read, beneath `written`, making its folder.
def put(at: strview, bytes: span<u8>): void {
var file := path::parse($"{written}/{at}".view()).expect("A path.");
if file.parent() is some(&above) {
_ = io::create_directories(above);
}
io::write_file(&file, bytes).expect("The file is written.");
}
# The positions, normals, and indices of a quad of two triangles, facing +z, a
# unit across, packed one after another: 48 bytes of positions, 48 of normals,
# and 12 of indices, as `quad_json` names them.
def quad_buffer(): vector<u8> {
var made := packed::new();
val places: f32[12] = [-0.5, -0.5, 0, 0.5, -0.5, 0, 0.5, 0.5, 0, -0.5, 0.5, 0];
val normals: f32[12] = [0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1];
made.floats(places[:]);
made.floats(normals[:]);
for each in [0u, 1, 2, 0, 2, 3] {
made.short(each as u16);
}
made.align();
return move(made.bytes);
}
# The JSON of the quad, of one node of one mesh, its material red and double
# sided, its buffer named by a URI, or the `.glb` file's where that's empty,
# and other members after, where given.
def quad_json(uri: strview, more: strview = ""): string {
var made := string::from("""
{
"asset": { "version": "2.0", "generator": "gltf tests" },
"scene": 0,
"scenes": [{ "name": "Quad", "nodes": [0] }],
"nodes": [{ "name": "Square", "mesh": 0, "translation": [1, 2, 3] }],
"meshes": [{ "name": "quad", "primitives": [{
"attributes": { "POSITION": 0, "NORMAL": 1 }, "indices": 2, "material": 0
}] }],
"materials": [{ "name": "red", "doubleSided": true, "pbrMetallicRoughness": { "baseColorFactor": [1, 0, 0, 1], "metallicFactor": 0 } }],
"accessors": [
{ "bufferView": 0, "componentType": 5126, "count": 4, "type": "VEC3", "min": [-0.5, -0.5, 0], "max": [0.5, 0.5, 0] },
{ "bufferView": 0, "byteOffset": 48, "componentType": 5126, "count": 4, "type": "VEC3" },
{ "bufferView": 1, "componentType": 5123, "count": 6, "type": "SCALAR" }
],
"bufferViews": [
{ "buffer": 0, "byteOffset": 0, "byteLength": 96 },
{ "buffer": 0, "byteOffset": 96, "byteLength": 12 }
],
"buffers": [{ "byteLength": 108
""", nothing{});
if uri.length != 0 {
made.append(", \"uri\": \"");
made.append(uri);
made.append("\"");
}
made.append(" }]");
made.append(more);
made.append(" }");
return made;
}
# JSON of an asset, a version and the members given, as the tests of what's
# refused write each.
def asset_with(members: strview): string {
var made := string::from("{ \"asset\": { \"version\": \"2.0\" }", nothing{});
if members.length != 0 {
made.append(", ");
made.append(members);
}
made.append(" }");
return made;
}
# Whether two floats are within a millionth.
def near(a: f32, b: f32): bool {
return math::abs(a - b) < 1e-5;
}