Files
models/tests/support.sakura
T

181 lines
5.7 KiB
Plaintext
Raw Normal View History

2026-10-30 12:04:54 -05:00
#! 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 malunal::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.span}";
}
# 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.span);
if bin.length != 0 {
made.word(data.bytes.length as u32);
made.word(0x004E4942);
made.bytes.extend(data.bytes.span);
}
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}".span).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;
}