♻️ Remove now-redundant Iqm* and iqm_* prefixes from names
parent
c3473b9507
commit
ffcb2c114d
68
src/iqm.rs
68
src/iqm.rs
|
@ -6,7 +6,7 @@ use nom::{
|
||||||
|
|
||||||
use std::convert::TryInto;
|
use std::convert::TryInto;
|
||||||
|
|
||||||
mod iqm_consts {
|
mod consts {
|
||||||
pub const VERSION: usize = 0;
|
pub const VERSION: usize = 0;
|
||||||
pub const FILESIZE: usize = 1;
|
pub const FILESIZE: usize = 1;
|
||||||
pub const FLAGS: usize = 2;
|
pub const FLAGS: usize = 2;
|
||||||
|
@ -36,7 +36,7 @@ mod iqm_consts {
|
||||||
pub const OFS_EXTENSIONS: usize = 26;
|
pub const OFS_EXTENSIONS: usize = 26;
|
||||||
}
|
}
|
||||||
|
|
||||||
mod iqm_types {
|
mod types {
|
||||||
pub const POSITION: u32 = 0;
|
pub const POSITION: u32 = 0;
|
||||||
pub const TEXCOORD: u32 = 1;
|
pub const TEXCOORD: u32 = 1;
|
||||||
pub const NORMAL: u32 = 2;
|
pub const NORMAL: u32 = 2;
|
||||||
|
@ -47,7 +47,7 @@ mod iqm_types {
|
||||||
pub const CUSTOM: u32 = 0x10;
|
pub const CUSTOM: u32 = 0x10;
|
||||||
}
|
}
|
||||||
|
|
||||||
mod iqm_formats {
|
mod formats {
|
||||||
pub const BYTE: u32 = 0;
|
pub const BYTE: u32 = 0;
|
||||||
pub const UBYTE: u32 = 1;
|
pub const UBYTE: u32 = 1;
|
||||||
pub const SHORT: u32 = 2;
|
pub const SHORT: u32 = 2;
|
||||||
|
@ -60,7 +60,7 @@ mod iqm_formats {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive (Debug)]
|
#[derive (Debug)]
|
||||||
pub struct IqmMesh {
|
pub struct Mesh {
|
||||||
pub name: u32,
|
pub name: u32,
|
||||||
pub material: u32,
|
pub material: u32,
|
||||||
pub first_vertex: u32,
|
pub first_vertex: u32,
|
||||||
|
@ -70,12 +70,12 @@ pub struct IqmMesh {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive (Debug)]
|
#[derive (Debug)]
|
||||||
pub struct IqmHeader {
|
pub struct Header {
|
||||||
fields: [u32; 27],
|
fields: [u32; 27],
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive (Debug)]
|
#[derive (Debug)]
|
||||||
pub struct IqmVertexArray {
|
pub struct VertexArray {
|
||||||
va_type: u32,
|
va_type: u32,
|
||||||
va_flags: u32,
|
va_flags: u32,
|
||||||
va_format: u32,
|
va_format: u32,
|
||||||
|
@ -84,17 +84,17 @@ pub struct IqmVertexArray {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive (Debug)]
|
#[derive (Debug)]
|
||||||
pub struct IqmModel <'a> {
|
pub struct Model <'a> {
|
||||||
data: &'a [u8],
|
data: &'a [u8],
|
||||||
|
|
||||||
header: IqmHeader,
|
header: Header,
|
||||||
text: Vec <u8>,
|
text: Vec <u8>,
|
||||||
pub meshes: Vec <IqmMesh>,
|
pub meshes: Vec <Mesh>,
|
||||||
vertexarrays: Vec <IqmVertexArray>,
|
vertexarrays: Vec <VertexArray>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl IqmHeader {
|
impl Header {
|
||||||
pub fn from_slice (input: &[u8]) -> IResult <&[u8], IqmHeader> {
|
pub fn from_slice (input: &[u8]) -> IResult <&[u8], Header> {
|
||||||
let (input, _) = tag (b"INTERQUAKEMODEL\0")(input)?;
|
let (input, _) = tag (b"INTERQUAKEMODEL\0")(input)?;
|
||||||
let (input, version) = le_u32 (input)?;
|
let (input, version) = le_u32 (input)?;
|
||||||
|
|
||||||
|
@ -111,15 +111,15 @@ impl IqmHeader {
|
||||||
fields [usize::from (index)] = h;
|
fields [usize::from (index)] = h;
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok ((input, IqmHeader {
|
Ok ((input, Header {
|
||||||
fields,
|
fields,
|
||||||
}))
|
}))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl IqmMesh {
|
impl Mesh {
|
||||||
pub fn from_slice (input: &[u8]) -> IResult <&[u8], IqmMesh> {
|
pub fn from_slice (input: &[u8]) -> IResult <&[u8], Mesh> {
|
||||||
let mut result = IqmMesh {
|
let mut result = Mesh {
|
||||||
name: 0,
|
name: 0,
|
||||||
material: 0,
|
material: 0,
|
||||||
first_vertex: 0,
|
first_vertex: 0,
|
||||||
|
@ -146,9 +146,9 @@ impl IqmMesh {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl IqmVertexArray {
|
impl VertexArray {
|
||||||
pub fn from_slice (input: &[u8]) -> IResult <&[u8], IqmVertexArray> {
|
pub fn from_slice (input: &[u8]) -> IResult <&[u8], VertexArray> {
|
||||||
let mut result = IqmVertexArray {
|
let mut result = VertexArray {
|
||||||
va_type: 0,
|
va_type: 0,
|
||||||
va_flags: 0,
|
va_flags: 0,
|
||||||
va_format: 0,
|
va_format: 0,
|
||||||
|
@ -173,44 +173,42 @@ impl IqmVertexArray {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl <'a> IqmModel <'a> {
|
impl <'a> Model <'a> {
|
||||||
pub fn from_slice (data: &'a [u8]) -> IqmModel <'a> {
|
pub fn from_slice (data: &'a [u8]) -> Model <'a> {
|
||||||
let header = IqmHeader::from_slice (data).unwrap ().1;
|
let header = Header::from_slice (data).unwrap ().1;
|
||||||
|
|
||||||
let text = {
|
let text = {
|
||||||
let offset: usize = header.fields [iqm_consts::OFS_TEXT].try_into ().unwrap ();
|
let offset: usize = header.fields [consts::OFS_TEXT].try_into ().unwrap ();
|
||||||
let num: usize = header.fields [iqm_consts::NUM_TEXT].try_into ().unwrap ();
|
let num: usize = header.fields [consts::NUM_TEXT].try_into ().unwrap ();
|
||||||
Vec::from (&data [offset..offset + num])
|
Vec::from (&data [offset..offset + num])
|
||||||
};
|
};
|
||||||
|
|
||||||
let meshes = {
|
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 mut meshes = Vec::with_capacity (num);
|
||||||
let mesh_size = 6 * 4;
|
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 {
|
for i in 0..num {
|
||||||
let offset = meshes_offset + i * mesh_size;
|
let offset = meshes_offset + i * mesh_size;
|
||||||
let mesh_slice = &data [offset..offset + 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
|
meshes
|
||||||
};
|
};
|
||||||
|
|
||||||
//println! ("{:?}", meshes);
|
|
||||||
|
|
||||||
let vertexarrays = {
|
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 mut vertexarrays = Vec::with_capacity (num);
|
||||||
let vertexarray_size = 5 * 4;
|
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 {
|
for i in 0..num {
|
||||||
let offset = vertexarrays_offset + i * vertexarray_size;
|
let offset = vertexarrays_offset + i * vertexarray_size;
|
||||||
let vertexarray_slice = &data [offset..offset + 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);
|
//println! ("{:?}", vertexarray);
|
||||||
|
|
||||||
|
@ -220,7 +218,7 @@ impl <'a> IqmModel <'a> {
|
||||||
vertexarrays
|
vertexarrays
|
||||||
};
|
};
|
||||||
|
|
||||||
IqmModel {
|
Model {
|
||||||
data,
|
data,
|
||||||
|
|
||||||
header,
|
header,
|
||||||
|
@ -240,7 +238,7 @@ impl <'a> IqmModel <'a> {
|
||||||
//assert_eq! (stride, 12);
|
//assert_eq! (stride, 12);
|
||||||
|
|
||||||
let offset: usize = (vertexarray.va_offset).try_into ().unwrap ();
|
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]
|
&self.data [offset..offset + num_bytes]
|
||||||
}
|
}
|
||||||
|
@ -251,7 +249,7 @@ impl <'a> IqmModel <'a> {
|
||||||
let indexes_per_tri = 3;
|
let indexes_per_tri = 3;
|
||||||
let stride = bytes_per_u32 * indexes_per_tri;
|
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 ();
|
let num_bytes: usize = (stride * mesh.num_triangles).try_into ().unwrap ();
|
||||||
|
|
||||||
|
|
14
src/main.rs
14
src/main.rs
|
@ -12,7 +12,7 @@ use std::time::{Duration, Instant};
|
||||||
|
|
||||||
mod iqm;
|
mod iqm;
|
||||||
|
|
||||||
use iqm::IqmModel;
|
use iqm::Model;
|
||||||
|
|
||||||
pub fn load_small_file <P> (name: P) -> Vec <u8>
|
pub fn load_small_file <P> (name: P) -> Vec <u8>
|
||||||
where P: AsRef <Path>
|
where P: AsRef <Path>
|
||||||
|
@ -264,7 +264,7 @@ unsafe fn vertex_attrib_pointer (id: Option <u32>, num_coords: i32, slice: &[u8]
|
||||||
|
|
||||||
unsafe fn point_to_model (
|
unsafe fn point_to_model (
|
||||||
attrs: &HashMap <String, Option <u32>>,
|
attrs: &HashMap <String, Option <u32>>,
|
||||||
model: &IqmModel
|
model: &Model
|
||||||
) {
|
) {
|
||||||
vertex_attrib_pointer (attrs ["pos"], 3, model.get_vertex_slice (0));
|
vertex_attrib_pointer (attrs ["pos"], 3, model.get_vertex_slice (0));
|
||||||
vertex_attrib_pointer (attrs ["uv"], 2, model.get_vertex_slice (1));
|
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 texture = ugly_load_texture ("sky.png");
|
||||||
|
|
||||||
let model_data = load_small_file ("pumpking.iqm");
|
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_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;
|
const FALSE_U8: u8 = 0;
|
||||||
|
|
||||||
|
@ -608,15 +608,15 @@ mod tests {
|
||||||
let data = load_small_file ("pumpking.iqm");
|
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 [1], 90368);
|
||||||
assert_eq! (model.fields [2], 0);
|
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.meshes);
|
||||||
println! ("{:?}", model.vertexarrays);
|
println! ("{:?}", model.vertexarrays);
|
||||||
|
|
Loading…
Reference in New Issue