Untitled

mail@pastecode.io avatar
unknown
rust
2 years ago
1.9 kB
2
Indexable
Never
fn create_mesh() -> rend3::types::Mesh {
    let mut vertex_positions: Vec<glam::Vec3> = Vec::new();
    let mut index_data: Vec<u32> = Vec::new();

    let file_data: String = fs::read_to_string("c:/users/user/desktop/untitled.obj").expect("Error opening file!");
    let lines: Vec<&str> = file_data.split("\n").collect();

    for line in lines {
        let line_first_letter = line.chars().nth(0).unwrap_or(' ');

        if line.len() < 2 {
            continue
        }

        let data = &line[2..];

        match line_first_letter {
            'v' => {
                let verts: Vec<&str> = data.split(" ").collect();
                let x: f32 = verts[0].parse::<f32>().unwrap_or(0.0);
                let y: f32 = verts[1].parse::<f32>().unwrap_or(0.0);
                let z: f32 = verts[2].parse::<f32>().unwrap_or(0.0);
                vertex_positions.push(vertex([x, y, z]));
            },
            'f' => {
                let verts: Vec<&str> = data.split(" ").collect();
                let x: u32 = verts[0].parse::<u32>().unwrap_or(0);
                let y: u32 = verts[1].parse::<u32>().unwrap_or(0);
                let z: u32 = verts[2].parse::<u32>().unwrap_or(0);
                index_data.push(x);
                index_data.push(y);
                index_data.push(z);
                let lax = index_data.get(index_data.len()-3).copied().unwrap();
                let lay = index_data.get(index_data.len()-2).copied().unwrap();
                let laz = index_data.last().copied().unwrap();
                assert!(lax < 2000);
                assert!(lay < 2000);
                assert!(laz < 2000);
            },
            _ => continue
        }
    }

    rend3::types::MeshBuilder::new(vertex_positions, rend3::types::Handedness::Right)
        .with_indices(index_data)
        .build()
        .unwrap()
}