#! Contains the tree of nodes: each read with where it is within its parent, as #! a translation, rotation, and scale, or as a matrix, the other worked out of #! it; each child's parent found, and the tree checked to have no node of two #! parents and no loop; and where a node is in the world, through its parents. import std::json; import std::math; import linear; module gltf; def read_nodes(made: &mut asset, root: &json::value, n: &counts): result { val found := array_member(root, "nodes", "nodes") else (e) { return e; }; val list := found else { return; }; for i in 0..list.length { val site := $"nodes[{i}]"; val each := object_at(&list, i, site.view()) else (e) { return e; }; var read := node{}; read.name = text_member(&each, "name", site.view()) else (e) { return e; }; read.children = indices_member(&each, "children", site.view(), n.nodes, "nodes") else (e) { return e; }; read.mesh = index_member(&each, "mesh", site.view(), n.meshes, "meshes") else (e) { return e; }; read.camera = index_member(&each, "camera", site.view(), n.cameras, "cameras") else (e) { return e; }; read.skin = index_member(&each, "skin", site.view(), n.skins, "skins") else (e) { return e; }; read.weights = floats_member(&each, "weights", site.view(), 0) else (e) { return e; }; val extensions := object_member(&each, "extensions", site.view()) else (e) { return e; }; if extensions is some(x) { if member_of(&x, "KHR_lights_punctual") is some(lit) { read.light = index_member(&lit, "light", site.view(), n.lights, "lights") else (e) { return e; }; } } place_node(&mut read, &each, site.view()) else (e) { return e; }; made.nodes.append(move(read)); } # Each child has one parent, and following parents up ends. for i in 0..made.nodes.length { for k in 0..made.nodes[i].children.length { val child := made.nodes[i].children[k]; return failed(failure::malformed, $"nodes[{i}]: it's a child of itself.".view()) if child == i; if made.nodes[child].parent is some(other) { return failed(failure::malformed, $"nodes[{child}]: it's a child of both {other} and {i}.".view()); } made.nodes[child].parent = i; } } for i in 0..made.nodes.length { var at := i; var steps: u64 = 0; while made.nodes[at].parent is some(up) { at = up; steps++; return failed(failure::malformed, $"nodes[{i}]: following its parents up comes back to it.".view()) if steps > made.nodes.length; } } return; } # Where a node is within its parent: its matrix, taken apart into its # translation, rotation, and scale; or those, put together into its matrix. def place_node(read: &mut node, v: &json::value, site: strview): result { val matrix := floats_member(v, "matrix", site, 16) else (e) { return e; }; if matrix.length == 16 { for i in 0..16u { read.matrix.m[i] = matrix[i]; } take_apart(read); return; } val t := floats_member(v, "translation", site, 3) else (e) { return e; }; val r := floats_member(v, "rotation", site, 4) else (e) { return e; }; val s := floats_member(v, "scale", site, 3) else (e) { return e; }; if t.length == 3 { read.translation = vec3{ t[0], t[1], t[2] }; } if r.length == 4 { val q := quat{ r[0], r[1], r[2], r[3] }; read.rotation = q.length > 0 ? q.normalized() : quat::identity; } if s.length == 3 { read.scale = vec3{ s[0], s[1], s[2] }; } read.matrix = mat4::compose(read.translation, read.rotation, read.scale); return; } # Takes a node's matrix apart: its translation, the lengths of its axes as its # scale, x's negative where the matrix mirrors, and the rotation of its axes # made unit length. glTF holds that a node's matrix is no more than that. def take_apart(read: &mut node): void { val m := &read.matrix.m; read.translation = vec3{ m[12], m[13], m[14] }; val x := vec3{ m[0], m[1], m[2] }; val y := vec3{ m[4], m[5], m[6] }; val z := vec3{ m[8], m[9], m[10] }; var sx := x.length; val sy := y.length; val sz := z.length; if x.cross(y).dot(z) < 0 { sx = -sx; } read.scale = vec3{ sx, sy, sz }; if sx == 0 || sy == 0 || sz == 0 { read.rotation = quat::identity; return; } read.rotation = quat::of_basis(x / sx, y / sy, z / sz).normalized(); } #! Where a node is in the world: its matrix, by its parent's, by its parent's, #! up to its root. #! @param this The asset. #! @param which The node. #! @returns The matrix taking its space to its scene's. pub def asset::world_matrix(&this, which: u64): mat4 { var made := this.nodes[which].matrix; var at := which; while this.nodes[at].parent is some(up) { made = this.nodes[up].matrix * made; at = up; } return made; } #! The nodes at the roots of the tree: those of no parent, in the order the #! asset holds them, as a scene of every one would hold them. pub def asset::roots(&this): vector { var made := vector::::new(); for i in 0..this.nodes.length { if this.nodes[i].parent is none { made.append(i); } } return made; }