♻️ refactor: reduce hard-coding of vertexarray stuff

main
_ 2022-02-22 23:44:57 +00:00
parent 938240e90d
commit 3fe2d17bce
1 changed files with 62 additions and 19 deletions

View File

@ -40,7 +40,7 @@ fn main () {
let mut vertexes = Vec::default (); let mut vertexes = Vec::default ();
let mut triangles = Vec::default (); let mut triangles = Vec::default ();
let mut meshes = Vec::default (); let mut meshes = Vec::default ();
let mut texts = String::default (); let mut texts = String::from ("\0");
for node in document.nodes () { for node in document.nodes () {
let mesh = match node.mesh () { let mesh = match node.mesh () {
@ -151,6 +151,21 @@ fn main () {
} }
} }
let vertexarrays_unplaced = [
iqm::VertexArrayUnplaced {
r#type: iqm::POSITION,
flags: 0,
format: iqm::FLOAT,
size: 3,
},
iqm::VertexArrayUnplaced {
r#type: iqm::NORMAL,
flags: 0,
format: iqm::FLOAT,
size: 3,
},
];
let vertexes = vertexes; let vertexes = vertexes;
let triangles = triangles; let triangles = triangles;
let meshes = meshes; let meshes = meshes;
@ -163,14 +178,34 @@ fn main () {
let num_text = u32::try_from (num_text).unwrap (); let num_text = u32::try_from (num_text).unwrap ();
let num_meshes = u32::try_from (meshes.len ()).unwrap (); let num_meshes = u32::try_from (meshes.len ()).unwrap ();
let num_vertexarrays = 2u32; let num_vertexarrays = u32::try_from (vertexarrays_unplaced.len ()).unwrap ();
let header_len = 124u32; let header_len = 124u32;
let ofs_meshes = header_len; let ofs_meshes = header_len;
let ofs_vertexarrays = ofs_meshes + num_meshes * 6 * 4; let ofs_vertexarrays = ofs_meshes + num_meshes * 6 * 4;
// Not part of IQM spec, but I need it for self-care // Not part of IQM spec, but I need it for self-care
let ofs_vertexes = ofs_vertexarrays + num_vertexarrays * 5 * 4; let ofs_vertexes = ofs_vertexarrays + num_vertexarrays * 5 * 4;
let ofs_triangles = ofs_vertexes + num_vertexes * 3 * 4 + num_vertexes * 3 * 4;
let mut vertexarrays = Vec::default ();
let mut ofs_va = ofs_vertexes;
for va_in in vertexarrays_unplaced {
let stride = va_in.stride ();
vertexarrays.push (iqm::VertexArray {
r#type: va_in.r#type,
flags: va_in.flags,
format: va_in.format,
size: va_in.size,
offset: ofs_va,
});
ofs_va += num_vertexes * stride;
}
let vertexarrays = vertexarrays;
let ofs_triangles = ofs_va;
let ofs_text = ofs_triangles + num_triangles * 3 * 4; let ofs_text = ofs_triangles + num_triangles * 3 * 4;
let filesize = ofs_text + num_text; let filesize = ofs_text + num_text;
@ -244,22 +279,7 @@ fn main () {
// Vertex arrays // Vertex arrays
for va in [ for va in &vertexarrays {
iqm::VertexArray {
r#type: 0,
flags: 0,
format: 7,
size: 3,
offset: ofs_vertexes + 0,
},
iqm::VertexArray {
r#type: 2,
flags: 0,
format: 7,
size: 3,
offset: ofs_vertexes + num_vertexes * 3 * 4,
},
] {
f.write_all (&(va.r#type.to_le_bytes ())).unwrap (); f.write_all (&(va.r#type.to_le_bytes ())).unwrap ();
f.write_all (&(va.flags.to_le_bytes ())).unwrap (); f.write_all (&(va.flags.to_le_bytes ())).unwrap ();
f.write_all (&(va.format.to_le_bytes ())).unwrap (); f.write_all (&(va.format.to_le_bytes ())).unwrap ();
@ -309,6 +329,29 @@ mod iqm {
pub num_triangles: u32, pub num_triangles: u32,
} }
pub const POSITION: u32 = 0;
pub const NORMAL: u32 = 2;
pub const BLENDINDEXES: u32 = 4;
pub const BLENDWEIGHTS: u32 = 5;
pub const FLOAT: u32 = 7;
pub struct VertexArrayUnplaced {
pub r#type: u32,
pub flags: u32,
pub format: u32,
pub size: u32,
}
impl VertexArrayUnplaced {
pub fn stride (&self) -> u32 {
self.size * match self.format {
FLOAT => 4,
_ => panic! ("Can't handle this vertexarray format yet"),
}
}
}
pub struct VertexArray { pub struct VertexArray {
pub r#type: u32, pub r#type: u32,
pub flags: u32, pub flags: u32,