158 lines
6.1 KiB
Plaintext
158 lines
6.1 KiB
Plaintext
#! Contains `model`, a model read from Wavefront's OBJ, why one couldn't be,
|
|
#! and reading one, from a file or from text, with the materials of the MTL
|
|
#! files it names.
|
|
#!
|
|
#! An OBJ file is lines of text, each a statement: `v`, `vt`, and `vn` give
|
|
#! positions, places on a texture, and normals, and `f`, `l`, and `p` faces,
|
|
#! lines, and points of them, by their numbers, counted from 1, or back from
|
|
#! the last where they're negative. `o`, `g`, `s`, and `usemtl` say which
|
|
#! object, group, smoothing group, and material what follows is of, and
|
|
#! `mtllib` names the MTL files the materials are in. A model keeps them as
|
|
#! they are: faces of any number of corners, each corner its position, and its
|
|
#! place on a texture and its normal where it has them, every index counted
|
|
#! from 0.
|
|
|
|
import std::io;
|
|
import malunal::linear;
|
|
module malunal::obj;
|
|
|
|
#! What kind of thing kept a model from being read or written.
|
|
pub enum failure: u8 {
|
|
unreadable, #! A file couldn't be read, or written.
|
|
malformed, #! The text breaks OBJ or MTL: a number that isn't one, an index past what it names, a face of fewer than three corners.
|
|
}
|
|
|
|
#! Why a model couldn't be read, or written: the kind of thing, and what, as
|
|
#! `line 12: its face has 2 corners, of the 3 it needs at least`. It holds its
|
|
#! text itself, its first 240 bytes, so it's plain data, as std's errors are.
|
|
pub struct obj_error {
|
|
pub var kind: failure;
|
|
var text: u8[240];
|
|
var length: u64;
|
|
}
|
|
|
|
#! What was wrong, and where.
|
|
pub def obj_error::describe(&this): string {
|
|
return string::from(strview::over(&this.text[0] as *u8, this.length), nothing{});
|
|
}
|
|
|
|
# An error of a kind, saying why.
|
|
def failed(kind: failure, why: strview): obj_error {
|
|
var made := obj_error{ kind = kind };
|
|
made.length = why.length < 240 ? why.length : 240;
|
|
for i in 0..made.length {
|
|
made.text[i] = why[i];
|
|
}
|
|
return made;
|
|
}
|
|
|
|
#! What a corner names where it names nothing: no place on a texture, or no
|
|
#! normal; and what an element is of where it's of no material, object, or
|
|
#! group.
|
|
pub const absent: u32 = 0xFFFFFFFF;
|
|
|
|
#! A corner of a face, or a point of a line or of points: indices into the
|
|
#! model's positions, places on a texture, and normals.
|
|
pub struct corner {
|
|
pub var position: u32;
|
|
pub var uv: u32 = absent;
|
|
pub var normal: u32 = absent;
|
|
}
|
|
|
|
#! A face, a line, or points: a run of the model's corners, and what it's of.
|
|
pub struct element {
|
|
#! Its first corner, in the model's `corners`.
|
|
pub var first: u64;
|
|
#! How many corners it has: three or more of a face, two or more of a line.
|
|
pub var count: u32;
|
|
#! Its material, in the model's `materials`.
|
|
pub var material: u32 = absent;
|
|
#! Its smoothing group, or 0 where it's of none, and drawn flat.
|
|
pub var smoothing: u32;
|
|
#! The object it's of, in the model's `objects`.
|
|
pub var object: u32 = absent;
|
|
#! The group it's of, in the model's `groups`.
|
|
pub var group: u32 = absent;
|
|
}
|
|
|
|
#! A model read from OBJ: what it's made of, and its faces, lines, and points,
|
|
#! each a run of corners.
|
|
pub struct model {
|
|
pub var positions: vector<vec3>;
|
|
#! Each position's color, where the file gives them, as `v x y z r g b`,
|
|
#! white where it gives one none; empty where it gives none.
|
|
pub var colors: vector<vec3>;
|
|
pub var uvs: vector<vec2>;
|
|
pub var normals: vector<vec3>;
|
|
pub var corners: vector<corner>;
|
|
pub var faces: vector<element>;
|
|
pub var lines: vector<element>;
|
|
pub var points: vector<element>;
|
|
#! The names of its objects, by `o`, and of its groups, by `g`.
|
|
pub var objects: vector<string>;
|
|
pub var groups: vector<string>;
|
|
#! Its materials: those of the MTL files it names, in their order, then one
|
|
#! of each name `usemtl` gives that none of them has, of the defaults.
|
|
pub var materials: vector<material>;
|
|
#! The MTL files it names, as it names them, and those of them that weren't
|
|
#! found or couldn't be read.
|
|
pub var libraries: vector<string>;
|
|
pub var missing: vector<string>;
|
|
}
|
|
|
|
#! The corners of an element.
|
|
pub def model::corners_of(&this, of: &element): span<corner> {
|
|
return this.corners.span[of.first:of.count as u64];
|
|
}
|
|
|
|
#! The material named so, where it has one.
|
|
pub def model::material_named(&this, name: strview): maybe<u64> {
|
|
for i in 0..this.materials.length {
|
|
return i if this.materials[i].name.span == name;
|
|
}
|
|
return nothing{};
|
|
}
|
|
|
|
#! Reads a model from a file, and the materials of the MTL files it names, from
|
|
#! beside it.
|
|
#! @param at The file's path.
|
|
#! @returns The model, or why it couldn't be read: `unreadable` where the file
|
|
#! couldn't be. An MTL file that can't be is named in `missing`.
|
|
pub def load(at: &path): result<model, obj_error> {
|
|
val text := io::read_file(at) else (_) { return failed(failure::unreadable, $"{at.span} couldn't be read.".span); };
|
|
var folder := path::new();
|
|
if at.parent() is some(&above) {
|
|
folder = path::copy_of(&above);
|
|
}
|
|
return decode(text.span, folder.span);
|
|
}
|
|
|
|
#! Reads a model from a file named by text, as `load` does.
|
|
pub def load(at: strview): result<model, obj_error> {
|
|
val named := path::parse(at) else (_) { return failed(failure::unreadable, $"{at} isn't a path.".span); };
|
|
return load(&named);
|
|
}
|
|
|
|
#! Reads a model from text.
|
|
#! @param text The OBJ file's text.
|
|
#! @param folder The folder the MTL files it names are found in, or empty,
|
|
#! where none are read, but named in `missing`.
|
|
#! @returns The model, or why it couldn't be read.
|
|
pub def decode(text: strview, folder: strview = ""): result<model, obj_error> {
|
|
return read_model(text, folder);
|
|
}
|
|
|
|
#! Reads the materials of an MTL file.
|
|
#! @param at The file's path.
|
|
#! @returns Its materials, in its order, or why they couldn't be read.
|
|
pub def load_materials(at: &path): result<vector<material>, obj_error> {
|
|
val text := io::read_file(at) else (_) { return failed(failure::unreadable, $"{at.span} couldn't be read.".span); };
|
|
return decode_materials(text.span);
|
|
}
|
|
|
|
#! Reads the materials of an MTL file named by text, as `load_materials` does.
|
|
pub def load_materials(at: strview): result<vector<material>, obj_error> {
|
|
val named := path::parse(at) else (_) { return failed(failure::unreadable, $"{at} isn't a path.".span); };
|
|
return load_materials(&named);
|
|
}
|