fn load_model<T>(&mut self, path: T)
    where
        T: Into<PathBuf>,
    {
        let path: PathBuf = path.into();

        // retrieve the directory path of the filepath
        self.directory = path.parent().unwrap_or_else(|| Path::new("")).to_str().unwrap().into();
        let obj = tobj::load_obj(
            path,
            &LoadOptions {
                triangulate: true,
                single_index: true,
                ..Default::default()
            },
        );

        let (models, materials) = obj.unwrap();
        for model in models {
            let mesh = &model.mesh;
            let num_vertices = mesh.positions.len() / 3;

            // data to fill
            let mut vertices: Vec<Vertex> = Vec::with_capacity(num_vertices);
            let indices: Vec<u32> = mesh.indices.clone();

            for idx in 0..num_vertices {
                vertices.push(Vertex {
                    position: vec3(mesh.positions[idx * 3], mesh.positions[idx * 3 + 1], mesh.positions[idx * 3 + 2]),
                    normal: vec3(mesh.normals[idx * 3], mesh.normals[idx * 3 + 1], mesh.normals[idx * 3 + 2]),
                    tex_coords: vec2(mesh.texcoords[idx * 2], mesh.texcoords[idx * 2 + 1]),
                    ..Vertex::default()
                })
            }

            // process material
            match &materials {
                Ok(materials) => {
                    let mut textures = Vec::new();
                    if let Some(material_id) = mesh.material_id {
                        let material = &materials[material_id];

                        // 1. diffuse map
                        if !material.diffuse_texture.is_empty() {
                            let texture = self.load_material_texture(&material.diffuse_texture, "texture_diffuse");
                            textures.push(texture);
                        }
                        // 2. specular map
                        if !material.specular_texture.is_empty() {
                            let texture = self.load_material_texture(&material.specular_texture, "texture_specular");
                            textures.push(texture);
                        }
                        // 3. normal map
                        if !material.normal_texture.is_empty() {
                            let texture = self.load_material_texture(&material.normal_texture, "texture_normal");
                            textures.push(texture);
                        }
                        // NOTE: no height maps
                    }

                    self.meshes.push(Mesh::new(vertices, indices, textures));
                }
                Err(err) => {
                    println!("{}", err)
                }
            }
        }
    }
// loads a model from file and stores the resulting meshes in the meshes vector.
loads a model from file and stores the resulting meshes in the meshes vector.
loads a model from file and stores the resulting meshes in the meshes vector .
fn load_model<T>(&mut self, path: T)
    where
        T: Into<PathBuf>,
    {
        let path: PathBuf = path.into();
        // retrieve the directory path of the filepath
        self.directory = path.parent().unwrap_or_else(|| Path::new("")).to_str().unwrap().into();
        let obj = tobj::load_obj(
            path,
            &LoadOptions {
                triangulate: true,
                single_index: true,
                ..Default::default()
            },
        );
        let (models, materials) = obj.unwrap();
        for model in models {
            let mesh = &model.mesh;
            let num_vertices = mesh.positions.len() / 3;
            // data to fill
            let mut vertices: Vec<Vertex> = Vec::with_capacity(num_vertices);
            let indices: Vec<u32> = mesh.indices.clone();
            for idx in 0..num_vertices {
                vertices.push(Vertex {
                    position: vec3(mesh.positions[idx * 3], mesh.positions[idx * 3 + 1], mesh.positions[idx * 3 + 2]),
                    normal: vec3(mesh.normals[idx * 3], mesh.normals[idx * 3 + 1], mesh.normals[idx * 3 + 2]),
                    tex_coords: vec2(mesh.texcoords[idx * 2], mesh.texcoords[idx * 2 + 1]),
                    ..Vertex::default()
                })
            }
            // process material
            match &materials {
                Ok(materials) => {
                    let mut textures = Vec::new();
                    if let Some(material_id) = mesh.material_id {
                        let material = &materials[material_id];
                        // 1. diffuse map
                        if !material.diffuse_texture.is_empty() {
                            let texture = self.load_material_texture(&material.diffuse_texture, "texture_diffuse");
                            textures.push(texture);
                        }
                        // 2. specular map
                        if !material.specular_texture.is_empty() {
                            let texture = self.load_material_texture(&material.specular_texture, "texture_specular");
                            textures.push(texture);
                        }
                        // 3. normal map
                        if !material.normal_texture.is_empty() {
                            let texture = self.load_material_texture(&material.normal_texture, "texture_normal");
                            textures.push(texture);
                        }
                        // NOTE: no height maps
                    }
                    self.meshes.push(Mesh::new(vertices, indices, textures));
                }
                Err(err) => {
                    println!("{}", err)
                }
            }
        }
    }
fn load_model < T > ( & mut self , path : T ) where T : Into < PathBuf > , { let path : PathBuf = path . into ( ) ; // retrieve the directory path of the filepath self . directory = path . parent ( ) . unwrap_or_else ( | | Path :: new ( " " ) ) . to_str ( ) . unwrap ( ) . into ( ) ; let obj = tobj :: load_obj ( path , & LoadOptions { triangulate : true , single_index : true , .. Default :: default ( ) } , ) ; let ( models , materials ) = obj . unwrap ( ) ; for model in models { let mesh = & model . mesh ; let num_vertices = mesh . positions . len ( ) / 3 ; // data to fill let mut vertices : Vec < Vertex > = Vec :: with_capacity ( num_vertices ) ; let indices : Vec < u32 > = mesh . indices . clone ( ) ; for idx in 0 .. num_vertices { vertices . push ( Vertex { position : vec3 ( mesh . positions [ idx * 3 ] , mesh . positions [ idx * 3 + 1 ] , mesh . positions [ idx * 3 + 2 ] ) , normal : vec3 ( mesh . normals [ idx * 3 ] , mesh . normals [ idx * 3 + 1 ] , mesh . normals [ idx * 3 + 2 ] ) , tex_coords : vec2 ( mesh . texcoords [ idx * 2 ] , mesh . texcoords [ idx * 2 + 1 ] ) , .. Vertex :: default ( ) } ) } // process material match & materials { Ok ( materials ) => { let mut textures = Vec :: new ( ) ; if let Some ( material_id ) = mesh . material_id { let material = & materials [ material_id ] ; // 1. diffuse map if ! material . diffuse_texture . is_empty ( ) { let texture = self . load_material_texture ( & material . diffuse_texture , " " ) ; textures . push ( texture ) ; } // 2. specular map if ! material . specular_texture . is_empty ( ) { let texture = self . load_material_texture ( & material . specular_texture , " " ) ; textures . push ( texture ) ; } // 3. normal map if ! material . normal_texture . is_empty ( ) { let texture = self . load_material_texture ( & material . normal_texture , " " ) ; textures . push ( texture ) ; } // NOTE: no height maps } self . meshes . push ( Mesh :: new ( vertices , indices , textures ) ) ; } Err ( err ) => { println ! ( " " err ) } } } }
loads a model from file and stores the resulting meshes in the meshes vector.
loads a model from file and stores the resulting meshes in the meshes vector .
&mut self
path
T
&mut self
path
T