Files
models/tests/obj.sakura
T

196 lines
8.6 KiB
Plaintext
Raw Normal View History

2026-10-30 12:04:54 -05:00
#! 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(_));
}