viewer wip

This commit is contained in:
Robin Appelman 2022-03-11 20:29:11 +01:00
commit 88bcdb35e7
8 changed files with 329 additions and 5 deletions

View file

@ -5,12 +5,44 @@ mod shared;
pub mod vtx;
pub mod vvd;
use crate::mdl::Mdl;
use crate::vtx::Vtx;
use crate::vvd::Vvd;
use binrw::{BinRead, BinReaderExt};
pub use error::*;
pub use handle::Handle;
pub use shared::*;
use std::any::type_name;
use std::io::Cursor;
pub struct Model {
mdl: Mdl,
vtx: Vtx,
vvd: Vvd,
}
impl Model {
pub fn from_parts(mdl: Mdl, vtx: Vtx, vvd: Vvd) -> Self {
Model { mdl, vtx, vvd }
}
pub fn vertex_strips(&self) -> impl Iterator<Item = impl Iterator<Item = Vector> + '_> {
self.vtx
.body_parts
.iter()
.flat_map(|part| part.models.iter())
.flat_map(|model| model.lods.iter().next())
.flat_map(|lod| lod.meshes.iter())
.flat_map(|mesh| mesh.strip_groups.iter())
.map(|strip_group| {
strip_group
.indices
.iter()
.map(|index| self.vvd.vertices[(*index) as usize].position)
})
}
}
fn read_indexes<'a, I: Iterator<Item = usize> + 'static, T: BinRead>(
indexes: I,
data: &'a [u8],
@ -21,7 +53,7 @@ where
indexes
.map(|index| {
data.get(index..).ok_or_else(|| ModelError::OutOfBounds {
data: "Bone",
data: type_name::<T>(),
offset: index,
})
})

View file

@ -11,6 +11,12 @@ pub struct Vector {
pub z: f32,
}
impl Vector {
pub fn iter(&self) -> impl Iterator<Item = f32> {
[self.x, self.y, self.z].into_iter()
}
}
impl From<Vector> for [f32; 3] {
fn from(vector: Vector) -> Self {
[vector.x, vector.y, vector.z]

View file

@ -142,6 +142,7 @@ impl Mesh {
pub struct StripGroup {
// todo vertex indexes
// todo topologies
pub indices: Vec<u16>,
pub vertices: Vec<Vertex>,
pub strips: Vec<Strip>,
pub flags: StripGroupFlags,
@ -173,6 +174,17 @@ impl StripGroup {
Strip::read(data, header)
})
.collect::<Result<_>>()?,
indices: header
.index_indexes()
.map(|index| {
let data = data.get(index..).ok_or_else(|| ModelError::OutOfBounds {
data: "VertexIndex",
offset: index,
})?;
let mut reader = Cursor::new(data);
Ok(reader.read_le()?)
})
.collect::<Result<_>>()?,
flags: header.flags,
})
}

View file

@ -123,7 +123,7 @@ impl StripGroupHeader {
}
pub fn index_indexes(&self) -> impl Iterator<Item = usize> {
index_range(self.index_offset, self.index_count, size_of::<Vertex>())
index_range(self.index_offset, self.index_count, size_of::<u16>())
}
pub fn strip_indexes(&self) -> impl Iterator<Item = usize> {