You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
minimum-viable-game-engine/src/geometry.rs

85 lines
2.3 KiB

use bytemuck::{Pod, Zeroable};
use nalgebra::Vector4;
#[repr(C)]
#[derive(Clone, Copy, Debug)]
4 years ago
pub struct Vertex {
pos: [f32; 4],
normal: [f32; 4],
uv: [f32; 2],
}
impl Vertex {
pub fn position(&self) -> Vector4<f32> {
Vector4::new(self.pos[0], self.pos[1], self.pos[2], self.pos[3])
}
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],
}
}
}
unsafe impl Pod for Vertex {}
unsafe impl Zeroable for Vertex {}
#[derive(Clone, Debug)]
pub struct RawMesh {
vertices: Vec<Vertex>,
normals: Vec<[u32; 3]>,
}
pub fn load_obj(obj_path: &str, mtl_path: Option<&str>) -> (Vec<Vertex>, Vec<u32>) {
let (models, materials) = tobj::load_obj(obj_path, true).expect("Failed to load file");
println!("# of models: {}", models.len());
println!("# of materials: {}", materials.len());
let mut index_data: Vec<u32> = Vec::new();
let mut vertex_data = Vec::new();
for model in models {
let mesh = &model.mesh;
let mut next_face = 0;
for f in 0..mesh.num_face_indices.len() {
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);
}
next_face = end;
}
assert!(mesh.positions.len() % 3 == 0);
assert!(mesh.texcoords.len() % 2 == 0);
for v in 0..mesh.positions.len() / 3 {
let texcoords = if mesh.texcoords.len() == 0 {
[0.0, 0.0]
} else {
[mesh.texcoords[2 * v], mesh.texcoords[2 * v + 1]]
};
vertex_data.push(Vertex::from(
[
mesh.positions[3 * v],
mesh.positions[3 * v + 1],
mesh.positions[3 * v + 2],
],
[
mesh.normals[3 * v],
mesh.normals[3 * v + 1],
mesh.normals[3 * v + 2],
],
texcoords,
));
}
}
(vertex_data.to_vec(), index_data.to_vec())
}