bone transform wip

This commit is contained in:
Robin Appelman 2023-12-27 22:54:10 +01:00
commit b731b7aab1
6 changed files with 67 additions and 23 deletions

View file

@ -17,14 +17,11 @@ fn main() -> Result<(), vmdl::ModelError> {
let data = fs::read(path.with_extension("vvd"))?;
let _vvd = Vvd::read(&data)?;
let models = mdl
.body_parts
.iter()
.flat_map(|part| part.models.iter())
.flat_map(|model| model.meshes.iter())
.map(|mesh| mesh.material)
.collect::<Vec<_>>();
dbg!(mdl.textures, models, mdl.skin_table);
for bone in mdl.bones {
dbg!(bone.flags);
dbg!(bone.rot);
dbg!(bone.quaternion);
}
// let model = Model::from_parts(mdl, vtx, vvd);
// for strip in model.vertex_strips() {

View file

@ -45,7 +45,7 @@ fn main() -> Result<(), Error> {
let mut camera = Camera::new_perspective(
window.viewport(),
vec3(2.0, 2.0, 5.0),
vec3(2.0, 2.0, 2.0),
vec3(0.0, 0.0, 0.0),
vec3(0.0, 1.0, 0.0),
degrees(90.0),
@ -57,7 +57,6 @@ fn main() -> Result<(), Error> {
let mut gui = three_d::GUI::new(&context);
let loader = Loader::new().expect("loader");
dbg!(&loader);
let skin_count = source_model.skin_tables().count();
let cpu_models = (0..skin_count).map(|skin| model_to_model(&source_model, &loader, skin));
@ -232,21 +231,30 @@ fn main() -> Result<(), Error> {
// 1 hammer unit is ~1.905cm
const UNIT_SCALE: f32 = 1.0 / (1.905 * 100.0);
pub fn map_coords<C: Into<Vec3>>(vec: C) -> Vec3 {
let vec = vec.into();
Vec3 {
x: vec.y * UNIT_SCALE,
y: vec.z * UNIT_SCALE,
z: vec.x * UNIT_SCALE,
}
}
fn model_to_model(model: &Model, loader: &Loader, skin: usize) -> CpuModel {
let offset = model
.vertices()
.iter()
.map(|vert| vert.position.y)
.max_by(|a, b| a.total_cmp(b))
.unwrap();
let bounds = model.bounding_box();
let offset = Vector {
x: 0.0,
y: -offset / 2.0,
z: 0.0,
x: -(bounds.1.x + bounds.0.x) / 2.0,
y: -(bounds.1.y + bounds.0.y) / 2.0,
z: -(bounds.1.z + bounds.0.z) / 2.0,
};
let skin = model.skin_tables().nth(skin).unwrap();
let transforms = model
.bones()
.map(|bone| Mat4::from(cgmath::Quaternion::from(bone.rot)))
.fold(Mat4::identity(), |a, b| a * b);
let geometries = model
.meshes()
.map(|mesh| {
@ -256,7 +264,8 @@ fn model_to_model(model: &Model, loader: &Loader, skin: usize) -> CpuModel {
let positions: Vec<Vec3> = mesh
.vertices()
.map(|vertex| ((vertex.position + offset) * UNIT_SCALE * 10.0).into())
.map(|vertex| map_coords(vertex.position + offset) * 10.0)
.map(|vertex: Vec3| (transforms * vertex.extend(1.0)).truncate())
.collect();
let normals: Vec<Vec3> = mesh.vertices().map(|vertex| vertex.normal.into()).collect();
let uvs: Vec<Vec2> = mesh

View file

@ -6,7 +6,7 @@ pub mod vtx;
pub mod vvd;
pub use crate::mdl::Mdl;
use crate::mdl::TextureInfo;
use crate::mdl::{Bone, TextureInfo};
pub use crate::vtx::Vtx;
use crate::vvd::Vertex;
pub use crate::vvd::Vvd;
@ -137,6 +137,10 @@ impl Model {
pub fn name(&self) -> &str {
self.mdl.name.as_str()
}
pub fn bones(&self) -> impl Iterator<Item = &Bone> {
self.mdl.bones.iter()
}
}
pub struct SkinTable<'a> {

View file

@ -2,9 +2,9 @@ mod raw;
pub use raw::header::*;
pub use raw::header2::*;
pub use raw::*;
use std::mem::size_of;
use crate::mdl::raw::{BodyPartHeader, Bone, MeshHeader, MeshTexture, ModelHeader};
use crate::vvd::Vertex;
use crate::{
read_indexes, read_relative, read_relative_iter, FixedString, ModelError, ReadRelative,

View file

@ -1,7 +1,8 @@
use crate::{ModelError, StringError};
use arrayvec::ArrayString;
use bytemuck::{Pod, Zeroable};
use cgmath::{Deg, Euler, Rad, Vector3};
use cgmath::{Deg, Euler, Rad, Rotation3, Vector3};
use std::f32::consts::PI;
use std::fmt;
use std::fmt::{Display, Formatter};
use std::ops::{Add, Mul};
@ -101,6 +102,23 @@ impl From<Quaternion> for cgmath::Quaternion<f32> {
}
}
impl From<cgmath::Quaternion<f32>> for Quaternion {
fn from(q: cgmath::Quaternion<f32>) -> Self {
Quaternion {
x: q.v.x,
y: q.v.y,
z: q.v.z,
w: q.s,
}
}
}
impl From<Quaternion> for cgmath::Matrix4<f32> {
fn from(q: Quaternion) -> Self {
cgmath::Quaternion::from(q).into()
}
}
#[derive(Debug, Clone, Copy, Zeroable, Pod)]
#[repr(C)]
pub struct RadianEuler {
@ -129,6 +147,21 @@ impl From<RadianEuler> for Euler<Deg<f32>> {
}
}
impl From<RadianEuler> for cgmath::Quaternion<f32> {
fn from(value: RadianEuler) -> Self {
// angles are applied in roll, pitch, yaw order
cgmath::Quaternion::from_angle_y(Rad(value.x))
* cgmath::Quaternion::from_angle_x(Rad(value.y))
* cgmath::Quaternion::from_angle_z(Rad(-value.z))
}
}
impl From<RadianEuler> for Quaternion {
fn from(value: RadianEuler) -> Self {
cgmath::Quaternion::from(value).into()
}
}
/// Fixed length, null-terminated string
#[derive(Debug, Clone, Default, Copy)]
pub struct FixedString<const LEN: usize>(ArrayString<LEN>);

View file

@ -215,6 +215,7 @@ impl StripHeader {
#[repr(C)]
#[repr(packed)]
pub struct Vertex {
// these index into the mesh's vert[origMeshVertID]'s bones
pub bone_weight_indexes: [u8; 3],
pub bone_count: u8,
pub original_mesh_vertex_id: u16,