Initial Commit
This commit is contained in:
36 files changed
+8436
No files matched your search
@@ -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 malunal::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.span);
|
||||
val json := accessors_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 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));
|
||||
}
|
||||
@@ -0,0 +1,530 @@
|
||||
#! Contains the tests of `fbx`: an ASCII scene of every part read, written as
|
||||
#! binary and read again; a document built by hand, written as binary of 7.4
|
||||
#! and 7.5 and read back; an array compressed by zlib; models placed by FBX's
|
||||
#! chain of pivots and rotations, and a scene's axes turned; a curve's values;
|
||||
#! and what's refused.
|
||||
|
||||
import std::math;
|
||||
import std::testing;
|
||||
import malunal::linear;
|
||||
import malunal::fbx;
|
||||
|
||||
# Whether two floats are within a ten-thousandth.
|
||||
def close_to(a: f32, b: f32): bool {
|
||||
return math::abs(a - b) < 1e-4;
|
||||
}
|
||||
|
||||
# A scene of every part FBX's scenes hold, as ASCII: its settings, Z up and
|
||||
# in meters; templates; a quad of two triangles, its normals by point, its
|
||||
# places on a texture through indices, and a material for each triangle; a
|
||||
# model placed with a pre-rotation, and a bone beneath it, holding a light;
|
||||
# two materials, the first with a texture, whose video embeds four bytes; a
|
||||
# skin of one cluster; and an animation turning the bone.
|
||||
const scene_text: strview = """
|
||||
; FBX 7.4.0 project file
|
||||
FBXHeaderExtension: {
|
||||
FBXHeaderVersion: 1003
|
||||
FBXVersion: 7400
|
||||
}
|
||||
GlobalSettings: {
|
||||
Version: 1000
|
||||
Properties70: {
|
||||
P: "UpAxis", "int", "Integer", "",2
|
||||
P: "UpAxisSign", "int", "Integer", "",1
|
||||
P: "FrontAxis", "int", "Integer", "",1
|
||||
P: "FrontAxisSign", "int", "Integer", "",-1
|
||||
P: "CoordAxis", "int", "Integer", "",0
|
||||
P: "CoordAxisSign", "int", "Integer", "",1
|
||||
P: "UnitScaleFactor", "double", "Number", "",100
|
||||
P: "TimeMode", "enum", "", "",11
|
||||
}
|
||||
}
|
||||
Definitions: {
|
||||
ObjectType: "Model" {
|
||||
Count: 2
|
||||
PropertyTemplate: "FbxNode" {
|
||||
Properties70: {
|
||||
P: "Lcl Scaling", "Lcl Scaling", "", "A",1,1,1
|
||||
P: "Visibility", "Visibility", "", "A",1
|
||||
}
|
||||
}
|
||||
}
|
||||
ObjectType: "Material" {
|
||||
PropertyTemplate: "FbxSurfacePhong" {
|
||||
Properties70: {
|
||||
P: "ShininessExponent", "Number", "", "A",42
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Objects: {
|
||||
Geometry: 100, "Geometry::Quad", "Mesh" {
|
||||
Vertices: *12 {
|
||||
a: -1,-1,0,1,-1,0,1,1,0,-1,1,0
|
||||
}
|
||||
PolygonVertexIndex: *6 {
|
||||
a: 0,1,-3,0,2,-4
|
||||
}
|
||||
LayerElementNormal: 0 {
|
||||
Version: 101
|
||||
MappingInformationType: "ByVertice"
|
||||
ReferenceInformationType: "Direct"
|
||||
Normals: *12 {
|
||||
a: 0,0,1,0,0,1,0,0,1,0,0,1
|
||||
}
|
||||
}
|
||||
LayerElementUV: 0 {
|
||||
Name: "UVMap"
|
||||
MappingInformationType: "ByPolygonVertex"
|
||||
ReferenceInformationType: "IndexToDirect"
|
||||
UV: *8 {
|
||||
a: 0,0,1,0,1,1,0,1
|
||||
}
|
||||
UVIndex: *6 {
|
||||
a: 0,1,2,0,2,3
|
||||
}
|
||||
}
|
||||
LayerElementMaterial: 0 {
|
||||
MappingInformationType: "ByPolygon"
|
||||
ReferenceInformationType: "IndexToDirect"
|
||||
Materials: *2 {
|
||||
a: 0,1
|
||||
}
|
||||
}
|
||||
}
|
||||
Model: 200, "Model::Quad", "Mesh" {
|
||||
Properties70: {
|
||||
P: "Lcl Translation", "Lcl Translation", "", "A",0,0,5
|
||||
P: "RotationActive", "bool", "", "",1
|
||||
P: "PreRotation", "Vector3D", "Vector", "",-90,0,0
|
||||
}
|
||||
Shading: T
|
||||
Culling: "CullingOff"
|
||||
}
|
||||
Model: 201, "Model::Bone", "LimbNode" {
|
||||
Properties70: {
|
||||
P: "Lcl Translation", "Lcl Translation", "", "A",0,2,0
|
||||
P: "Lcl Rotation", "Lcl Rotation", "", "A",0,0,90
|
||||
}
|
||||
}
|
||||
Material: 300, "Material::Red", "" {
|
||||
ShadingModel: "phong"
|
||||
Properties70: {
|
||||
P: "DiffuseColor", "Color", "", "A",1,0,0
|
||||
}
|
||||
}
|
||||
Material: 301, "Material::Blue", "" {
|
||||
ShadingModel: "lambert"
|
||||
}
|
||||
Texture: 400, "Texture::Bricks", "" {
|
||||
FileName: "C:/textures/bricks.png"
|
||||
RelativeFilename: "textures/bricks.png"
|
||||
Properties70: {
|
||||
P: "UVSet", "KString", "", "", "UVMap"
|
||||
}
|
||||
}
|
||||
Video: 500, "Video::Bricks", "Clip" {
|
||||
Content: , "AAEC/w=="
|
||||
RelativeFilename: "textures/bricks.png"
|
||||
}
|
||||
NodeAttribute: 600, "NodeAttribute::Sun", "Light" {
|
||||
Properties70: {
|
||||
P: "LightType", "enum", "", "",1
|
||||
P: "Intensity", "Number", "", "A",150
|
||||
}
|
||||
}
|
||||
Deformer: 700, "Deformer::Skin", "Skin" {
|
||||
}
|
||||
Deformer: 701, "SubDeformer::Bone", "Cluster" {
|
||||
Indexes: *2 {
|
||||
a: 0,1
|
||||
}
|
||||
Weights: *2 {
|
||||
a: 1,0.5
|
||||
}
|
||||
Transform: *16 {
|
||||
a: 1,0,0,0,0,1,0,0,0,0,1,0,0,0,0,1
|
||||
}
|
||||
TransformLink: *16 {
|
||||
a: 1,0,0,0,0,1,0,0,0,0,1,0,0,2,0,1
|
||||
}
|
||||
}
|
||||
AnimationStack: 800, "AnimStack::Wave", "" {
|
||||
Properties70: {
|
||||
P: "LocalStop", "KTime", "Time", "",46186158000
|
||||
}
|
||||
}
|
||||
AnimationLayer: 801, "AnimLayer::Base", "" {
|
||||
}
|
||||
AnimationCurveNode: 802, "AnimCurveNode::R", "" {
|
||||
Properties70: {
|
||||
P: "d|X", "Number", "", "A",0
|
||||
P: "d|Y", "Number", "", "A",0
|
||||
P: "d|Z", "Number", "", "A",90
|
||||
}
|
||||
}
|
||||
AnimationCurve: 803, "AnimCurve::", "" {
|
||||
Default: 90
|
||||
KeyVer: 4008
|
||||
KeyTime: *2 {
|
||||
a: 0,46186158000
|
||||
}
|
||||
KeyValueFloat: *2 {
|
||||
a: 90,180
|
||||
}
|
||||
KeyAttrFlags: *1 {
|
||||
a: 4
|
||||
}
|
||||
KeyAttrDataFloat: *4 {
|
||||
a: 0,0,0,0
|
||||
}
|
||||
KeyAttrRefCount: *1 {
|
||||
a: 2
|
||||
}
|
||||
}
|
||||
}
|
||||
Connections: {
|
||||
;Model::Quad, Model::RootNode
|
||||
C: "OO",200,0
|
||||
C: "OO",201,200
|
||||
C: "OO",100,200
|
||||
C: "OO",300,200
|
||||
C: "OO",301,200
|
||||
C: "OP",400,300, "DiffuseColor"
|
||||
C: "OO",500,400
|
||||
C: "OO",600,201
|
||||
C: "OO",700,100
|
||||
C: "OO",701,700
|
||||
C: "OO",201,701
|
||||
C: "OO",801,800
|
||||
C: "OO",802,801
|
||||
C: "OP",802,201, "Lcl Rotation"
|
||||
C: "OP",803,802, "d|Z"
|
||||
}
|
||||
""";
|
||||
|
||||
# Checks the scene read of `scene_text`, whichever way it was written.
|
||||
def expect_scene(t: &mut tester, s: &fbx::scene): void {
|
||||
t.expects(s.settings.up_axis == 2 && s.settings.front_sign == -1 && close_to(s.settings.meters_per_unit(), 1) && s.settings.frame_rate == 24);
|
||||
t.expects(s.meshes.length == 1 && s.models.length == 2 && s.materials.length == 2 && s.textures.length == 1 && s.videos.length == 1);
|
||||
val quad := &s.meshes[0];
|
||||
t.expects(quad.name.span == "Quad" && quad.positions.length == 4 && quad.polygons.length == 2 && quad.polygons[1].first == 3 && quad.polygons[1].count == 3);
|
||||
t.expects(quad.corners.length == 6 && quad.corners[2] == 2 && quad.corners[5] == 3);
|
||||
t.expects(quad.normals.length == 6 && close_to(quad.normals[5].z, 1));
|
||||
t.expects(quad.uv_sets.length == 1 && quad.uv_sets[0].name.span == "UVMap" && close_to(quad.uv_sets[0].uvs[4].x, 1) && close_to(quad.uv_sets[0].uvs[5].y, 1) && close_to(quad.uv_sets[0].uvs[5].x, 0));
|
||||
t.expects(quad.materials.length == 2 && quad.materials[0] == 0 && quad.materials[1] == 1);
|
||||
# The tree, and what's whose.
|
||||
val square := &s.models[0];
|
||||
t.expects(square.name.span == "Quad" && square.kind.span == "Mesh" && square.parent == fbx::absent && square.mesh == 0 && square.materials.length == 2 && square.materials[1] == 1);
|
||||
t.expects(s.roots.length == 1 && s.roots[0] == 0 && square.children.length == 1 && square.children[0] == 1);
|
||||
t.expects(close_to(square.translation.z, 5) && square.rotation_active && close_to(square.pre_rotation.x, -90) && close_to(square.scale.y, 1) && square.visible);
|
||||
val bone := &s.models[1];
|
||||
t.expects(bone.kind.span == "LimbNode" && bone.parent == 0 && bone.light == 0 && close_to(bone.rotation.z, 90));
|
||||
# The bone's place: 2 up, turned by its parent's pre-rotation to 2 back, from 5 forward.
|
||||
val placed := s.world_matrix(1);
|
||||
t.expects(close_to(placed.m[12], 0) && close_to(placed.m[13], 0) && close_to(placed.m[14], 3));
|
||||
# Materials, their template's values, and their textures.
|
||||
val red := &s.materials[0];
|
||||
t.expects(red.name.span == "Red" && red.shading.span == "phong" && close_to(red.diffuse.x, 1) && close_to(red.diffuse.y, 0) && close_to(red.shininess, 42));
|
||||
t.expects(red.map("DiffuseColor") is some(k) && k == 0 && red.map("NormalMap") is none);
|
||||
t.expects(close_to(s.materials[1].diffuse.x, 0.8) && close_to(s.materials[1].shininess, 42) && s.materials[1].shading.span == "lambert");
|
||||
val bricks := &s.textures[0];
|
||||
t.expects(bricks.file.span == "C:/textures/bricks.png" && bricks.relative.span == "textures/bricks.png" && bricks.uv_set.span == "UVMap" && bricks.video == 0);
|
||||
val held := &s.videos[0].content;
|
||||
t.expects(held.length == 4 && held[0] == 0 && held[2] == 2 && held[3] == 255);
|
||||
t.expects(s.lights.length == 1 && s.lights[0].kind == fbx::light_kind::directional && close_to(s.lights[0].intensity, 150));
|
||||
# The skin, its cluster, and its bone.
|
||||
t.expects(s.skins.length == 1 && s.skins[0].mesh == 0 && quad.skin == 0 && s.skins[0].clusters.length == 1);
|
||||
val bound := &s.clusters[0];
|
||||
t.expects(bound.bone == 1 && bound.points.length == 2 && bound.points[1] == 1 && close_to(bound.weights[1], 0.5) && close_to(bound.transform_link.m[13], 2));
|
||||
# The animation, what it turns, and its curve.
|
||||
t.expects(s.stacks.length == 1 && s.stacks[0].stop == 1 && s.stacks[0].layers.length == 1 && s.layers[0].curve_nodes.length == 1);
|
||||
val turning := &s.curve_nodes[0];
|
||||
t.expects(turning.target_kind == fbx::part_kind::model && turning.target == 1 && turning.property.span == "Lcl Rotation" && turning.channels.length == 3);
|
||||
if turning.channel("Z") is some(&z) {
|
||||
t.expects(z.curve == 0 && close_to(z.value, 90));
|
||||
} else {
|
||||
t.expects(false);
|
||||
}
|
||||
t.expects(turning.channel("X") is some(&x) && x.curve == fbx::absent);
|
||||
val wave := &s.curves[0];
|
||||
t.expects(wave.times.length == 2 && wave.times[1] == 1 && wave.interpolations[0] == fbx::key_interpolation::linear && close_to(wave.at(0.5), 135) && close_to(wave.at(2), 180));
|
||||
}
|
||||
|
||||
@test def reads_an_ascii_scene_of_every_part(t: &mut tester): void {
|
||||
var doc := fbx::decode(scene_text).expect("The scene is read.");
|
||||
t.expects(doc.version == 7400 && !doc.binary);
|
||||
if doc.child(fbx::absent, "Objects") is some(objects) {
|
||||
t.expects(doc.nodes[objects as u64].children.length == 14);
|
||||
} else {
|
||||
t.expects(false);
|
||||
}
|
||||
val s := fbx::scene::of(move(doc)).expect("The scene's objects are read.");
|
||||
expect_scene(t, &s);
|
||||
}
|
||||
|
||||
@test def writes_a_scene_as_binary_and_reads_it_again(t: &mut tester): void {
|
||||
val doc := fbx::decode(scene_text).expect("The scene is read.");
|
||||
val bytes := fbx::encode(&doc).expect("It's written.");
|
||||
# The header, and Autodesk's footer.
|
||||
t.expects(bytes.length > 200 && bytes[0] == 'K' && bytes[21] == 0x1A && bytes[bytes.length - 1] == 0x0B && bytes[bytes.length - 16] == 0xF8);
|
||||
var again := fbx::decode(bytes.span).expect("It's read again.");
|
||||
t.expects(again.binary && again.version == 7400 && again.nodes.length == doc.nodes.length);
|
||||
val s := fbx::scene::of(move(again)).expect("The scene's objects are read.");
|
||||
expect_scene(t, &s);
|
||||
}
|
||||
|
||||
# A name as binary FBX writes it: the name, two bytes 0 and 1, and its class.
|
||||
def binary_name(name: strview, class_name: strview): string {
|
||||
var made := string::from(name, nothing{});
|
||||
made.push(0);
|
||||
made.push(1);
|
||||
made.append(class_name);
|
||||
return made;
|
||||
}
|
||||
|
||||
# A document built by hand: a header, a quad's geometry, its model, and the
|
||||
# connections between them.
|
||||
def built_document(version: u32): fbx::document {
|
||||
var doc := fbx::document{ version = version };
|
||||
val header := doc.add(fbx::absent, "FBXHeaderExtension");
|
||||
val stated := doc.add(header, "FBXVersion");
|
||||
doc.nodes[stated as u64].properties.append(fbx::property::of_int32(version as i32));
|
||||
val objects := doc.add(fbx::absent, "Objects");
|
||||
val geometry := doc.add(objects, "Geometry");
|
||||
val geometry_name := binary_name("Quad", "Geometry");
|
||||
doc.nodes[geometry as u64].properties.append(fbx::property::of_int64(100));
|
||||
doc.nodes[geometry as u64].properties.append(fbx::property::of_text(geometry_name.span));
|
||||
doc.nodes[geometry as u64].properties.append(fbx::property::of_text("Mesh"));
|
||||
val places: f64[12] = [-1, -1, 0, 1, -1, 0, 1, 1, 0, -1, 1, 0];
|
||||
val vertices := doc.add(geometry, "Vertices");
|
||||
doc.nodes[vertices as u64].properties.append(fbx::property::of_reals(fbx::property_kind::float64s, places[:]));
|
||||
val corners: i64[6] = [0, 1, -3, 0, 2, -4];
|
||||
val polygons := doc.add(geometry, "PolygonVertexIndex");
|
||||
doc.nodes[polygons as u64].properties.append(fbx::property::of_integers(fbx::property_kind::int32s, corners[:]));
|
||||
val model := doc.add(objects, "Model");
|
||||
val model_name := binary_name("Quad", "Model");
|
||||
doc.nodes[model as u64].properties.append(fbx::property::of_int64(200));
|
||||
doc.nodes[model as u64].properties.append(fbx::property::of_text(model_name.span));
|
||||
doc.nodes[model as u64].properties.append(fbx::property::of_text("Mesh"));
|
||||
val properties := doc.add(model, "Properties70");
|
||||
val moved := doc.add(properties, "P");
|
||||
doc.nodes[moved as u64].properties.append(fbx::property::of_text("Lcl Translation"));
|
||||
doc.nodes[moved as u64].properties.append(fbx::property::of_text("Lcl Translation"));
|
||||
doc.nodes[moved as u64].properties.append(fbx::property::of_text(""));
|
||||
doc.nodes[moved as u64].properties.append(fbx::property::of_text("A"));
|
||||
doc.nodes[moved as u64].properties.append(fbx::property::of_float64(0));
|
||||
doc.nodes[moved as u64].properties.append(fbx::property::of_float64(0));
|
||||
doc.nodes[moved as u64].properties.append(fbx::property::of_float64(5));
|
||||
val shown := doc.add(model, "Shading");
|
||||
doc.nodes[shown as u64].properties.append(fbx::property::of_bool(true));
|
||||
val flags := doc.add(model, "Flags");
|
||||
doc.nodes[flags as u64].properties.append(fbx::property::of_float32(1.5));
|
||||
val connections := doc.add(fbx::absent, "Connections");
|
||||
val pairs: i64[4] = [200, 0, 100, 200];
|
||||
for k in 0..2u {
|
||||
val link := doc.add(connections, "C");
|
||||
doc.nodes[link as u64].properties.append(fbx::property::of_text("OO"));
|
||||
doc.nodes[link as u64].properties.append(fbx::property::of_int64(pairs[k * 2]));
|
||||
doc.nodes[link as u64].properties.append(fbx::property::of_int64(pairs[k * 2 + 1]));
|
||||
}
|
||||
return doc;
|
||||
}
|
||||
|
||||
@test def writes_a_built_document_as_binary_of_both_widths(t: &mut tester): void {
|
||||
for k in 0..2u {
|
||||
val version: u32 = k == 0 ? 7400 : 7500;
|
||||
val doc := built_document(version);
|
||||
val bytes := fbx::encode(&doc).expect("It's written.");
|
||||
var read := fbx::decode(bytes.span).expect("It's read back.");
|
||||
t.expects(read.version == version && read.nodes.length == doc.nodes.length && read.roots.length == 3);
|
||||
for i in 0..doc.nodes.length {
|
||||
val a := &doc.nodes[i];
|
||||
val b := &read.nodes[i];
|
||||
t.expects(a.name.span == b.name.span && a.properties.length == b.properties.length && a.children.length == b.children.length && a.parent == b.parent);
|
||||
for p in 0..a.properties.length {
|
||||
t.expects(a.properties[p].kind == b.properties[p].kind && a.properties[p].integer == b.properties[p].integer && a.properties[p].real == b.properties[p].real);
|
||||
t.expects(a.properties[p].text() == b.properties[p].text() && a.properties[p].count() == b.properties[p].count());
|
||||
}
|
||||
}
|
||||
if read.child(fbx::absent, "Objects") is some(objects) && read.child(objects, "Geometry") is some(geometry) && read.child(geometry, "PolygonVertexIndex") is some(indices) {
|
||||
val values := read.integers(indices);
|
||||
t.expects(values.length == 6 && values[2] == -3 && values[5] == -4);
|
||||
} else {
|
||||
t.expects(false);
|
||||
}
|
||||
val s := fbx::scene::of(move(read)).expect("Its scene is read.");
|
||||
t.expects(s.models.length == 1 && s.models[0].name.span == "Quad" && s.models[0].mesh == 0 && close_to(s.models[0].translation.z, 5));
|
||||
t.expects(s.meshes.length == 1 && s.meshes[0].name.span == "Quad" && s.meshes[0].polygons.length == 2 && close_to(s.meshes[0].positions[2].y, 1));
|
||||
}
|
||||
}
|
||||
|
||||
# The Adler-32 of bytes.
|
||||
def adler_of(data: span<u8>): u32 {
|
||||
var low: u32 = 1;
|
||||
var high: u32 = 0;
|
||||
for byte in data {
|
||||
low = (low + byte as u32) % 65521;
|
||||
high = (high + low) % 65521;
|
||||
}
|
||||
return (high << 16) | low;
|
||||
}
|
||||
|
||||
def put_word(out: &mut vector<u8>, value: u32): void {
|
||||
for i in 0..4u {
|
||||
out.append(((value >> (8 * i)) & 0xFF) as u8);
|
||||
}
|
||||
}
|
||||
|
||||
@test def reads_an_array_compressed_by_zlib(t: &mut tester): void {
|
||||
# Two doubles, 0.5 and -2, compressed as a stored block within zlib.
|
||||
var raw := vector::<u8>::new();
|
||||
for value in [0.5, -2.0] {
|
||||
val bits := math::bits_of(value as f64);
|
||||
for i in 0..8u {
|
||||
raw.append(((bits >> (8 * i)) & 0xFF) as u8);
|
||||
}
|
||||
}
|
||||
var zlib := vector::<u8>::new();
|
||||
zlib.append(0x78);
|
||||
zlib.append(0x01);
|
||||
zlib.append(0x01);
|
||||
zlib.append(16);
|
||||
zlib.append(0);
|
||||
zlib.append(0xEF);
|
||||
zlib.append(0xFF);
|
||||
zlib.extend(raw.span);
|
||||
val sum := adler_of(raw.span);
|
||||
for i in 0..4u {
|
||||
zlib.append(((sum >> (8 * (3 - i))) & 0xFF) as u8);
|
||||
}
|
||||
# A node `A` of that array, then the record of zeros ending the root.
|
||||
var file := vector::<u8>::new();
|
||||
file.extend("Kaydara FBX Binary ");
|
||||
file.append(0);
|
||||
file.append(0x1A);
|
||||
file.append(0);
|
||||
put_word(&mut file, 7400);
|
||||
val properties := 1 + 12 + zlib.length;
|
||||
val end := file.length + 13 + 1 + properties;
|
||||
put_word(&mut file, end as u32);
|
||||
put_word(&mut file, 1);
|
||||
put_word(&mut file, properties as u32);
|
||||
file.append(1);
|
||||
file.append('A');
|
||||
file.append('d');
|
||||
put_word(&mut file, 2);
|
||||
put_word(&mut file, 1);
|
||||
put_word(&mut file, zlib.length as u32);
|
||||
file.extend(zlib.span);
|
||||
for _ in 0..13u {
|
||||
file.append(0);
|
||||
}
|
||||
val doc := fbx::decode(file.span).expect("The compressed array is read.");
|
||||
t.expects(doc.nodes.length == 1 && doc.nodes[0].name.span == "A" && doc.nodes[0].properties[0].kind == fbx::property_kind::float64s);
|
||||
val values := doc.reals(0);
|
||||
t.expects(values.length == 2 && values[0] == 0.5 && values[1] == -2);
|
||||
# Its checksum broken, it won't inflate.
|
||||
file[file.length - 14] = file[file.length - 14] ^ 0xFF;
|
||||
t.expects(fbx::decode(file.span) is error(e) && e.kind == fbx::failure::malformed);
|
||||
}
|
||||
|
||||
@test def places_models_by_fbx_chain_of_pivots_and_rotations(t: &mut tester): void {
|
||||
# Turned a quarter about z, about a pivot at x = 1.
|
||||
var pivoted := fbx::model{};
|
||||
pivoted.rotation = vec3{ 0, 0, 90 };
|
||||
pivoted.rotation_pivot = vec3{ 1, 0, 0 };
|
||||
val turned := pivoted.local_matrix().transform_point(vec3{ 2, 0, 0 });
|
||||
t.expects(close_to(turned.x, 1) && close_to(turned.y, 1) && close_to(turned.z, 0));
|
||||
# Scaled twice about a pivot at x = 1.
|
||||
var scaled := fbx::model{};
|
||||
scaled.scale = vec3{ 2, 2, 2 };
|
||||
scaled.scaling_pivot = vec3{ 1, 0, 0 };
|
||||
val grown := scaled.local_matrix().transform_point(vec3{ 2, 0, 0 });
|
||||
t.expects(close_to(grown.x, 3) && close_to(grown.y, 0));
|
||||
# The order of a rotation: x first, or z first; and an order not active is
|
||||
# taken as x first.
|
||||
var ordered := fbx::model{};
|
||||
ordered.rotation = vec3{ 90, 90, 0 };
|
||||
val first_x := ordered.local_matrix().transform_point(vec3{ 0, 1, 0 });
|
||||
t.expects(close_to(first_x.x, 1) && close_to(first_x.z, 0));
|
||||
ordered.order = fbx::rotation_order::zyx;
|
||||
ordered.rotation_active = true;
|
||||
val first_z := ordered.local_matrix().transform_point(vec3{ 0, 1, 0 });
|
||||
t.expects(close_to(first_z.z, 1) && close_to(first_z.x, 0));
|
||||
ordered.rotation_active = false;
|
||||
val inactive := ordered.local_matrix().transform_point(vec3{ 0, 1, 0 });
|
||||
t.expects(close_to(inactive.x, 1));
|
||||
# A post-rotation is turned back.
|
||||
var posted := fbx::model{ rotation_active = true };
|
||||
posted.post_rotation = vec3{ 0, 0, 90 };
|
||||
val back := posted.local_matrix().transform_point(vec3{ 1, 0, 0 });
|
||||
t.expects(close_to(back.y, -1));
|
||||
# Z up, -y to the front, in centimeters, turned to y up and meters.
|
||||
val zup := fbx::settings{ up_axis = 2, front_axis = 1, front_sign = -1 };
|
||||
val up := zup.axes().transform_point(vec3{ 0, 0, 1 });
|
||||
val front := zup.conversion().transform_point(vec3{ 0, -100, 0 });
|
||||
t.expects(close_to(up.y, 1) && close_to(front.z, 1) && close_to(front.y, 0));
|
||||
}
|
||||
|
||||
@test def gives_a_curve_s_values_between_its_keys(t: &mut tester): void {
|
||||
var held := fbx::curve{ value = 7 };
|
||||
t.expects(held.at(1) == 7);
|
||||
held.times.append(0);
|
||||
held.times.append(1);
|
||||
held.times.append(2);
|
||||
held.values.append(0);
|
||||
held.values.append(10);
|
||||
held.values.append(10);
|
||||
held.interpolations.append(fbx::key_interpolation::constant);
|
||||
held.interpolations.append(fbx::key_interpolation::cubic);
|
||||
held.interpolations.append(fbx::key_interpolation::linear);
|
||||
held.right_slopes.append(0);
|
||||
held.right_slopes.append(0);
|
||||
held.right_slopes.append(0);
|
||||
held.next_left_slopes.append(0);
|
||||
held.next_left_slopes.append(0);
|
||||
held.next_left_slopes.append(0);
|
||||
t.expects(held.at(-1) == 0 && held.at(0.5) == 0 && held.at(3) == 10);
|
||||
# A cubic of flat slopes, between equal values, stays.
|
||||
t.expects(close_to(held.at(1.5), 10));
|
||||
}
|
||||
|
||||
# Whether bytes are refused as a kind, saying what's given.
|
||||
def refused(bytes: span<u8>, kind: fbx::failure, says: strview): bool {
|
||||
if fbx::decode(bytes) is error(e) {
|
||||
val why := e.describe();
|
||||
return e.kind == kind && why.span.find(says) is some(_);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@test def refuses_what_breaks_fbx(t: &mut tester): void {
|
||||
val doc := built_document(7400);
|
||||
var bytes := fbx::encode(&doc).expect("It's written.");
|
||||
# Cut short, and a property of a type FBX hasn't.
|
||||
var cut := vector::<u8>::new();
|
||||
cut.extend(bytes.span[0:60]);
|
||||
t.expects(refused(cut.span, fbx::failure::malformed, "past the file's end"));
|
||||
var wrong := vector::<u8>::new();
|
||||
wrong.extend(bytes.span);
|
||||
# The first property of the first node, `FBXHeaderExtension`'s child, is
|
||||
# `FBXVersion`'s, after its header and name.
|
||||
val first: u64 = 27 + 13 + 18 + 13 + 10;
|
||||
t.expects(wrong[first] == 'I');
|
||||
wrong[first] = 'Z';
|
||||
t.expects(refused(wrong.span, fbx::failure::malformed, "isn't FBX's"));
|
||||
# Before 7.0.
|
||||
var old := vector::<u8>::new();
|
||||
old.extend(bytes.span);
|
||||
old[23] = 0xD4;
|
||||
old[24] = 0x17;
|
||||
t.expects(refused(old.span, fbx::failure::unsupported, "6100"));
|
||||
# ASCII unclosed, of an array short, and of version 6.
|
||||
t.expects(refused("Objects: {\n Model: 1, \"Model::A\", \"Null\" {\n", fbx::failure::malformed, "isn't closed"));
|
||||
t.expects(refused("Objects: {\n Geometry: 1, \"Geometry::A\", \"Mesh\" {\n Vertices: *3 {\n a: 0,0\n }\n }\n}\n", fbx::failure::malformed, "holds 2"));
|
||||
t.expects(refused("FBXHeaderExtension: {\n FBXVersion: 6100\n}\n", fbx::failure::unsupported, "6100"));
|
||||
# A polygon naming a point the mesh hasn't.
|
||||
var broken := fbx::decode("Objects: {\n Geometry: 1, \"Geometry::A\", \"Mesh\" {\n Vertices: *3 {\n a: 0,0,0\n }\n PolygonVertexIndex: *3 {\n a: 0,0,-3\n }\n }\n}\n").expect("The text is read.");
|
||||
t.expects(fbx::scene::of(move(broken)) is error(e) && e.kind == fbx::failure::malformed);
|
||||
}
|
||||
@@ -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));
|
||||
}
|
||||
@@ -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 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));
|
||||
}
|
||||
@@ -0,0 +1,196 @@
|
||||
#! Contains the tests of `obj`: a cube read with its materials, of every form
|
||||
#! of corner, colors, groups, and smoothing; lines and points, and an MTL file
|
||||
#! that isn't there; what's refused; and a model written as OBJ and MTL and
|
||||
#! read back the same.
|
||||
|
||||
import std::io;
|
||||
import std::math;
|
||||
import std::testing;
|
||||
import malunal::linear;
|
||||
import malunal::obj;
|
||||
|
||||
# The folder the tests write their files into.
|
||||
const written: strview = ".blossom/obj tests";
|
||||
|
||||
# Writes a file the tests read, beneath `written`, making its folder.
|
||||
def put(at: strview, text: strview): 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, text).expect("The file is written.");
|
||||
}
|
||||
|
||||
# Whether two floats are within a millionth.
|
||||
def near(a: f32, b: f32): bool {
|
||||
return math::abs(a - b) < 1e-5;
|
||||
}
|
||||
|
||||
# The materials of the cube: red, its texture mapped with options, and blue,
|
||||
# physically based, half see-through.
|
||||
const cube_mtl: strview = """
|
||||
# Two materials
|
||||
newmtl red
|
||||
Ka 0.1 0.1 0.1
|
||||
Kd 1 0 0
|
||||
Ks 0.5 0.5 0.5
|
||||
Ns 96
|
||||
illum 2
|
||||
map_Kd -s 2 2 1 -o 0.5 0 -bm 0.25 -clamp on textures/red brick.png
|
||||
bump -bm 2 bumps.png
|
||||
|
||||
newmtl blue
|
||||
Kd 0 0 1
|
||||
Tr 0.5
|
||||
Pr 0.25
|
||||
Pm 1
|
||||
norm blue_normal.png
|
||||
""";
|
||||
|
||||
# A cube of six quads, its corners given every way: positions alone, with
|
||||
# places on a texture, with normals, and with both, some counted back from the
|
||||
# last; colors, a comment after a statement, two groups, smoothing on and off,
|
||||
# and a material no MTL file gives.
|
||||
const cube_obj: strview = """
|
||||
# A cube
|
||||
mtllib cube.mtl
|
||||
o Cube
|
||||
v -1 -1 -1 1 0 0
|
||||
v 1 -1 -1 0 1 0
|
||||
v 1 1 -1 0 0 1
|
||||
v -1 1 -1
|
||||
v -1 -1 1
|
||||
v 1 -1 1
|
||||
v 1 1 1 0.5 0.5 0.5
|
||||
v -1 1 1
|
||||
vt 0 0
|
||||
vt 1 0
|
||||
vt 1 1
|
||||
vt 0 1
|
||||
vn 0 0 -1
|
||||
vn 0 0 1
|
||||
g front
|
||||
usemtl red
|
||||
s 1
|
||||
f 1 4 3 2
|
||||
f 5/1 6/2 7/3 8/4 # the back
|
||||
g sides
|
||||
usemtl blue
|
||||
s off
|
||||
f 1//1 2//1 6//1 5//1
|
||||
f -5/-4/-1 -1/-3/-1 -2/-2/-1 -6/-1/-1
|
||||
usemtl green
|
||||
f 1 5 8 4
|
||||
f 2 3 7 6
|
||||
""";
|
||||
|
||||
@test def reads_a_cube_and_its_materials(t: &mut tester): void {
|
||||
put("cube.mtl", cube_mtl);
|
||||
put("cube.obj", cube_obj);
|
||||
val cube := obj::load($"{written}/cube.obj".span).expect("The cube is read.");
|
||||
t.expects(cube.positions.length == 8 && cube.uvs.length == 4 && cube.normals.length == 2 && cube.faces.length == 6);
|
||||
t.expects(near(cube.positions[6].x, 1) && near(cube.positions[6].z, 1));
|
||||
# Colors, the line joined to the next among them, white where none's given.
|
||||
t.expects(cube.colors.length == 8 && near(cube.colors[0].x, 1) && near(cube.colors[2].z, 1) && near(cube.colors[3].x, 1) && near(cube.colors[6].y, 0.5));
|
||||
# Every form of corner, and those counted back from the last.
|
||||
val back := cube.corners_of(&cube.faces[1]);
|
||||
t.expects(back.length == 4 && back[0].position == 4 && back[0].uv == 0 && back[0].normal == obj::absent);
|
||||
val side := cube.corners_of(&cube.faces[2]);
|
||||
t.expects(side[1].position == 1 && side[1].uv == obj::absent && side[1].normal == 0);
|
||||
val counted := cube.corners_of(&cube.faces[3]);
|
||||
t.expects(counted[0].position == 3 && counted[0].uv == 0 && counted[0].normal == 1 && counted[1].position == 7 && counted[3].uv == 3);
|
||||
# Objects, groups, and smoothing.
|
||||
t.expects(cube.objects.length == 1 && cube.objects[0].span == "Cube" && cube.groups.length == 2 && cube.groups[1].span == "sides");
|
||||
t.expects(cube.faces[0].object == 0 && cube.faces[0].group == 0 && cube.faces[0].smoothing == 1 && cube.faces[2].group == 1 && cube.faces[2].smoothing == 0);
|
||||
# The materials of the MTL file, and the one it doesn't give, of the defaults.
|
||||
t.expects(cube.libraries.length == 1 && cube.missing.length == 0 && cube.materials.length == 3);
|
||||
val red := &cube.materials[0];
|
||||
t.expects(red.name.span == "red" && near(red.diffuse.x, 1) && near(red.diffuse.y, 0) && near(red.ambient.x, 0.1) && near(red.shininess, 96) && red.illumination == 2);
|
||||
t.expects(cube.faces[0].material == 0 && cube.faces[2].material == 1 && cube.faces[4].material == 2 && cube.materials[2].name.span == "green");
|
||||
if red.map(obj::map_slot::diffuse) is some(&brick) {
|
||||
t.expects(brick.file.span == "textures/red brick.png" && near(brick.scale.x, 2) && near(brick.scale.z, 1) && near(brick.offset.x, 0.5) && near(brick.bump_multiplier, 0.25) && brick.clamp);
|
||||
} else {
|
||||
t.expects(false);
|
||||
}
|
||||
t.expects(red.map(obj::map_slot::bump) is some(&bumps) && bumps.file.span == "bumps.png" && near(bumps.bump_multiplier, 2));
|
||||
val blue := &cube.materials[1];
|
||||
t.expects(near(blue.dissolve, 0.5) && near(blue.roughness, 0.25) && near(blue.metallic, 1) && blue.map(obj::map_slot::normal) is some(_));
|
||||
}
|
||||
|
||||
@test def reads_lines_and_points_and_notes_missing_libraries(t: &mut tester): void {
|
||||
val text: strview = """
|
||||
mtllib nowhere.mtl
|
||||
v 0 0 0
|
||||
v 1 0 0
|
||||
v 1 1 0
|
||||
vt 0 0
|
||||
vt 1 1
|
||||
usemtl line
|
||||
l 1/1 2/2 3
|
||||
p 1 2 3
|
||||
""";
|
||||
val read := obj::decode(text, written).expect("The lines are read.");
|
||||
t.expects(read.lines.length == 1 && read.lines[0].count == 3 && read.points.length == 1 && read.points[0].count == 3 && read.faces.length == 0);
|
||||
t.expects(read.corners_of(&read.lines[0])[1].uv == 1);
|
||||
t.expects(read.missing.length == 1 && read.missing[0].span == "nowhere.mtl" && read.materials.length == 1 && read.lines[0].material == 0);
|
||||
# A line ending with `\` goes on in the next.
|
||||
val joined := obj::decode("v 1 2 \\\n 3 4 5 6\r\nv 0 0 0\n").expect("The joined line is read.");
|
||||
t.expects(joined.positions.length == 2 && near(joined.positions[0].z, 3) && joined.colors.length == 2 && near(joined.colors[0].z, 6) && near(joined.colors[1].x, 1));
|
||||
}
|
||||
|
||||
# Whether text is refused as malformed, saying what's given.
|
||||
def refused(text: strview, says: strview): bool {
|
||||
if obj::decode(text) is error(e) {
|
||||
val why := e.describe();
|
||||
return e.kind == obj::failure::malformed && why.span.find(says) is some(_);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@test def refuses_what_breaks_obj(t: &mut tester): void {
|
||||
t.expects(refused("v 0 0 0\nv 1 0 0\nf 1 2\n", "line 3: Its face has 2 corners"));
|
||||
t.expects(refused("v 0 0 0\nv 1 0 0\nv 1 1 0\nf 1 2 4\n", "names position 4, of the 3"));
|
||||
t.expects(refused("v 0 0 0\nf 0 1 1\n", "names nothing"));
|
||||
t.expects(refused("v 0 zero 0\n", "`zero` isn't a number"));
|
||||
t.expects(refused("v 0 0\n", "of the 3 it needs"));
|
||||
t.expects(refused("v 0 0 0\nv 1 0 0\nv 1 1 0\nf 1/9 2 3\n", "names texture place 9"));
|
||||
t.expects(obj::decode_materials("Kd 1 0 0\n") is error(e) && e.kind == obj::failure::malformed);
|
||||
t.expects(obj::load("nowhere/nothing.obj") is error(gone) && gone.kind == obj::failure::unreadable);
|
||||
}
|
||||
|
||||
@test def writes_a_model_and_its_materials_and_reads_them_back(t: &mut tester): void {
|
||||
put("cube.mtl", cube_mtl);
|
||||
put("cube.obj", cube_obj);
|
||||
val cube := obj::load($"{written}/cube.obj".span).expect("The cube is read.");
|
||||
obj::save(&cube, $"{written}/out/again.obj".span).expect("The cube is written.");
|
||||
val again := obj::load($"{written}/out/again.obj".span).expect("The cube is read again.");
|
||||
t.expects(again.libraries.length == 1 && again.libraries[0].span == "cube.mtl" && again.missing.length == 0);
|
||||
t.expects(again.positions.length == 8 && again.uvs.length == 4 && again.normals.length == 2 && again.colors.length == 8 && again.faces.length == 6);
|
||||
for i in 0..cube.corners.length {
|
||||
val a := cube.corners[i];
|
||||
val b := again.corners[i];
|
||||
t.expects(a.position == b.position && a.uv == b.uv && a.normal == b.normal);
|
||||
}
|
||||
for i in 0..cube.faces.length {
|
||||
val a := cube.faces[i];
|
||||
val b := again.faces[i];
|
||||
t.expects(a.count == b.count && a.material == b.material && a.smoothing == b.smoothing && a.group == b.group && a.object == b.object);
|
||||
}
|
||||
t.expects(near(again.colors[6].x, 0.5) && near(again.positions[0].x, -1));
|
||||
t.expects(again.materials.length == 3 && again.materials[2].name.span == "green");
|
||||
val red := &again.materials[0];
|
||||
t.expects(near(red.ambient.x, 0.1) && near(red.shininess, 96) && red.maps.length == 2);
|
||||
t.expects(red.map(obj::map_slot::diffuse) is some(&brick) && brick.file.span == "textures/red brick.png" && near(brick.scale.y, 2) && near(brick.offset.x, 0.5) && brick.clamp && near(brick.bump_multiplier, 0.25));
|
||||
t.expects(near(again.materials[1].dissolve, 0.5) && near(again.materials[1].metallic, 1));
|
||||
# A model of no materials is written alone.
|
||||
var bare := obj::model{};
|
||||
bare.positions.append(vec3{ 0, 0, 0 });
|
||||
bare.positions.append(vec3{ 1, 0, 0 });
|
||||
bare.positions.append(vec3{ 0, 1, 0 });
|
||||
bare.corners.append(obj::corner{ position = 0 });
|
||||
bare.corners.append(obj::corner{ position = 1 });
|
||||
bare.corners.append(obj::corner{ position = 2 });
|
||||
bare.faces.append(obj::element{ first = 0, count = 3 });
|
||||
val text := obj::encode(&bare);
|
||||
t.expects(text.span.find("mtllib") is none && text.span.find("f 1 2 3") is some(_));
|
||||
}
|
||||
@@ -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 malunal::linear;
|
||||
import malunal::gltf;
|
||||
|
||||
# Checks what the quad holds, read any way.
|
||||
def expect_quad(t: &mut tester, read: &gltf::asset): void {
|
||||
t.expects(read.version.span == "2.0" && read.generator.span == "gltf tests");
|
||||
t.expects(read.scene is some(s) && s == 0 && read.scenes.length == 1 && read.scenes[0].name.span == "Quad");
|
||||
t.expects(read.nodes.length == 1 && read.nodes[0].name.span == "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.span == "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.span);
|
||||
val json := quad_json(uri.span);
|
||||
val read := gltf::decode(json.span) else (e) {
|
||||
t.expects(false);
|
||||
io::println(e.describe().span);
|
||||
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.span, bytes.span);
|
||||
put("quad.glb", glb.span);
|
||||
val read := gltf::load(".blossom/gltf tests/quad.glb") else (e) {
|
||||
t.expects(false);
|
||||
io::println(e.describe().span);
|
||||
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.span);
|
||||
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.span);
|
||||
val read := gltf::load(".blossom/gltf tests/beside/quad.gltf") else (e) {
|
||||
t.expects(false);
|
||||
io::println(e.describe().span);
|
||||
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.span, "\"byteLength\": 108", "\"byteLength\": 116").span,
|
||||
"{ \"buffer\": 0, \"byteOffset\": 96, \"byteLength\": 12 }",
|
||||
"{ \"buffer\": 0, \"byteOffset\": 96, \"byteLength\": 12 }, { \"buffer\": 0, \"byteOffset\": 108, \"byteLength\": 8 }");
|
||||
val glb := glb_of(with_image.span, packed_bytes.span);
|
||||
val bound := gltf::decode(glb.span) else (e) {
|
||||
t.expects(false);
|
||||
io::println(e.describe().span);
|
||||
return;
|
||||
};
|
||||
t.expects(bound.images[0].mime_type.span == "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}".span);
|
||||
t.expects(false);
|
||||
}
|
||||
error(e) => {
|
||||
val why := e.describe();
|
||||
val found := holds(why.span, says);
|
||||
if e.kind != kind || !found {
|
||||
io::println($"refused as `{why.span}`, not as `{says}`".span);
|
||||
}
|
||||
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\"]").span, gltf::failure::unsupported, "KHR_draco_mesh_compression");
|
||||
refuses(t, asset_with("\"nodes\": [{ \"mesh\": 0 }]").span, gltf::failure::malformed, "nodes[0]: its mesh is 0, of the 0 meshes");
|
||||
refuses(t, asset_with("\"nodes\": [{ \"children\": [1] }, { \"children\": [0] }]").span, gltf::failure::malformed, "comes back to it");
|
||||
refuses(t, asset_with("\"nodes\": [{ \"children\": [1] }, {}, { \"children\": [1] }]").span, gltf::failure::malformed, "child of both");
|
||||
refuses(t, asset_with("\"nodes\": [{ \"children\": [1] }, {}], \"scenes\": [{ \"nodes\": [1] }]").span, gltf::failure::malformed, "not a root");
|
||||
refuses(t, asset_with("\"buffers\": [{ \"byteLength\": 8 }]").span, gltf::failure::malformed, "buffers[0]");
|
||||
refuses(t, asset_with("\"buffers\": [{ \"byteLength\": 8, \"uri\": \"missing.bin\" }]").span, gltf::failure::unreadable, "missing.bin");
|
||||
refuses(t, asset_with("\"buffers\": [{ \"byteLength\": 4, \"uri\": \"https://example.com/a.bin\" }]").span, gltf::failure::unsupported, "another place");
|
||||
refuses(t, asset_with("\"buffers\": [{ \"byteLength\": 4, \"uri\": \"data:application/octet-stream;base64,AAAA\" }]").span, 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\" }]").span).span,
|
||||
gltf::failure::malformed, "reach past");
|
||||
refuses(t, asset_with(joined(one_view, ", \"accessors\": [{ \"bufferView\": 0, \"componentType\": 5124, \"count\": 1, \"type\": \"SCALAR\" }]").span).span,
|
||||
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 }]").span).span,
|
||||
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("").span, none.span);
|
||||
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.span) else (_) {
|
||||
t.expects(false);
|
||||
return;
|
||||
};
|
||||
t.expects(read.extensions.length == 1 && read.extensions[0].span == "KHR_texture_transform" && read.nodes.length == 1);
|
||||
}
|
||||
@@ -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 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;
|
||||
}
|
||||
@@ -0,0 +1,240 @@
|
||||
#! Contains the tests of writing assets: one built by hand, as `.glb` and as
|
||||
#! `.gltf` JSON, read back; one read, written, and read again, its every part;
|
||||
#! and files saved, beside it where they're named so.
|
||||
|
||||
import std::io;
|
||||
import std::testing;
|
||||
import malunal::linear;
|
||||
import malunal::gltf;
|
||||
|
||||
# A quad built by hand, of positions, normals, and indices, a red material, a
|
||||
# node placed and turned, and a PNG's first bytes as an image.
|
||||
def built_quad(): gltf::asset {
|
||||
var made := gltf::asset::new("gltf tests");
|
||||
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];
|
||||
val indices: u32[6] = [0, 1, 2, 0, 2, 3];
|
||||
val p := made.add_floats(places[:], gltf::element::vec3, "places");
|
||||
val n := made.add_floats(normals[:], gltf::element::vec3);
|
||||
val i := made.add_indices(indices[:]);
|
||||
var part := gltf::primitive{ indices = i, material = 0 };
|
||||
part.attributes.append(gltf::attribute{ name = string::from("POSITION", nothing{}), accessor = p });
|
||||
part.attributes.append(gltf::attribute{ name = string::from("NORMAL", nothing{}), accessor = n });
|
||||
var shape := gltf::mesh{ name = string::from("quad", nothing{}) };
|
||||
shape.primitives.append(move(part));
|
||||
made.meshes.append(move(shape));
|
||||
var red := gltf::material{ name = string::from("red", nothing{}), metallic = 0, double_sided = true };
|
||||
red.base_color = [1, 0, 0, 1];
|
||||
red.emissive = [4, 2, 0];
|
||||
made.materials.append(move(red));
|
||||
val picture: u8[8] = [0x89, 'P', 'N', 'G', 13, 10, 26, 10];
|
||||
_ = made.add_image(picture[:]);
|
||||
made.textures.append(gltf::texture{ source = 0 });
|
||||
var square := gltf::node{ name = string::from("Square", nothing{}), mesh = 0 };
|
||||
square.translation = vec3{ 1, 2, 3 };
|
||||
square.rotation = quat::axis_angle(vec3::up, 0.5);
|
||||
made.nodes.append(move(square));
|
||||
var whole := gltf::scene{ name = string::from("Quad", nothing{}) };
|
||||
whole.nodes.append(0);
|
||||
made.scenes.append(move(whole));
|
||||
made.scene = 0;
|
||||
return made;
|
||||
}
|
||||
|
||||
# Checks what the quad built holds, read back.
|
||||
def expect_built(t: &mut tester, read: &gltf::asset): void {
|
||||
t.expects(read.version.span == "2.0" && read.generator.span == "gltf tests");
|
||||
t.expects(read.scene is some(s) && s == 0 && read.scenes[0].name.span == "Quad" && read.scenes[0].nodes.length == 1);
|
||||
val node := &read.nodes[0];
|
||||
t.expects(node.name.span == "Square" && node.mesh is some(m) && m == 0 && near(node.translation.z, 3));
|
||||
val turned := quat::axis_angle(vec3::up, 0.5);
|
||||
t.expects(near(node.rotation.y, turned.y) && near(node.rotation.w, turned.w) && near(node.scale.x, 1));
|
||||
val part := &read.meshes[0].primitives[0];
|
||||
t.expects(read.meshes[0].name.span == "quad" && part.find("POSITION") is some(at) && at == 0 && part.material is some(k) && k == 0);
|
||||
val places := read.floats(0);
|
||||
t.expects(places.length == 12 && near(places[3], 0.5) && near(places[10], 0.5));
|
||||
t.expects(read.accessors[0].name.span == "places" && read.accessors[0].min.length == 3 && read.accessors[0].min[0] == -0.5 && read.accessors[0].max[1] == 0.5);
|
||||
val normals := read.floats(1);
|
||||
t.expects(normals.length == 12 && near(normals[11], 1));
|
||||
val indices := read.integers(2);
|
||||
t.expects(indices.length == 6 && indices[2] == 2 && indices[5] == 3 && read.accessors[2].component_type == gltf::component::unsigned_short);
|
||||
val red := &read.materials[0];
|
||||
t.expects(red.name.span == "red" && red.double_sided && near(red.base_color[0], 1) && near(red.base_color[1], 0) && near(red.metallic, 0));
|
||||
# Emission past 1 comes back by its strength.
|
||||
t.expects(near(red.emissive[0], 4) && near(red.emissive[1], 2) && near(red.emissive[2], 0));
|
||||
t.expects(read.images.length == 1 && read.images[0].bytes.length == 8 && read.images[0].bytes[1] == 'P' && read.images[0].mime_type.span == "image/png");
|
||||
t.expects(read.textures[0].source is some(src) && src == 0);
|
||||
}
|
||||
|
||||
@test def writes_what_it_built_as_glb_and_json(t: &mut tester): void {
|
||||
val built := built_quad();
|
||||
val glb := gltf::encode(&built, true) else (e) {
|
||||
t.expects(false);
|
||||
io::println(e.describe().span);
|
||||
return;
|
||||
};
|
||||
t.expects(glb.length % 4 == 0 && glb[0] == 'g' && glb[1] == 'l' && glb[2] == 'T' && glb[3] == 'F');
|
||||
val from_glb := gltf::decode(glb.span) else (e) {
|
||||
t.expects(false);
|
||||
io::println(e.describe().span);
|
||||
return;
|
||||
};
|
||||
expect_built(t, &from_glb);
|
||||
t.expects(from_glb.buffers.length == 1 && from_glb.buffers[0].uri.length == 0);
|
||||
|
||||
val json := gltf::encode(&built, false) else (e) {
|
||||
t.expects(false);
|
||||
io::println(e.describe().span);
|
||||
return;
|
||||
};
|
||||
val text := string::from(strview::over(json.data, json.length), nothing{});
|
||||
t.expects(text.span.find("\"uri\":\"data:application/octet-stream;base64,") is some(_));
|
||||
t.expects(text.span.find("KHR_materials_emissive_strength") is some(_) && text.span.find("\"emissiveStrength\":4") is some(_));
|
||||
val from_json := gltf::decode(json.span) else (e) {
|
||||
t.expects(false);
|
||||
io::println(e.describe().span);
|
||||
return;
|
||||
};
|
||||
expect_built(t, &from_json);
|
||||
}
|
||||
|
||||
@test def writes_what_it_read_as_it_was(t: &mut tester): void {
|
||||
# Every part, read of JSON: lights, a camera of each kind, a skin, an
|
||||
# animation, a sampler, textures of a material, and a node's children.
|
||||
# The quad's buffer, and the animation's keys, two times and two rotations,
|
||||
# in a buffer of their own, each a data URI.
|
||||
val bytes := quad_buffer();
|
||||
val quad_uri := data_uri(bytes.span);
|
||||
var keys := packed::new();
|
||||
val times: f32[2] = [0, 1];
|
||||
val turns: f32[8] = [0, 0, 0, 1, 0, 0.7071068, 0, 0.7071068];
|
||||
keys.floats(times[:]);
|
||||
keys.floats(turns[:]);
|
||||
val keys_uri := data_uri(keys.bytes.span);
|
||||
val whole := string::from("""
|
||||
{
|
||||
"asset": { "version": "2.0", "generator": "gltf tests" },
|
||||
"extensionsUsed": ["KHR_lights_punctual", "KHR_materials_unlit", "EXT_unknown"],
|
||||
"scene": 0,
|
||||
"scenes": [{ "name": "Quad", "nodes": [0] }],
|
||||
"nodes": [
|
||||
{ "name": "Square", "mesh": 0, "translation": [1, 2, 3], "children": [1], "skin": 0 },
|
||||
{ "name": "Lamp", "scale": [2, 2, 2], "extensions": { "KHR_lights_punctual": { "light": 1 } } }
|
||||
],
|
||||
"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 },
|
||||
"normalTexture": { "index": 0, "scale": 0.5 }, "extensions": { "KHR_materials_unlit": {} } }],
|
||||
"textures": [{ "sampler": 0, "source": 0 }],
|
||||
"samplers": [{ "magFilter": 9728, "minFilter": 9987, "wrapS": 33071 }],
|
||||
"images": [{ "uri": "data:image/png;base64,iVBORw0KGgo=" }],
|
||||
"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" },
|
||||
{ "bufferView": 2, "componentType": 5126, "count": 2, "type": "SCALAR" },
|
||||
{ "bufferView": 2, "byteOffset": 8, "componentType": 5126, "count": 2, "type": "VEC4" }
|
||||
],
|
||||
"bufferViews": [
|
||||
{ "buffer": 0, "byteOffset": 0, "byteLength": 96 },
|
||||
{ "buffer": 0, "byteOffset": 96, "byteLength": 12 },
|
||||
{ "buffer": 1, "byteLength": 40 }
|
||||
],
|
||||
"buffers": [{ "byteLength": 108, "uri": "QUAD" }, { "byteLength": 40, "uri": "KEYS" }],
|
||||
"cameras": [
|
||||
{ "name": "Eye", "type": "perspective", "perspective": { "yfov": 0.8, "znear": 0.1 } },
|
||||
{ "type": "orthographic", "orthographic": { "xmag": 2, "ymag": 1, "znear": 0, "zfar": 50 } }
|
||||
],
|
||||
"skins": [{ "joints": [0] }],
|
||||
"animations": [{ "name": "Spin",
|
||||
"samplers": [{ "input": 3, "output": 4, "interpolation": "STEP" }],
|
||||
"channels": [{ "sampler": 0, "target": { "node": 0, "path": "rotation" } }] }],
|
||||
"extensions": { "KHR_lights_punctual": { "lights": [
|
||||
{ "name": "Sun", "type": "directional", "intensity": 3 },
|
||||
{ "type": "spot", "color": [1, 0.5, 0], "range": 10, "spot": { "innerConeAngle": 0.2, "outerConeAngle": 0.4 } }
|
||||
] } }
|
||||
}
|
||||
""", nothing{});
|
||||
val json := replaced(replaced(whole.span, "QUAD", quad_uri.span).span, "KEYS", keys_uri.span);
|
||||
val read := gltf::decode(json.span) else (e) {
|
||||
t.expects(false);
|
||||
io::println(e.describe().span);
|
||||
return;
|
||||
};
|
||||
for binary in [false, true] {
|
||||
val written := gltf::encode(&read, binary) else (e) {
|
||||
t.expects(false);
|
||||
io::println(e.describe().span);
|
||||
return;
|
||||
};
|
||||
val again := gltf::decode(written.span) else (e) {
|
||||
t.expects(false);
|
||||
io::println(e.describe().span);
|
||||
return;
|
||||
};
|
||||
t.expects(again.generator.span == "gltf tests" && again.scene is some(s) && s == 0 && again.scenes[0].name.span == "Quad");
|
||||
t.expects(again.nodes[0].name.span == "Square" && near(again.nodes[0].translation.y, 2) && again.nodes[0].children.length == 1);
|
||||
val places := again.floats(0);
|
||||
val indices := again.integers(2);
|
||||
t.expects(places.length == 12 && near(places[3], 0.5) && indices.length == 6 && indices[5] == 3);
|
||||
t.expects(again.accessors[0].max.length == 3 && again.accessors[0].max[0] == 0.5 && again.accessors[1].offset == 48);
|
||||
val red := &again.materials[0];
|
||||
t.expects(red.name.span == "red" && red.double_sided && near(red.base_color[1], 0) && near(red.metallic, 0) && near(red.roughness, 1));
|
||||
t.expects(again.lights.length == 2 && again.lights[0].name.span == "Sun" && again.lights[0].kind == gltf::light_kind::directional && near(again.lights[0].intensity, 3));
|
||||
t.expects(again.lights[1].kind == gltf::light_kind::spot && near(again.lights[1].color[1], 0.5) && again.lights[1].range is some(r) && near(r, 10) && near(again.lights[1].outer_cone, 0.4));
|
||||
t.expects(again.cameras.length == 2 && near(again.cameras[0].fov_y, 0.8) && again.cameras[0].far is none && again.cameras[1].kind == gltf::camera_kind::orthographic);
|
||||
t.expects(again.cameras[1].far is some(f) && near(f, 50) && near(again.cameras[1].x_mag, 2));
|
||||
t.expects(again.samplers[0].mag_filter == gltf::filter::nearest && again.samplers[0].min_filter == gltf::filter::linear && again.samplers[0].mip_filter == gltf::filter::linear);
|
||||
t.expects(again.samplers[0].wrap_s == gltf::wrap::clamp && again.samplers[0].wrap_t == gltf::wrap::repeat);
|
||||
t.expects(again.images[0].bytes.length == 8 && again.images[0].mime_type.span == "image/png");
|
||||
t.expects(again.materials[0].unlit && again.materials[0].normal_texture is some(nt) && near(nt.scale, 0.5));
|
||||
t.expects(again.skins.length == 1 && again.skins[0].joints[0] == 0 && again.nodes[0].skin is some(sk) && sk == 0);
|
||||
t.expects(again.nodes.length == 2 && again.nodes[1].parent is some(up) && up == 0 && again.nodes[1].light is some(l) && l == 1 && near(again.nodes[1].scale.y, 2));
|
||||
val spin := &again.animations[0];
|
||||
t.expects(spin.name.span == "Spin" && spin.samplers[0].interpolation == gltf::interpolation::step && spin.channels[0].path == gltf::target_path::rotation);
|
||||
val turned := again.floats(spin.samplers[0].output);
|
||||
t.expects(turned.length == 8 && near(turned[5], 0.7071068));
|
||||
# The extension it didn't read isn't written.
|
||||
val text := string::from(strview::over(written.data, written.length), nothing{});
|
||||
t.expects(text.span.find("EXT_unknown") is none && text.span.find("KHR_materials_unlit") is some(_));
|
||||
# A .glb file's first buffer, the keys', is its binary chunk; the quad's
|
||||
# is a data URI.
|
||||
if binary {
|
||||
t.expects(again.buffers[0].uri.length == 0 && again.buffers[1].uri.span.starts_with("data:"));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@test def saves_files_and_those_beside_them(t: &mut tester): void {
|
||||
var built := built_quad();
|
||||
# Its buffer is a file beside it, named with a space.
|
||||
built.buffers.at(0).uri = string::from("quad%20data.bin", nothing{});
|
||||
gltf::save(&built, ".blossom/gltf tests/saved/quad.gltf") else (e) {
|
||||
t.expects(false);
|
||||
io::println(e.describe().span);
|
||||
return;
|
||||
};
|
||||
t.expects(io::read_file(&path::parse(".blossom/gltf tests/saved/quad data.bin").expect("A path.")) is value(_));
|
||||
val read := gltf::load(".blossom/gltf tests/saved/quad.gltf") else (e) {
|
||||
t.expects(false);
|
||||
io::println(e.describe().span);
|
||||
return;
|
||||
};
|
||||
expect_built(t, &read);
|
||||
|
||||
# As a .glb file, the buffer named a file still is one.
|
||||
gltf::save(&built, ".blossom/gltf tests/saved/quad.glb") else (e) {
|
||||
t.expects(false);
|
||||
io::println(e.describe().span);
|
||||
return;
|
||||
};
|
||||
val glb := gltf::load(".blossom/gltf tests/saved/quad.glb") else (e) {
|
||||
t.expects(false);
|
||||
io::println(e.describe().span);
|
||||
return;
|
||||
};
|
||||
expect_built(t, &glb);
|
||||
t.expects(glb.buffers[0].uri.span == "quad%20data.bin");
|
||||
}
|
||||
Reference in new issue
Block a user