wip(rendering): obj model loading
This commit is contained in:
parent
bbfdbfd50e
commit
1aa3efb54e
3 changed files with 129 additions and 17 deletions
43
src/model.rs
43
src/model.rs
|
@ -1,4 +1,8 @@
|
|||
use crate::Vertex;
|
||||
use crate::texture::Texture;
|
||||
use cgmath::{Vector2, Vector3, Zero};
|
||||
use tobj::Model;
|
||||
|
||||
pub struct Material {
|
||||
pub name: String,
|
||||
pub diffuse_texture: Texture,
|
||||
|
@ -12,3 +16,42 @@ pub struct Mesh {
|
|||
pub num_elements: u32,
|
||||
pub material: usize,
|
||||
}
|
||||
|
||||
pub fn parse_obj(obj: &Vec<Model>) -> (Vec<Vertex>, Vec<u32>) {
|
||||
let mut combined_vertices = Vec::new();
|
||||
let mut combined_indices = Vec::new();
|
||||
let mut vertex_offset = 0;
|
||||
|
||||
for object in obj {
|
||||
let mesh: &_ = &object.mesh;
|
||||
let vertices: Vec<Vertex> = (0..mesh.positions.len() / 3)
|
||||
.map(|i| Vertex {
|
||||
position: Vector3::from([
|
||||
mesh.positions[i * 3],
|
||||
mesh.positions[i * 3 + 1],
|
||||
mesh.positions[i * 3 + 2],
|
||||
]),
|
||||
color: cgmath::Vector3::from([1.0, 1.0, 1.0]),
|
||||
normal: if !mesh.normals.is_empty() {
|
||||
Vector3::from([
|
||||
mesh.normals[i * 3],
|
||||
mesh.normals[i * 3 + 1],
|
||||
mesh.normals[i * 3 + 2],
|
||||
])
|
||||
} else {
|
||||
Vector3::zero()
|
||||
},
|
||||
tex_coords: if !mesh.texcoords.is_empty() {
|
||||
Vector2::from([mesh.texcoords[i * 2], mesh.texcoords[i * 2 + 1]])
|
||||
} else {
|
||||
Vector2::zero()
|
||||
},
|
||||
})
|
||||
.collect();
|
||||
combined_vertices.extend(vertices);
|
||||
combined_indices.extend(mesh.indices.iter().map(|&index| index + vertex_offset));
|
||||
vertex_offset += (mesh.positions.len() as u32) / 3;
|
||||
}
|
||||
|
||||
(combined_vertices, combined_indices)
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue