better vertex indices

This commit is contained in:
Robin Appelman 2022-03-11 22:53:57 +01:00
commit 3649ffd9b2
7 changed files with 75 additions and 56 deletions

View file

@ -7,7 +7,7 @@ use std::ops::Deref;
/// reference parts from other structures in the mdl file
#[derive(Debug)]
pub struct Handle<'a, T> {
mdl: &'a Mdl,
_mdl: &'a Mdl,
data: &'a T,
}

View file

@ -7,7 +7,7 @@ pub mod vvd;
use crate::mdl::Mdl;
use crate::vtx::Vtx;
use crate::vvd::Vvd;
use crate::vvd::{Vertex, Vvd};
use binrw::{BinRead, BinReaderExt};
pub use error::*;
pub use handle::Handle;
@ -16,6 +16,7 @@ use std::any::type_name;
use std::io::Cursor;
pub struct Model {
#[allow(dead_code)]
mdl: Mdl,
vtx: Vtx,
vvd: Vvd,
@ -26,7 +27,16 @@ impl Model {
Model { mdl, vtx, vvd }
}
pub fn vertex_strips(&self) -> impl Iterator<Item = impl Iterator<Item = Vector> + '_> {
pub fn vertex_strips(&self) -> impl Iterator<Item = impl Iterator<Item = &'_ Vertex> + '_> {
self.vertex_strip_indices()
.map(|strip| strip.map(|index| &self.vvd.vertices[index]))
}
pub fn vertices(&self) -> &[Vertex] {
&self.vvd.vertices
}
pub fn vertex_strip_indices(&self) -> impl Iterator<Item = impl Iterator<Item = usize> + '_> {
self.vtx
.body_parts
.iter()
@ -34,11 +44,16 @@ impl Model {
.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)
.flat_map(|strip_group| {
let group_indices = &strip_group.indices;
let vertices = &strip_group.vertices;
strip_group.strips.iter().cloned().map(move |strip| {
strip
.indices()
.flat_map(|i| i)
.map(move |index| group_indices[index] as usize)
.map(move |index| vertices[index].original_mesh_vertex_id as usize)
})
})
}
}

View file

@ -2,9 +2,11 @@ mod raw;
use crate::ModelError;
use binrw::BinReaderExt;
use itertools::Either;
use raw::*;
pub use raw::{MeshFlags, StripFlags, StripGroupFlags, Vertex};
use std::io::Cursor;
use std::ops::Range;
pub const MDL_VERSION: i32 = 7;
@ -140,7 +142,6 @@ impl Mesh {
#[derive(Debug, Clone)]
pub struct StripGroup {
// todo vertex indexes
// todo topologies
pub indices: Vec<u16>,
pub vertices: Vec<Vertex>,
@ -192,27 +193,35 @@ impl StripGroup {
#[derive(Debug, Clone)]
pub struct Strip {
// todo vertex indexes
// todo bone state changes
pub vertices: Vec<Vertex>,
vertices: Range<usize>,
pub flags: StripFlags,
indices: Range<usize>,
}
impl Strip {
fn read(data: &[u8], header: StripHeader) -> Result<Self> {
fn read(_data: &[u8], header: StripHeader) -> Result<Self> {
Ok(Strip {
vertices: header
.vertex_indexes()
.map(|index| {
let data = data.get(index..).ok_or_else(|| ModelError::OutOfBounds {
data: "Vertex",
offset: index,
})?;
let mut reader = Cursor::new(data);
reader.read_le().map_err(ModelError::from)
})
.collect::<Result<_>>()?,
vertices: header.vertex_indexes(),
indices: header.index_indexes(),
flags: header.flags,
})
}
pub fn vertices(&self) -> impl Iterator<Item = usize> + 'static {
self.vertices.clone()
}
pub fn indices(&self) -> impl Iterator<Item = [usize; 3]> + 'static {
if self.flags.contains(StripFlags::IS_TRI_STRIP) {
let offset = self.indices.start;
Either::Left((0..self.indices.len()).map(move |i| {
let cw = i & 1;
let idx = offset + i;
[idx, idx + 1 - cw, idx + 2 - cw]
}))
} else {
Either::Right(self.indices.clone().step_by(3).map(|i| [i, i + 1, i + 2]))
}
}
}

View file

@ -2,6 +2,7 @@ use crate::index_range;
use binrw::BinRead;
use bitflags::bitflags;
use std::mem::size_of;
use std::ops::Range;
#[derive(Debug, Clone, BinRead)]
pub struct VtxHeader {
@ -169,18 +170,15 @@ bitflags! {
impl StripHeader {
/// Index into the VVD file vertexes
pub fn vertex_indexes(&self) -> impl Iterator<Item = usize> {
index_range(
self.vertex_offset,
self.vertex_count,
size_of::<u16>(), // Vertex index from .VVD's vertex array
)
pub fn vertex_indexes(&self) -> Range<usize> {
self.vertex_offset as usize..(self.vertex_offset + self.vertex_count) as usize
}
pub fn index_indexes(&self) -> impl Iterator<Item = usize> {
index_range(self.index_offset, self.index_count, size_of::<Vertex>())
pub fn index_indexes(&self) -> Range<usize> {
self.index_offset as usize..(self.index_offset + self.index_count) as usize
}
#[allow(dead_code)]
pub fn bone_state_change_indexes(&self) -> impl Iterator<Item = usize> {
index_range(
self.bone_state_change_offset,

View file

@ -2,8 +2,6 @@ use crate::{index_range, Vector};
use binrw::BinRead;
use std::mem::size_of;
pub const MDL_VERSION: i32 = 7;
#[derive(Debug, Clone, BinRead)]
pub struct VvdHeader {
pub id: i32,