♻️ refactor: parameterize the IQM writing

main
_ 2022-02-20 23:31:29 +00:00
parent 0b57f1a53e
commit d250cf12fa
1 changed files with 102 additions and 51 deletions

View File

@ -4,6 +4,71 @@ use std::{
}; };
fn main () { fn main () {
let vertexes = [
[-1.0, -1.0, -1.0],
[ 1.0, -1.0, -1.0],
[ 1.0, 1.0, -1.0],
[-1.0, 1.0, -1.0],
[-1.0, -1.0, 1.0],
[ 1.0, -1.0, 1.0],
[ 1.0, 1.0, 1.0],
[-1.0, 1.0, 1.0],
];
let triangles = [
[0, 2, 1],
[0, 3, 2],
[0, 5, 1],
[1, 6, 2],
[2, 7, 3],
[3, 4, 0],
[0, 4, 5],
[1, 5, 6],
[2, 6, 7],
[3, 7, 4],
[4, 6, 5],
[4, 7, 6],
];
let num_vertexes = u32::try_from (vertexes.len ()).unwrap ();
let num_triangles = u32::try_from (triangles.len ()).unwrap ();
let mut texts = Vec::default ();
let mesh = iqm::Mesh {
name: u32::try_from (texts.len ()).unwrap (),
material: 0,
first_vertex: 0,
num_vertexes,
first_triangle: 0,
num_triangles,
};
texts.push ("Cube");
let meshes = vec! [
mesh,
];
let num_text: usize = texts.iter ()
.map (|s| s.len () + 1)
.sum ();
let num_text = u32::try_from (num_text).unwrap ();
let num_meshes = u32::try_from (meshes.len ()).unwrap ();
let num_vertexarrays = 1u32;
let header_len = 124u32;
let ofs_meshes = header_len;
let ofs_vertexarrays = ofs_meshes + num_meshes * 6 * 4;
let ofs_triangles = ofs_vertexarrays + num_vertexarrays * 5 * 4 + num_vertexes * 3 * 4;
let ofs_text = ofs_triangles + num_triangles * 3 * 4;
let filesize = ofs_text + num_text;
let mut f = File::create ("cube.iqm").unwrap (); let mut f = File::create ("cube.iqm").unwrap ();
f.write_all (b"INTERQUAKEMODEL\0").unwrap (); f.write_all (b"INTERQUAKEMODEL\0").unwrap ();
@ -11,27 +76,27 @@ fn main () {
f.write_all (&(2u32.to_le_bytes ())).unwrap (); f.write_all (&(2u32.to_le_bytes ())).unwrap ();
// filesize // filesize
f.write_all (&(0x198u32.to_le_bytes ())).unwrap (); f.write_all (&(filesize.to_le_bytes ())).unwrap ();
// flags // flags
f.write_all (&(0u32.to_le_bytes ())).unwrap (); f.write_all (&(0u32.to_le_bytes ())).unwrap ();
// num_text, ofs_text // num_text, ofs_text
f.write_all (&(0u32.to_le_bytes ())).unwrap (); f.write_all (&(num_text.to_le_bytes ())).unwrap ();
f.write_all (&(0u32.to_le_bytes ())).unwrap (); f.write_all (&(ofs_text.to_le_bytes ())).unwrap ();
// num_meshes, ofs_meshes // num_meshes, ofs_meshes
f.write_all (&(1u32.to_le_bytes ())).unwrap (); f.write_all (&(num_meshes.to_le_bytes ())).unwrap ();
f.write_all (&(0x7Cu32.to_le_bytes ())).unwrap (); f.write_all (&(ofs_meshes.to_le_bytes ())).unwrap ();
// num_vertexarrays, num_vertexes, ofs_vertexarrays // num_vertexarrays, num_vertexes, ofs_vertexarrays
f.write_all (&(1u32.to_le_bytes ())).unwrap (); f.write_all (&(num_vertexarrays.to_le_bytes ())).unwrap ();
f.write_all (&(8u32.to_le_bytes ())).unwrap (); f.write_all (&(num_vertexes.to_le_bytes ())).unwrap ();
f.write_all (&(0x94u32.to_le_bytes ())).unwrap (); f.write_all (&(ofs_vertexarrays.to_le_bytes ())).unwrap ();
// num_triangles, ofs_triangles, ofs_adjacency // num_triangles, ofs_triangles, ofs_adjacency
f.write_all (&(1u32.to_le_bytes ())).unwrap (); f.write_all (&(num_triangles.to_le_bytes ())).unwrap ();
f.write_all (&(0x108u32.to_le_bytes ())).unwrap (); f.write_all (&(ofs_triangles.to_le_bytes ())).unwrap ();
f.write_all (&(0u32.to_le_bytes ())).unwrap (); f.write_all (&(0u32.to_le_bytes ())).unwrap ();
// num_joints, ofs_joints // num_joints, ofs_joints
@ -62,19 +127,14 @@ fn main () {
// Meshes // Meshes
// name for mesh in meshes {
f.write_all (&(0u32.to_le_bytes ())).unwrap (); f.write_all (&(mesh.name.to_le_bytes ())).unwrap ();
f.write_all (&(mesh.material.to_le_bytes ())).unwrap ();
// material f.write_all (&(mesh.first_vertex.to_le_bytes ())).unwrap ();
f.write_all (&(0u32.to_le_bytes ())).unwrap (); f.write_all (&(mesh.num_vertexes.to_le_bytes ())).unwrap ();
f.write_all (&(mesh.first_triangle.to_le_bytes ())).unwrap ();
// first_vertex, num_vertexes f.write_all (&(mesh.num_triangles.to_le_bytes ())).unwrap ();
f.write_all (&(0u32.to_le_bytes ())).unwrap (); }
f.write_all (&(8u32.to_le_bytes ())).unwrap ();
// first_triangle, num_triangles
f.write_all (&(0u32.to_le_bytes ())).unwrap ();
f.write_all (&(12u32.to_le_bytes ())).unwrap ();
// Vertex arrays // Vertex arrays
@ -95,17 +155,7 @@ fn main () {
// Vertexes // Vertexes
for t in [ for t in vertexes {
[-1.0, -1.0, -1.0],
[ 1.0, -1.0, -1.0],
[ 1.0, 1.0, -1.0],
[-1.0, 1.0, -1.0],
[-1.0, -1.0, 1.0],
[ 1.0, -1.0, 1.0],
[ 1.0, 1.0, 1.0],
[-1.0, 1.0, 1.0],
] {
for x in t { for x in t {
f.write_all (&(f32::to_le_bytes (x))).unwrap (); f.write_all (&(f32::to_le_bytes (x))).unwrap ();
} }
@ -113,30 +163,31 @@ fn main () {
// Triangles // Triangles
for t in [ for t in triangles {
[0, 2, 1],
[0, 3, 2],
[0, 5, 1],
[1, 6, 2],
[2, 7, 3],
[3, 4, 0],
[0, 4, 5],
[1, 5, 6],
[2, 6, 7],
[3, 7, 4],
[4, 6, 5],
[4, 7, 6],
] {
for x in t { for x in t {
f.write_all (&(u32::to_le_bytes (x))).unwrap (); f.write_all (&(u32::to_le_bytes (x))).unwrap ();
} }
} }
// Text
for s in texts {
f.write_all (s.as_bytes ()).unwrap ();
f.write_all (b"\0").unwrap ();
}
} }
/// IQM structures as written to disk
mod iqm { mod iqm {
pub struct Mesh {
pub name: u32,
pub material: u32,
pub first_vertex: u32,
pub num_vertexes: u32,
pub first_triangle: u32,
pub num_triangles: u32,
}
struct VertexArray { struct VertexArray {
r#type: u32, r#type: u32,
flags: u32, flags: u32,