Files
models/obj/reading.sakura
T

253 lines
9.9 KiB
Plaintext
Raw Permalink Normal View History

2026-10-30 12:04:54 -05:00
#! Contains reading an OBJ file's statements into a model: what each gives,
#! its corners' indices checked once all are read, as one may name a position
#! given after it, and the materials of the MTL files it names.
import std::io;
import malunal::linear;
module malunal::obj;
# What the statements read so far say what follows is of.
struct state {
var material: u32 = absent;
var smoothing: u32;
var object: u32 = absent;
var group: u32 = absent;
}
# Reads a model from OBJ's text, the MTL files it names from a folder.
def read_model(text: strview, folder: strview): result<model, obj_error> {
var made := model{};
# The names `usemtl` gives, which elements' materials index until the end.
var named := vector::<string>::new();
var now := state{};
var lines := statements_of(text);
while lines.next() {
val line := lines.line;
var words := words_of(lines.current);
val keyword := words.next();
if keyword == "v" {
read_position(&mut made, &mut words, line) else (e) { return e; };
} else if keyword == "vt" {
val numbers := read_numbers(&mut words, line, 1, 3) else (e) { return e; };
made.uvs.append(vec2{ numbers[0] as f32, numbers[1] as f32 });
} else if keyword == "vn" {
val numbers := read_numbers(&mut words, line, 3, 3) else (e) { return e; };
made.normals.append(vec3{ numbers[0] as f32, numbers[1] as f32, numbers[2] as f32 });
} else if keyword == "f" || keyword == "fo" || keyword == "l" || keyword == "p" {
val first := made.corners.length;
while true {
val word := words.next();
break if word.length == 0;
val held := read_corner(&made, word, line) else (e) { return e; };
made.corners.append(held);
}
val count := made.corners.length - first;
var held := element{ first = first, count = count as u32, material = now.material, smoothing = now.smoothing, object = now.object, group = now.group };
if keyword == "l" {
return at_line(line, $"Its line has {count} points, of the 2 it needs at least.".span) if count < 2;
made.lines.append(held);
} else if keyword == "p" {
return at_line(line, "Its points are none.") if count < 1;
made.points.append(held);
} else {
return at_line(line, $"Its face has {count} corners, of the 3 it needs at least.".span) if count < 3;
made.faces.append(held);
}
} else if keyword == "o" {
now.object = name_index(&mut made.objects, words.rest());
} else if keyword == "g" {
val name := words.rest();
now.group = name.length == 0 ? absent : name_index(&mut made.groups, name);
} else if keyword == "s" {
val word := words.next();
if word == "off" || word.length == 0 {
now.smoothing = 0;
} else {
val value := integer_of(word) else { return at_line(line, $"`{word}` isn't a smoothing group.".span); };
now.smoothing = value as u32;
}
} else if keyword == "usemtl" {
val name := words.rest();
now.material = name.length == 0 ? absent : name_index(&mut named, name);
} else if keyword == "mtllib" {
read_libraries(&mut made, &mut words, folder);
}
# Other statements, of curves and surfaces, and of display, are passed over.
}
check_corners(&made) else (e) { return e; };
read_materials(&mut made, folder) else (e) { return e; };
name_materials(&mut made, &named);
return made;
}
# Reads a position, and its color, where it has one.
def read_position(made: &mut model, words: &mut words, line: u64): result<void, obj_error> {
val numbers := read_numbers(words, line, 3, 7) else (e) { return e; };
made.positions.append(vec3{ numbers[0] as f32, numbers[1] as f32, numbers[2] as f32 });
# Three more are a color; a fourth alone is a weight, which isn't kept.
if numbers[7] >= 6 {
while made.colors.length + 1 < made.positions.length {
made.colors.append(vec3::one);
}
made.colors.append(vec3{ numbers[3] as f32, numbers[4] as f32, numbers[5] as f32 });
} else if made.colors.length != 0 {
made.colors.append(vec3::one);
}
return;
}
# Reads from a least to a most numbers, those not given 0; how many were given
# is the last.
def read_numbers(words: &mut words, line: u64, least: u64, most: u64): result<f64[8], obj_error> {
var made: f64[8];
var count: u64 = 0;
while count < most {
val word := words.next();
break if word.length == 0;
val value := number_of(word) else { return at_line(line, $"`{word}` isn't a number.".span); };
made[count] = value;
count++;
}
return at_line(line, $"It gives {count} numbers, of the {least} it needs at least.".span) if count < least;
made[7] = count as f64;
return made;
}
# Reads a corner, `v`, `v/vt`, `v//vn`, or `v/vt/vn`, each index counted from 1,
# or back from the last given where it's negative.
def read_corner(made: &model, word: strview, line: u64): result<corner, obj_error> {
var parts: strview[3];
var count: u64 = 0;
var begun: u64 = 0;
for i in 0..=word.length {
if i == word.length || word[i] == '/' {
return at_line(line, $"`{word}` isn't a corner.".span) if count == 3;
parts[count] = word.substr(begun, i - begun);
count++;
begun = i + 1;
}
}
var held := corner{};
held.position = resolve(parts[0], made.positions.length, word, line) else (e) { return e; };
if count > 1 && parts[1].length != 0 {
held.uv = resolve(parts[1], made.uvs.length, word, line) else (e) { return e; };
}
if count > 2 && parts[2].length != 0 {
held.normal = resolve(parts[2], made.normals.length, word, line) else (e) { return e; };
}
return held;
}
# An index, counted from 0, of one counted from 1, or back from the last of
# those given so far where it's negative.
def resolve(text: strview, given: u64, word: strview, line: u64): result<u32, obj_error> {
val value := integer_of(text) else { return at_line(line, $"`{word}` isn't a corner.".span); };
if value > 0 {
return at_line(line, $"`{word}` names more than there may be.".span) if value > 0xFFFFFFFE;
return (value - 1) as u32;
}
val back := given as i64 + value;
return at_line(line, $"`{word}` names nothing: an index is counted from 1, or back from -1.".span) if value == 0 || back < 0;
return back as u32;
}
# The index of a name in a list, added where it isn't there.
def name_index(names: &mut vector<string>, name: strview): u32 {
for i in 0..names.length {
return i as u32 if names[i].span == name;
}
names.append(string::from(name, nothing{}));
return (names.length - 1) as u32;
}
# Notes the MTL files a statement names: its words, or the whole of it where
# that names a file beside the model, as a name with a space.
def read_libraries(made: &mut model, words: &mut words, folder: strview): void {
val whole := words.rest();
if folder.length != 0 && whole.find(" ") is some(_) {
val at := path::parse($"{folder}/{whole}".span) else (_) { return; };
if io::exists(&at) {
_ = name_index(&mut made.libraries, whole);
return;
}
}
var each := words_of(whole);
while true {
val word := each.next();
break if word.length == 0;
_ = name_index(&mut made.libraries, word);
}
}
# Checks each corner names a position, a place on a texture, and a normal there
# are.
def check_corners(made: &model): result<void, obj_error> {
for i in 0..made.corners.length {
val held := made.corners[i];
if held.position as u64 >= made.positions.length {
return failed(failure::malformed, $"Corner {i} names position {held.position + 1}, of the {made.positions.length} there are.".span);
}
if held.uv != absent && (held.uv as u64) >= made.uvs.length {
return failed(failure::malformed, $"Corner {i} names texture place {held.uv + 1}, of the {made.uvs.length} there are.".span);
}
if held.normal != absent && (held.normal as u64) >= made.normals.length {
return failed(failure::malformed, $"Corner {i} names normal {held.normal + 1}, of the {made.normals.length} there are.".span);
}
}
return;
}
# Reads the materials of the MTL files the model names, each name kept by the
# first that gives it; those that can't be read are noted as missing.
def read_materials(made: &mut model, folder: strview): result<void, obj_error> {
for k in 0..made.libraries.length {
val name := made.libraries[k].span;
if folder.length == 0 {
made.missing.append(string::from(name, nothing{}));
continue;
}
val at := path::parse($"{folder}/{name}".span) else (_) {
made.missing.append(string::from(name, nothing{}));
continue;
};
val text := io::read_file(&at) else (_) {
made.missing.append(string::from(name, nothing{}));
continue;
};
var read := decode_materials(text.span) else (e) {
val why := e.describe();
return failed(failure::malformed, $"{name}: {why.span}".span);
};
for i in 0..read.length {
continue if made.material_named(read[i].name.span) is some(_);
made.materials.append(move(read[i]));
}
}
return;
}
# Gives each element the index of its material in the model's, those named by
# `usemtl` that no MTL file gave made of the defaults.
def name_materials(made: &mut model, named: &vector<string>): void {
var index := vector::<u32>::new();
for i in 0..named.length {
if made.material_named(named[i].span) is some(found) {
index.append(found as u32);
} else {
made.materials.append(material::named(named[i].span));
index.append((made.materials.length - 1) as u32);
}
}
for i in 0..made.faces.length {
val m := made.faces[i].material;
made.faces[i].material = m == absent ? absent : index[m as u64];
}
for i in 0..made.lines.length {
val m := made.lines[i].material;
made.lines[i].material = m == absent ? absent : index[m as u64];
}
for i in 0..made.points.length {
val m := made.points[i].material;
made.points[i].material = m == absent ? absent : index[m as u64];
}
}