mirror of
https://codeberg.org/icewind/vmdl.git
synced 2026-06-03 08:34:23 +02:00
better vertex indices
This commit is contained in:
parent
88bcdb35e7
commit
3649ffd9b2
7 changed files with 75 additions and 56 deletions
|
|
@ -10,6 +10,7 @@ binrw = "0.8.0"
|
|||
thiserror = "1.0.30"
|
||||
static_assertions = "1.1.0"
|
||||
bitflags = "1.0.4"
|
||||
itertools = "0.10.3"
|
||||
|
||||
[dev-dependencies]
|
||||
three-d = "0.10.2"
|
||||
|
|
|
|||
|
|
@ -1,6 +1,5 @@
|
|||
use std::env::args_os;
|
||||
use std::fs;
|
||||
use std::fs::read;
|
||||
use std::path::{Path, PathBuf};
|
||||
use thiserror::Error;
|
||||
use three_d::*;
|
||||
|
|
@ -17,8 +16,6 @@ enum Error {
|
|||
Mdl(#[from] vmdl::ModelError),
|
||||
#[error(transparent)]
|
||||
IO(#[from] std::io::Error),
|
||||
#[error("{0}")]
|
||||
Other(&'static str),
|
||||
}
|
||||
|
||||
fn main() -> Result<(), Error> {
|
||||
|
|
@ -32,7 +29,7 @@ fn main() -> Result<(), Error> {
|
|||
let window = Window::new(WindowSettings {
|
||||
title: path.display().to_string(),
|
||||
min_size: (512, 512),
|
||||
max_size: Some((1280, 720)),
|
||||
max_size: Some((1920, 1080)),
|
||||
..Default::default()
|
||||
})
|
||||
.unwrap();
|
||||
|
|
@ -53,7 +50,7 @@ fn main() -> Result<(), Error> {
|
|||
let mut control = OrbitControl::new(*camera.target(), 1.0, 100.0);
|
||||
let mut gui = three_d::GUI::new(&context).unwrap();
|
||||
|
||||
let mut cpu_mesh = model_to_mesh(&model);
|
||||
let cpu_mesh = model_to_mesh(&model);
|
||||
let material = PhysicalMaterial {
|
||||
albedo: Color {
|
||||
r: 128,
|
||||
|
|
@ -228,25 +225,26 @@ const UNIT_SCALE: f32 = 1.0 / (1.905 * 100.0);
|
|||
|
||||
fn model_to_mesh(model: &Model) -> CPUMesh {
|
||||
let positions: Vec<f32> = model
|
||||
.vertex_strips()
|
||||
.flat_map(|mut strip| {
|
||||
let mut a = strip.next().unwrap();
|
||||
let mut b = strip.next().unwrap();
|
||||
strip
|
||||
.flat_map(move |c| {
|
||||
let tri = [a, b, c];
|
||||
a = b;
|
||||
b = c;
|
||||
tri
|
||||
})
|
||||
.flat_map(|vec| vec.iter())
|
||||
})
|
||||
.vertices()
|
||||
.iter()
|
||||
.flat_map(|vertex| vertex.position.iter().map(|pos| pos * UNIT_SCALE * 10.0))
|
||||
.collect();
|
||||
let normals: Vec<f32> = model
|
||||
.vertices()
|
||||
.iter()
|
||||
.flat_map(|vertex| vertex.normal.iter())
|
||||
.collect();
|
||||
let indices = Indices::U32(
|
||||
model
|
||||
.vertex_strip_indices()
|
||||
.flat_map(|strip| strip.map(|index| index as u32))
|
||||
.collect(),
|
||||
);
|
||||
|
||||
let mut mesh = CPUMesh {
|
||||
CPUMesh {
|
||||
positions,
|
||||
normals: Some(normals),
|
||||
indices: Some(indices),
|
||||
..Default::default()
|
||||
};
|
||||
mesh.compute_normals();
|
||||
mesh
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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,
|
||||
}
|
||||
|
||||
|
|
|
|||
29
src/lib.rs
29
src/lib.rs
|
|
@ -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)
|
||||
})
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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]))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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,
|
||||
|
|
|
|||
|
|
@ -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,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue