119 lines
5.5 KiB
Plaintext
119 lines
5.5 KiB
Plaintext
#! 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 malunal::linear;
|
||
|
|
import malunal::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.span);
|
||
|
|
val json := nodes_json(uri.span);
|
||
|
|
match gltf::decode(json.span) {
|
||
|
|
value(read) => return read;
|
||
|
|
error(e) => {
|
||
|
|
io::println(e.describe().span);
|
||
|
|
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.span == "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.span == "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));
|
||
|
|
}
|