better to use a struct for the raw mesh data

This commit is contained in:
2021-02-19 21:18:47 -08:00
parent e5815ce0d6
commit 85376e5b17
3 changed files with 52 additions and 40 deletions

View File

@@ -1,5 +1,6 @@
use bytemuck::{Pod, Zeroable};
use nalgebra::Vector4;
use rapier3d::parry::math::Point;
#[repr(C)]
#[derive(Clone, Copy, Debug)]
@@ -10,14 +11,14 @@ pub struct Vertex {
}
impl Vertex {
pub fn position(&self) -> Vector4<f32> {
Vector4::new(self.pos[0], self.pos[1], self.pos[2], self.pos[3])
pub fn position(&self) -> Point<f32> {
Point::<f32>::new(self.pos[0], self.pos[1], self.pos[2])
}
pub fn from(pos: [f32; 3], nor: [f32; 3], uv: [f32; 2]) -> Vertex {
Vertex {
pos: [pos[0], pos[1], pos[2], 1.0],
normal: [nor[0], nor[1], nor[2], 0.0],
uv: [0.0, 0.0],
uv: [uv[0], uv[1]],
}
}
}
@@ -27,31 +28,35 @@ unsafe impl Zeroable for Vertex {}
#[derive(Clone, Debug)]
pub struct RawMesh {
vertices: Vec<Vertex>,
normals: Vec<[u32; 3]>,
pub vertices: Vec<Vertex>,
pub indices: Vec<[u32; 3]>,
}
pub fn load_obj(obj_path: &str, mtl_path: Option<&str>) -> (Vec<Vertex>, Vec<u32>) {
pub fn load_obj(obj_path: &str) -> RawMesh {
let (models, materials) = tobj::load_obj(obj_path, true).expect("Failed to load file");
println!("# of models: {}", models.len());
println!("# of materials: {}", materials.len());
println!("{:?}", materials);
let mut index_data: Vec<u32> = Vec::new();
let mut index_data: Vec<[u32; 3]> = Vec::new();
let mut vertex_data = Vec::new();
for model in models {
let mesh = &model.mesh;
// Cycle through the faces and chunk out the indices
let mut next_face = 0;
for f in 0..mesh.num_face_indices.len() {
// calculate the next chunk
let end = next_face + mesh.num_face_indices[f] as usize;
let face_indices: Vec<_> = mesh.indices[next_face..end].iter().collect();
for i in face_indices {
index_data.push(*i);
}
assert!(face_indices.len() == 3, "we only handle triangulated faces");
index_data.push([*face_indices[0], *face_indices[1], *face_indices[2]]);
next_face = end;
}
@@ -80,5 +85,8 @@ pub fn load_obj(obj_path: &str, mtl_path: Option<&str>) -> (Vec<Vertex>, Vec<u32
));
}
}
(vertex_data.to_vec(), index_data.to_vec())
RawMesh {
vertices: vertex_data.to_vec(),
indices: index_data.to_vec(),
}
}