Initial Commit
This commit is contained in:
36 files changed
+8436
No files matched your search
@@ -0,0 +1,240 @@
|
||||
#! Contains buffers, the views of them, and accessors, which say how a view's
|
||||
#! bytes are read as numbers; and reading an accessor's numbers, as floats or
|
||||
#! as integers, its sparse values put in place.
|
||||
|
||||
import std::math;
|
||||
module malunal::gltf;
|
||||
|
||||
#! A buffer: bytes, read from its file, a `data:` URI, or a `.glb` file's
|
||||
#! binary chunk.
|
||||
pub struct buffer {
|
||||
pub var name: string;
|
||||
#! Its URI, as the JSON writes it, or empty for a `.glb` file's binary chunk.
|
||||
pub var uri: string;
|
||||
#! How many bytes it is, as the JSON says, which its bytes are at least.
|
||||
pub var length: u64;
|
||||
pub var bytes: vector<u8>;
|
||||
}
|
||||
|
||||
#! A part of a buffer, which accessors read.
|
||||
pub struct buffer_view {
|
||||
pub var name: string;
|
||||
pub var buffer: u64;
|
||||
pub var offset: u64;
|
||||
pub var length: u64;
|
||||
#! The bytes from one element to the next, or zero where they're packed.
|
||||
pub var stride: u64;
|
||||
}
|
||||
|
||||
#! The type of each number of an accessor.
|
||||
pub enum component: u8 { byte, unsigned_byte, short, unsigned_short, unsigned_int, float }
|
||||
|
||||
#! How many bytes a number of the type takes.
|
||||
pub val component::bytes: u64 {
|
||||
get => match this {
|
||||
byte, unsigned_byte => 1,
|
||||
short, unsigned_short => 2,
|
||||
unsigned_int, float => 4,
|
||||
};
|
||||
}
|
||||
|
||||
#! What each element of an accessor is.
|
||||
pub enum element: u8 { scalar, vec2, vec3, vec4, mat2, mat3, mat4 }
|
||||
|
||||
#! How many numbers an element holds.
|
||||
pub val element::components: u64 {
|
||||
get => match this {
|
||||
scalar => 1,
|
||||
vec2 => 2,
|
||||
vec3 => 3,
|
||||
vec4, mat2 => 4,
|
||||
mat3 => 9,
|
||||
mat4 => 16,
|
||||
};
|
||||
}
|
||||
|
||||
# How many rows a matrix has, or 1 for a vector, as a column of a matrix is
|
||||
# kept on its own, at a multiple of 4 bytes.
|
||||
def element::rows(&this): u64 {
|
||||
return match this {
|
||||
mat2 => 2,
|
||||
mat3 => 3,
|
||||
mat4 => 4,
|
||||
else => 0,
|
||||
};
|
||||
}
|
||||
|
||||
#! Values an accessor puts in place of some of its elements, of `count`
|
||||
#! indices, in a view of integers, and as many values, in a view of the
|
||||
#! accessor's own type.
|
||||
pub struct sparse {
|
||||
pub var count: u64;
|
||||
pub var indices_view: u64;
|
||||
pub var indices_offset: u64;
|
||||
pub var indices_type := component::unsigned_int;
|
||||
pub var values_view: u64;
|
||||
pub var values_offset: u64;
|
||||
}
|
||||
|
||||
#! How the bytes of a view are read as `count` elements, each of `kind`'s
|
||||
#! numbers of `type`, from `offset`.
|
||||
pub struct accessor {
|
||||
pub var name: string;
|
||||
#! Its view, or none where every element is zero but its sparse ones.
|
||||
pub var view: maybe<u64>;
|
||||
pub var offset: u64;
|
||||
pub var component_type := component::float;
|
||||
#! Whether its integers are read as fractions, 0 to 1, or -1 to 1 where signed.
|
||||
pub var normalized: bool;
|
||||
pub var count: u64;
|
||||
pub var kind := element::scalar;
|
||||
#! The least and the most of each of its numbers, where the JSON says.
|
||||
pub var min: vector<f64>;
|
||||
pub var max: vector<f64>;
|
||||
pub var sparse: maybe<sparse>;
|
||||
}
|
||||
|
||||
# How many bytes an element of an accessor takes, each column of a matrix
|
||||
# begun at a multiple of 4.
|
||||
def accessor::element_bytes(&this): u64 {
|
||||
val rows := this.kind.rows();
|
||||
return this.kind.components * this.component_type.bytes if rows == 0;
|
||||
return rows * column_stride(rows, this.component_type);
|
||||
}
|
||||
|
||||
# The bytes from one column of a matrix to the next.
|
||||
def column_stride(rows: u64, numbers: component): u64 {
|
||||
val bytes := rows * numbers.bytes;
|
||||
return (bytes + 3) / 4 * 4;
|
||||
}
|
||||
|
||||
# Where the number `k` of an element is, from the element's first byte.
|
||||
def accessor::component_at(&this, k: u64): u64 {
|
||||
val rows := this.kind.rows();
|
||||
return k * this.component_type.bytes if rows == 0;
|
||||
return (k / rows) * column_stride(rows, this.component_type) + (k % rows) * this.component_type.bytes;
|
||||
}
|
||||
|
||||
#! The numbers of an accessor, as floats: each element's in turn, `count` of
|
||||
#! them, integers made floats, normalized ones made fractions as glTF says, and
|
||||
#! its sparse values put in place.
|
||||
#! @param this The asset.
|
||||
#! @param which The accessor.
|
||||
#! @returns `count * kind.components` floats.
|
||||
pub def asset::floats(&this, which: u64): vector<f32> {
|
||||
val read := &this.accessors[which];
|
||||
val width := read.kind.components;
|
||||
var made := vector::<f32>::new();
|
||||
made.resize(read.count * width);
|
||||
if read.view is some(v) {
|
||||
val at := this.place_of(read, v);
|
||||
val stride := this.stride_of(read, v);
|
||||
for e in 0..read.count {
|
||||
for k in 0..width {
|
||||
made[e * width + k] = float_at(at.bytes, at.start + e * stride + read.component_at(k), read.component_type, read.normalized);
|
||||
}
|
||||
}
|
||||
}
|
||||
if read.sparse is some(&s) {
|
||||
val indices := this.place_of_view(s.indices_view, s.indices_offset);
|
||||
val values := this.place_of_view(s.values_view, s.values_offset);
|
||||
val size := read.element_bytes();
|
||||
for i in 0..s.count {
|
||||
val e := integer_at(indices.bytes, indices.start + i * s.indices_type.bytes, s.indices_type);
|
||||
for k in 0..width {
|
||||
made[e * width + k] = float_at(values.bytes, values.start + i * size + read.component_at(k), read.component_type, read.normalized);
|
||||
}
|
||||
}
|
||||
}
|
||||
return made;
|
||||
}
|
||||
|
||||
#! The numbers of an accessor of integers, as indices and joints are: each
|
||||
#! element's in turn, its sparse values put in place.
|
||||
#! @param this The asset.
|
||||
#! @param which The accessor, of a type of integers, not normalized.
|
||||
#! @returns `count * kind.components` integers.
|
||||
pub def asset::integers(&this, which: u64): vector<u32> {
|
||||
val read := &this.accessors[which];
|
||||
val width := read.kind.components;
|
||||
var made := vector::<u32>::new();
|
||||
made.resize(read.count * width);
|
||||
if read.view is some(v) {
|
||||
val at := this.place_of(read, v);
|
||||
val stride := this.stride_of(read, v);
|
||||
for e in 0..read.count {
|
||||
for k in 0..width {
|
||||
made[e * width + k] = integer_at(at.bytes, at.start + e * stride + read.component_at(k), read.component_type) as u32;
|
||||
}
|
||||
}
|
||||
}
|
||||
if read.sparse is some(&s) {
|
||||
val indices := this.place_of_view(s.indices_view, s.indices_offset);
|
||||
val values := this.place_of_view(s.values_view, s.values_offset);
|
||||
val size := read.element_bytes();
|
||||
for i in 0..s.count {
|
||||
val e := integer_at(indices.bytes, indices.start + i * s.indices_type.bytes, s.indices_type);
|
||||
for k in 0..width {
|
||||
made[e * width + k] = integer_at(values.bytes, values.start + i * size + read.component_at(k), read.component_type) as u32;
|
||||
}
|
||||
}
|
||||
}
|
||||
return made;
|
||||
}
|
||||
|
||||
# Where an accessor's, or a view's, bytes begin, in its buffer's.
|
||||
struct place {
|
||||
var bytes: span<u8>;
|
||||
var start: u64;
|
||||
}
|
||||
|
||||
def asset::place_of(&this, read: &accessor, view: u64): place {
|
||||
return this.place_of_view(view, read.offset);
|
||||
}
|
||||
|
||||
def asset::place_of_view(&this, view: u64, offset: u64): place {
|
||||
val v := &this.views[view];
|
||||
return place{ bytes = this.buffers[v.buffer].bytes.span, start = v.offset + offset };
|
||||
}
|
||||
|
||||
# The bytes from one element of an accessor to the next.
|
||||
def asset::stride_of(&this, read: &accessor, view: u64): u64 {
|
||||
val given := this.views[view].stride;
|
||||
return given != 0 ? given : read.element_bytes();
|
||||
}
|
||||
|
||||
# A number of a type, at a byte, as a float, made a fraction where it's
|
||||
# normalized.
|
||||
def float_at(bytes: span<u8>, at: u64, numbers: component, normalized: bool): f32 {
|
||||
if numbers == component::float {
|
||||
return math::real_of_bits(integer_at(bytes, at, component::unsigned_int) as u32);
|
||||
}
|
||||
val raw := integer_at(bytes, at, numbers);
|
||||
val signed := signed_value(raw, numbers);
|
||||
return signed as f32 if !normalized;
|
||||
return match numbers {
|
||||
byte => math::max(signed as f32 / 127, -1f),
|
||||
unsigned_byte => raw as f32 / 255,
|
||||
short => math::max(signed as f32 / 32767, -1f),
|
||||
unsigned_short => raw as f32 / 65535,
|
||||
else => raw as f32 / 4294967295,
|
||||
};
|
||||
}
|
||||
|
||||
# The bytes of an integer of a type, at a byte, little-endian, unsigned.
|
||||
def integer_at(bytes: span<u8>, at: u64, numbers: component): u64 {
|
||||
var value: u64 = 0;
|
||||
for i in 0..numbers.bytes {
|
||||
value = value | (bytes[at + i] as u64 << (8 * i) as u32);
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
# An integer's bytes as the signed value they are, of a signed type.
|
||||
def signed_value(raw: u64, numbers: component): i64 {
|
||||
return match numbers {
|
||||
byte => raw >= 0x80 ? raw as i64 - 0x100 : raw as i64,
|
||||
short => raw >= 0x8000 ? raw as i64 - 0x10000 : raw as i64,
|
||||
else => raw as i64,
|
||||
};
|
||||
}
|
||||
Reference in new issue
Block a user