113 lines
4.8 KiB
Plaintext
113 lines
4.8 KiB
Plaintext
#! Contains the tests of reading accessors: integers of each size, normalized
|
|||
|
|
#! or not, interleaved in a view by its stride; a matrix of bytes, each column
|
||
|
|
#! begun at a multiple of 4; and sparse values, over a view's or over zeros.
|
||
|
|
|
||
|
|
import std::io;
|
||
|
|
import std::testing;
|
||
|
|
import gltf;
|
||
|
|
|
||
|
|
# A buffer of 68 bytes, its views as `accessors_json` names them.
|
||
|
|
def accessors_buffer(): vector<u8> {
|
||
|
|
var made := packed::new();
|
||
|
|
# View 0, interleaved by 8: a color of normalized bytes, and two normalized
|
||
|
|
# shorts, for each of two elements.
|
||
|
|
for each in [255u, 0, 128, 255] { made.byte(each as u8); }
|
||
|
|
made.short(32767);
|
||
|
|
made.short(0x8000);
|
||
|
|
for each in [0u, 51, 255, 0] { made.byte(each as u8); }
|
||
|
|
made.short(0);
|
||
|
|
made.short(0xC000);
|
||
|
|
# View 1: signed bytes, -3 and 7.
|
||
|
|
made.byte(253);
|
||
|
|
made.byte(7);
|
||
|
|
made.align();
|
||
|
|
# View 2: a matrix of two columns of two bytes, each column padded to 4.
|
||
|
|
for each in [1u, 2, 0, 0, 3, 4, 0, 0] { made.byte(each as u8); }
|
||
|
|
# View 3: four floats; views 4 and 5: sparse indices 3 and 1, and their values.
|
||
|
|
val four: f32[4] = [10, 20, 30, 40];
|
||
|
|
made.floats(four[:]);
|
||
|
|
made.byte(3);
|
||
|
|
made.byte(1);
|
||
|
|
made.align();
|
||
|
|
val two: f32[2] = [-1, -2];
|
||
|
|
made.floats(two[:]);
|
||
|
|
# View 6: a sparse value of two floats; view 7: its index, 2, a short.
|
||
|
|
val pair: f32[2] = [5, 6];
|
||
|
|
made.floats(pair[:]);
|
||
|
|
made.short(2);
|
||
|
|
made.align();
|
||
|
|
return move(made.bytes);
|
||
|
|
}
|
||
|
|
|
||
|
|
def accessors_json(uri: strview): string {
|
||
|
|
var made := string::from("""
|
||
|
|
{
|
||
|
|
"asset": { "version": "2.0" },
|
||
|
|
"accessors": [
|
||
|
|
{ "bufferView": 0, "componentType": 5121, "normalized": true, "count": 2, "type": "VEC4" },
|
||
|
|
{ "bufferView": 0, "byteOffset": 4, "componentType": 5122, "normalized": true, "count": 2, "type": "VEC2" },
|
||
|
|
{ "bufferView": 1, "componentType": 5120, "count": 2, "type": "SCALAR" },
|
||
|
|
{ "bufferView": 2, "componentType": 5121, "count": 1, "type": "MAT2" },
|
||
|
|
{ "bufferView": 3, "componentType": 5126, "count": 4, "type": "SCALAR",
|
||
|
|
"sparse": { "count": 2, "indices": { "bufferView": 4, "componentType": 5121 }, "values": { "bufferView": 5 } } },
|
||
|
|
{ "componentType": 5126, "count": 3, "type": "VEC2",
|
||
|
|
"sparse": { "count": 1, "indices": { "bufferView": 7, "componentType": 5123 }, "values": { "bufferView": 6 } } },
|
||
|
|
{ "bufferView": 7, "componentType": 5123, "count": 1, "type": "SCALAR" }
|
||
|
|
],
|
||
|
|
"bufferViews": [
|
||
|
|
{ "buffer": 0, "byteOffset": 0, "byteLength": 16, "byteStride": 8 },
|
||
|
|
{ "buffer": 0, "byteOffset": 16, "byteLength": 2 },
|
||
|
|
{ "buffer": 0, "byteOffset": 20, "byteLength": 8 },
|
||
|
|
{ "buffer": 0, "byteOffset": 28, "byteLength": 16 },
|
||
|
|
{ "buffer": 0, "byteOffset": 44, "byteLength": 2 },
|
||
|
|
{ "buffer": 0, "byteOffset": 48, "byteLength": 8 },
|
||
|
|
{ "buffer": 0, "byteOffset": 56, "byteLength": 8 },
|
||
|
|
{ "buffer": 0, "byteOffset": 64, "byteLength": 2 }
|
||
|
|
],
|
||
|
|
"buffers": [{ "byteLength": 68
|
||
|
|
""", nothing{});
|
||
|
|
made.append(", \"uri\": \"");
|
||
|
|
made.append(uri);
|
||
|
|
made.append("\" }] }");
|
||
|
|
return made;
|
||
|
|
}
|
||
|
|
|
||
|
|
# The accessors' asset, read.
|
||
|
|
def read_accessors(t: &mut tester): maybe<gltf::asset> {
|
||
|
|
val bytes := accessors_buffer();
|
||
|
|
t.expects(bytes.length == 68);
|
||
|
|
val uri := data_uri(bytes.view());
|
||
|
|
val json := accessors_json(uri.view());
|
||
|
|
match gltf::decode(json.view()) {
|
||
|
|
value(read) => return read;
|
||
|
|
error(e) => {
|
||
|
|
io::println(e.describe().view());
|
||
|
|
t.expects(false);
|
||
|
|
return nothing{};
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
@test def reads_numbers_of_every_size_interleaved_by_stride(t: &mut tester): void {
|
||
|
|
val read := read_accessors(t) else { return; };
|
||
|
|
val colors := read.floats(0);
|
||
|
|
t.expects(colors.length == 8 && near(colors[0], 1) && near(colors[1], 0) && near(colors[2], 128.0 / 255) && near(colors[3], 1));
|
||
|
|
t.expects(near(colors[4], 0) && near(colors[5], 0.2) && near(colors[6], 1) && near(colors[7], 0));
|
||
|
|
# A normalized short's least is -1, as -32767 is.
|
||
|
|
val shorts := read.floats(1);
|
||
|
|
t.expects(shorts.length == 4 && near(shorts[0], 1) && near(shorts[1], -1) && near(shorts[2], 0) && near(shorts[3], -16384.0 / 32767));
|
||
|
|
val signed := read.floats(2);
|
||
|
|
t.expects(signed.length == 2 && near(signed[0], -3) && near(signed[1], 7));
|
||
|
|
val matrix := read.floats(3);
|
||
|
|
t.expects(matrix.length == 4 && near(matrix[0], 1) && near(matrix[1], 2) && near(matrix[2], 3) && near(matrix[3], 4));
|
||
|
|
val index := read.integers(6);
|
||
|
|
t.expects(index.length == 1 && index[0] == 2);
|
||
|
|
}
|
||
|
|
|
||
|
|
@test def puts_sparse_values_in_place(t: &mut tester): void {
|
||
|
|
val read := read_accessors(t) else { return; };
|
||
|
|
val over_view := read.floats(4);
|
||
|
|
t.expects(over_view.length == 4 && near(over_view[0], 10) && near(over_view[1], -2) && near(over_view[2], 30) && near(over_view[3], -1));
|
||
|
|
val over_zeros := read.floats(5);
|
||
|
|
t.expects(over_zeros.length == 6 && near(over_zeros[0], 0) && near(over_zeros[3], 0) && near(over_zeros[4], 5) && near(over_zeros[5], 6));
|
||
|
|
}
|