diff --git a/src/iqm.rs b/src/iqm.rs index 6bf812b..6658acb 100644 --- a/src/iqm.rs +++ b/src/iqm.rs @@ -6,7 +6,7 @@ use nom::{ use std::convert::TryInto; -mod iqm_consts { +mod consts { pub const VERSION: usize = 0; pub const FILESIZE: usize = 1; pub const FLAGS: usize = 2; @@ -36,7 +36,7 @@ mod iqm_consts { pub const OFS_EXTENSIONS: usize = 26; } -mod iqm_types { +mod types { pub const POSITION: u32 = 0; pub const TEXCOORD: u32 = 1; pub const NORMAL: u32 = 2; @@ -47,7 +47,7 @@ mod iqm_types { pub const CUSTOM: u32 = 0x10; } -mod iqm_formats { +mod formats { pub const BYTE: u32 = 0; pub const UBYTE: u32 = 1; pub const SHORT: u32 = 2; @@ -60,7 +60,7 @@ mod iqm_formats { } #[derive (Debug)] -pub struct IqmMesh { +pub struct Mesh { pub name: u32, pub material: u32, pub first_vertex: u32, @@ -70,12 +70,12 @@ pub struct IqmMesh { } #[derive (Debug)] -pub struct IqmHeader { +pub struct Header { fields: [u32; 27], } #[derive (Debug)] -pub struct IqmVertexArray { +pub struct VertexArray { va_type: u32, va_flags: u32, va_format: u32, @@ -84,17 +84,17 @@ pub struct IqmVertexArray { } #[derive (Debug)] -pub struct IqmModel <'a> { +pub struct Model <'a> { data: &'a [u8], - header: IqmHeader, + header: Header, text: Vec , - pub meshes: Vec , - vertexarrays: Vec , + pub meshes: Vec , + vertexarrays: Vec , } -impl IqmHeader { - pub fn from_slice (input: &[u8]) -> IResult <&[u8], IqmHeader> { +impl Header { + pub fn from_slice (input: &[u8]) -> IResult <&[u8], Header> { let (input, _) = tag (b"INTERQUAKEMODEL\0")(input)?; let (input, version) = le_u32 (input)?; @@ -111,15 +111,15 @@ impl IqmHeader { fields [usize::from (index)] = h; } - Ok ((input, IqmHeader { + Ok ((input, Header { fields, })) } } -impl IqmMesh { - pub fn from_slice (input: &[u8]) -> IResult <&[u8], IqmMesh> { - let mut result = IqmMesh { +impl Mesh { + pub fn from_slice (input: &[u8]) -> IResult <&[u8], Mesh> { + let mut result = Mesh { name: 0, material: 0, first_vertex: 0, @@ -146,9 +146,9 @@ impl IqmMesh { } } -impl IqmVertexArray { - pub fn from_slice (input: &[u8]) -> IResult <&[u8], IqmVertexArray> { - let mut result = IqmVertexArray { +impl VertexArray { + pub fn from_slice (input: &[u8]) -> IResult <&[u8], VertexArray> { + let mut result = VertexArray { va_type: 0, va_flags: 0, va_format: 0, @@ -173,44 +173,42 @@ impl IqmVertexArray { } } -impl <'a> IqmModel <'a> { - pub fn from_slice (data: &'a [u8]) -> IqmModel <'a> { - let header = IqmHeader::from_slice (data).unwrap ().1; +impl <'a> Model <'a> { + pub fn from_slice (data: &'a [u8]) -> Model <'a> { + let header = Header::from_slice (data).unwrap ().1; let text = { - let offset: usize = header.fields [iqm_consts::OFS_TEXT].try_into ().unwrap (); - let num: usize = header.fields [iqm_consts::NUM_TEXT].try_into ().unwrap (); + let offset: usize = header.fields [consts::OFS_TEXT].try_into ().unwrap (); + let num: usize = header.fields [consts::NUM_TEXT].try_into ().unwrap (); Vec::from (&data [offset..offset + num]) }; let meshes = { - let num: usize = header.fields [iqm_consts::NUM_MESHES].try_into ().unwrap (); + let num: usize = header.fields [consts::NUM_MESHES].try_into ().unwrap (); let mut meshes = Vec::with_capacity (num); let mesh_size = 6 * 4; - let meshes_offset: usize = header.fields [iqm_consts::OFS_MESHES].try_into ().unwrap (); + let meshes_offset: usize = header.fields [consts::OFS_MESHES].try_into ().unwrap (); for i in 0..num { let offset = meshes_offset + i * mesh_size; let mesh_slice = &data [offset..offset + mesh_size]; - meshes.push (IqmMesh::from_slice (mesh_slice).unwrap ().1); + meshes.push (Mesh::from_slice (mesh_slice).unwrap ().1); } meshes }; - //println! ("{:?}", meshes); - let vertexarrays = { - let num: usize = header.fields [iqm_consts::NUM_VERTEXARRAYS].try_into ().unwrap (); + let num: usize = header.fields [consts::NUM_VERTEXARRAYS].try_into ().unwrap (); let mut vertexarrays = Vec::with_capacity (num); let vertexarray_size = 5 * 4; - let vertexarrays_offset: usize = header.fields [iqm_consts::OFS_VERTEXARRAYS].try_into ().unwrap (); + let vertexarrays_offset: usize = header.fields [consts::OFS_VERTEXARRAYS].try_into ().unwrap (); for i in 0..num { let offset = vertexarrays_offset + i * vertexarray_size; let vertexarray_slice = &data [offset..offset + vertexarray_size]; - let vertexarray = IqmVertexArray::from_slice (vertexarray_slice).unwrap ().1; + let vertexarray = VertexArray::from_slice (vertexarray_slice).unwrap ().1; //println! ("{:?}", vertexarray); @@ -220,7 +218,7 @@ impl <'a> IqmModel <'a> { vertexarrays }; - IqmModel { + Model { data, header, @@ -240,7 +238,7 @@ impl <'a> IqmModel <'a> { //assert_eq! (stride, 12); let offset: usize = (vertexarray.va_offset).try_into ().unwrap (); - let num_bytes: usize = (stride * self.header.fields [iqm_consts::NUM_VERTEXES]).try_into ().unwrap (); + let num_bytes: usize = (stride * self.header.fields [consts::NUM_VERTEXES]).try_into ().unwrap (); &self.data [offset..offset + num_bytes] } @@ -251,7 +249,7 @@ impl <'a> IqmModel <'a> { let indexes_per_tri = 3; let stride = bytes_per_u32 * indexes_per_tri; - let offset: usize = (self.header.fields [iqm_consts::OFS_TRIANGLES] + stride * mesh.first_triangle).try_into ().unwrap (); + let offset: usize = (self.header.fields [consts::OFS_TRIANGLES] + stride * mesh.first_triangle).try_into ().unwrap (); let num_bytes: usize = (stride * mesh.num_triangles).try_into ().unwrap (); diff --git a/src/main.rs b/src/main.rs index 8af3d95..50d0fe0 100644 --- a/src/main.rs +++ b/src/main.rs @@ -12,7 +12,7 @@ use std::time::{Duration, Instant}; mod iqm; -use iqm::IqmModel; +use iqm::Model; pub fn load_small_file

(name: P) -> Vec where P: AsRef @@ -264,7 +264,7 @@ unsafe fn vertex_attrib_pointer (id: Option , num_coords: i32, slice: &[u8] unsafe fn point_to_model ( attrs: &HashMap >, - model: &IqmModel + model: &Model ) { vertex_attrib_pointer (attrs ["pos"], 3, model.get_vertex_slice (0)); vertex_attrib_pointer (attrs ["uv"], 2, model.get_vertex_slice (1)); @@ -477,10 +477,10 @@ fn main () { let texture = ugly_load_texture ("sky.png"); let model_data = load_small_file ("pumpking.iqm"); - let model = IqmModel::from_slice (&model_data [..]); + let model = Model::from_slice (&model_data [..]); let sky_data = load_small_file ("sky-sphere.iqm"); - let sky_model = IqmModel::from_slice (&sky_data [..]); + let sky_model = Model::from_slice (&sky_data [..]); const FALSE_U8: u8 = 0; @@ -608,15 +608,15 @@ mod tests { let data = load_small_file ("pumpking.iqm"); { - let model = IqmHeader::from_slice (&data [..]).unwrap ().1; + let model = eader::from_slice (&data [..]).unwrap ().1; assert_eq! (model.fields [1], 90368); assert_eq! (model.fields [2], 0); - assert_eq! (model.fields [iqm_consts::VERSION], 2); + assert_eq! (model.fields [consts::VERSION], 2); } { - let model = IqmModel::from_slice (&data [..]); + let model = Model::from_slice (&data [..]); println! ("{:?}", model.meshes); println! ("{:?}", model.vertexarrays);