mirror of
https://codeberg.org/icewind/vmdl.git
synced 2026-06-03 16:44:11 +02:00
texture wip
This commit is contained in:
parent
cd5ec8492e
commit
7948e6ba5b
8 changed files with 1180 additions and 67 deletions
65
src/lib.rs
65
src/lib.rs
|
|
@ -6,6 +6,7 @@ pub mod vtx;
|
|||
pub mod vvd;
|
||||
|
||||
pub use crate::mdl::Mdl;
|
||||
use crate::mdl::TextureInfo;
|
||||
pub use crate::vtx::Vtx;
|
||||
use crate::vvd::Vertex;
|
||||
pub use crate::vvd::Vvd;
|
||||
|
|
@ -37,6 +38,44 @@ impl Model {
|
|||
&self.vvd.vertices
|
||||
}
|
||||
|
||||
pub fn texture_directories(&self) -> &[String] {
|
||||
&self.mdl.texture_paths
|
||||
}
|
||||
|
||||
pub fn textures(&self) -> &[TextureInfo] {
|
||||
&self.mdl.textures
|
||||
}
|
||||
|
||||
pub fn meshes(&self) -> impl Iterator<Item = Mesh> {
|
||||
let mdl_meshes = self
|
||||
.mdl
|
||||
.body_parts
|
||||
.iter()
|
||||
.flat_map(|part| part.models.iter())
|
||||
.flat_map(|model| {
|
||||
model
|
||||
.meshes
|
||||
.iter()
|
||||
.map(|mesh| (mesh, model.vertex_offset as usize))
|
||||
});
|
||||
|
||||
let vtx_meshes = self
|
||||
.vtx
|
||||
.body_parts
|
||||
.iter()
|
||||
.flat_map(|part| part.models.iter())
|
||||
.flat_map(|model| model.lods.first())
|
||||
.flat_map(|lod| lod.meshes.iter());
|
||||
|
||||
mdl_meshes
|
||||
.zip(vtx_meshes)
|
||||
.map(|((mdl, model_vertex_offset), vtx)| Mesh {
|
||||
model_vertex_offset,
|
||||
mdl,
|
||||
vtx,
|
||||
})
|
||||
}
|
||||
|
||||
pub fn vertex_strip_indices(&self) -> impl Iterator<Item = impl Iterator<Item = usize> + '_> {
|
||||
let mesh_vertex_offsets = self
|
||||
.mdl
|
||||
|
|
@ -81,6 +120,32 @@ impl Model {
|
|||
}
|
||||
}
|
||||
|
||||
pub struct Mesh<'a> {
|
||||
model_vertex_offset: usize,
|
||||
mdl: &'a mdl::Mesh,
|
||||
vtx: &'a vtx::Mesh,
|
||||
}
|
||||
|
||||
impl<'a> Mesh<'a> {
|
||||
pub fn vertex_strip_indices(&self) -> impl Iterator<Item = impl Iterator<Item = usize> + '_> {
|
||||
let mdl_offset = self.mdl.vertex_offset as usize + self.model_vertex_offset;
|
||||
self.vtx.strip_groups.iter().flat_map(move |strip_group| {
|
||||
let group_indices = &strip_group.indices;
|
||||
let vertices = &strip_group.vertices;
|
||||
strip_group.strips.iter().map(move |strip| {
|
||||
strip
|
||||
.indices()
|
||||
.map(move |index| group_indices[index] as usize)
|
||||
.map(move |index| vertices[index].original_mesh_vertex_id as usize + mdl_offset)
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
pub fn material_index(&self) -> i32 {
|
||||
self.mdl.material
|
||||
}
|
||||
}
|
||||
|
||||
fn read_indexes<I: Iterator<Item = usize> + 'static, T: Readable>(
|
||||
indexes: I,
|
||||
data: &[u8],
|
||||
|
|
|
|||
|
|
@ -29,10 +29,12 @@ impl Mdl {
|
|||
.collect::<Result<Vec<TextureInfo>>>()?;
|
||||
let texture_dirs_indexes =
|
||||
read_relative_iter(data, header.texture_dir_indexes()).collect::<Result<Vec<u32>>>()?;
|
||||
let texture_paths = read_relative::<String, _>(
|
||||
let texture_paths = read_relative_iter::<String, _>(
|
||||
data,
|
||||
texture_dirs_indexes.into_iter().map(|index| index as usize),
|
||||
)?;
|
||||
)
|
||||
.map(|path| path.map(|path| path.replace('\\', "/")))
|
||||
.collect::<Result<Vec<_>>>()?;
|
||||
|
||||
let bones = read_indexes(header.bone_indexes(), data).collect::<Result<_>>()?;
|
||||
Ok(Mdl {
|
||||
|
|
@ -98,6 +100,7 @@ impl ReadRelative for Model {
|
|||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct Mesh {
|
||||
pub material: i32,
|
||||
pub vertex_offset: i32,
|
||||
}
|
||||
|
||||
|
|
@ -106,6 +109,7 @@ impl ReadRelative for Mesh {
|
|||
|
||||
fn read(_data: &[u8], header: Self::Header) -> Result<Self> {
|
||||
Ok(Mesh {
|
||||
material: header.material,
|
||||
vertex_offset: header.vertex_index,
|
||||
})
|
||||
}
|
||||
|
|
@ -121,7 +125,7 @@ impl ReadRelative for TextureInfo {
|
|||
|
||||
fn read(data: &[u8], header: Self::Header) -> Result<Self> {
|
||||
Ok(TextureInfo {
|
||||
name: String::read(&data[header.name_index as usize..], ())?,
|
||||
name: String::read(&data[header.name_index as usize..], ())?.replace('\\', "/"),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -124,7 +124,7 @@ pub struct ModelVertexData {
|
|||
#[repr(C)]
|
||||
#[allow(dead_code)]
|
||||
pub struct MeshHeader {
|
||||
material: i32,
|
||||
pub material: i32,
|
||||
model_index: i32,
|
||||
vertex_count: i32,
|
||||
pub vertex_index: i32,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue