models cache

This commit is contained in:
Piotr 2020-09-26 00:32:07 +02:00
parent edc2d42113
commit f0a510d2a1
5 changed files with 48 additions and 20 deletions

View File

@ -48,11 +48,11 @@ impl Client for ExampleClient {
scale: vec3(0.5, 0.5, 0.5), scale: vec3(0.5, 0.5, 0.5),
..Transform::default() ..Transform::default()
}, },
model: model::Model::new("resources/objects/ground/ground.obj", cache), model: cache.get_model("resources/objects/ground/ground.obj"),
}; };
let robot = ModelComponent { let robot = ModelComponent {
transform: Transform::default(), transform: Transform::default(),
model: model::Model::new("resources/objects/robot/robot.obj", cache), model: cache.get_model("resources/objects/robot/robot.obj"),
}; };
let tree = ModelComponent { let tree = ModelComponent {
transform: Transform { transform: Transform {
@ -60,10 +60,9 @@ impl Client for ExampleClient {
scale: vec3(2.5, 2.5, 2.5), scale: vec3(2.5, 2.5, 2.5),
..Transform::default() ..Transform::default()
}, },
model: model::Model::new_ext( model: cache.get_model_ext(
"resources/objects/tree/tree_6_d.obj", "resources/objects/tree/tree_6_d.obj",
Some("tree_e.png"), Some("tree_e.png")
cache,
), ),
}; };
let tree2 = ModelComponent { let tree2 = ModelComponent {
@ -72,10 +71,9 @@ impl Client for ExampleClient {
scale: vec3(2.5, 2.5, 2.5), scale: vec3(2.5, 2.5, 2.5),
..Transform::default() ..Transform::default()
}, },
model: model::Model::new_ext( model: cache.get_model_ext(
"resources/objects/tree/tree_6_c.obj", "resources/objects/tree/tree_6_c.obj",
Some("tree_e.png"), Some("tree_e.png")
cache,
), ),
}; };
let tree3 = ModelComponent { let tree3 = ModelComponent {
@ -84,10 +82,9 @@ impl Client for ExampleClient {
scale: vec3(2.5, 2.5, 2.5), scale: vec3(2.5, 2.5, 2.5),
..Transform::default() ..Transform::default()
}, },
model: model::Model::new_ext( model: cache.get_model_ext(
"resources/objects/tree/tree_6_c.obj", "resources/objects/tree/tree_6_c.obj",
Some("tree_e.png"), Some("tree_e.png")
cache,
), ),
}; };
self.models = vec![tree, tree2, tree3, ground, robot]; self.models = vec![tree, tree2, tree3, ground, robot];

View File

@ -1,4 +1,5 @@
use crate::gaia::mesh::Texture; use crate::gaia::mesh::Texture;
use crate::gaia::model::Model;
use crate::gaia::utils::load_texture_from_dir; use crate::gaia::utils::load_texture_from_dir;
use std::collections::HashMap; use std::collections::HashMap;
use std::path::Path; use std::path::Path;
@ -6,15 +7,40 @@ use std::path::Path;
#[derive(Default)] #[derive(Default)]
pub struct AssetsCache { pub struct AssetsCache {
textures: HashMap<String, Texture>, textures: HashMap<String, Texture>,
models: HashMap<String, Model>,
} }
impl AssetsCache { impl AssetsCache {
pub fn load_material_texture(&mut self, dir: &str, path: &str, type_name: &str) -> Texture { pub fn get_model(&mut self, path: &str) -> Model {
match self.models.get(path) {
Some(model) => model.clone(),
None => {
println!("Loading model: {}", path);
let model = Model::new(path, self);
self.models.insert(path.to_string(), model.clone());
model
}
}
}
pub fn get_model_ext(&mut self, path: &str, diff_texture: Option<&str>) -> Model {
match self.models.get(path) {
Some(model) => model.clone(),
None => {
println!("Loading model: {}", path);
let model = Model::new_ext(path, diff_texture, self);
self.models.insert(path.to_string(), model.clone());
model
}
}
}
pub fn get_material_texture(&mut self, dir: &str, path: &str, type_name: &str) -> Texture {
match self.textures.get(path) { match self.textures.get(path) {
Some(texture) => texture.clone(), Some(texture) => texture.clone(),
None => { None => {
let directory: String = dir.into(); let directory: String = dir.into();
println!("{}", directory); println!("Loading texture: {}", path);
let texture = Texture { let texture = Texture {
id: unsafe { load_texture_from_dir(path, &directory) }, id: unsafe { load_texture_from_dir(path, &directory) },
type_: type_name.into(), type_: type_name.into(),

View File

@ -16,6 +16,7 @@ use crate::gaia::shader::Shader;
// Depending on how you pass the data to OpenGL, this may be bad. In this case it's not strictly // Depending on how you pass the data to OpenGL, this may be bad. In this case it's not strictly
// necessary though because of the `offset!` macro used below in setupMesh() // necessary though because of the `offset!` macro used below in setupMesh()
#[repr(C)] #[repr(C)]
#[derive(Clone)]
pub struct Vertex { pub struct Vertex {
// position // position
pub position: Vector3<f32>, pub position: Vector3<f32>,
@ -48,6 +49,7 @@ pub struct Texture {
pub path: String, pub path: String,
} }
#[derive(Clone)]
pub struct Mesh { pub struct Mesh {
/* Mesh Data */ /* Mesh Data */
pub vertices: Vec<Vertex>, pub vertices: Vec<Vertex>,

View File

@ -9,7 +9,7 @@ use cgmath::{vec2, vec3};
use std::path::Path; use std::path::Path;
use tobj; use tobj;
// #[derive(Default)] #[derive(Clone)]
pub struct Model { pub struct Model {
/* Model Data */ /* Model Data */
pub meshes: Vec<Mesh>, pub meshes: Vec<Mesh>,
@ -87,7 +87,7 @@ impl Model {
// 1. diffuse map // 1. diffuse map
if !material.diffuse_texture.is_empty() { if !material.diffuse_texture.is_empty() {
let texture = cache.load_material_texture( let texture = cache.get_material_texture(
&self.directory, &self.directory,
&material.diffuse_texture, &material.diffuse_texture,
"texture_diffuse", "texture_diffuse",
@ -96,7 +96,7 @@ impl Model {
} }
// 2. specular map // 2. specular map
if !material.specular_texture.is_empty() { if !material.specular_texture.is_empty() {
let texture = cache.load_material_texture( let texture = cache.get_material_texture(
&self.directory, &self.directory,
&material.specular_texture, &material.specular_texture,
"texture_specular", "texture_specular",
@ -105,7 +105,7 @@ impl Model {
} }
// 3. normal map // 3. normal map
if !material.normal_texture.is_empty() { if !material.normal_texture.is_empty() {
let texture = cache.load_material_texture( let texture = cache.get_material_texture(
&self.directory, &self.directory,
&material.normal_texture, &material.normal_texture,
"texture_normal", "texture_normal",
@ -115,7 +115,7 @@ impl Model {
// NOTE: no height maps // NOTE: no height maps
} else if diffuse_path.is_some() { } else if diffuse_path.is_some() {
println!("Loading {}", &diffuse_path.unwrap()); println!("Loading {}", &diffuse_path.unwrap());
let texture = cache.load_material_texture( let texture = cache.get_material_texture(
&self.directory, &self.directory,
&diffuse_path.unwrap(), &diffuse_path.unwrap(),
"texture_diffuse", "texture_diffuse",

View File

@ -1,7 +1,7 @@
use gl; use gl;
use image2::image::Image;
use image2::{io, ImagePtr, Rgb, Rgba}; use image2::{io, ImagePtr, Rgb, Rgba};
use std::os::raw::c_void; use std::os::raw::c_void;
use image2::image::Image;
pub unsafe fn load_texture(path: &str, file_format: &str) -> u32 { pub unsafe fn load_texture(path: &str, file_format: &str) -> u32 {
println!( println!(
@ -30,7 +30,10 @@ pub unsafe fn load_texture(path: &str, file_format: &str) -> u32 {
gl::BindTexture(gl::TEXTURE_2D, id); gl::BindTexture(gl::TEXTURE_2D, id);
gl::TexImage2D( gl::TexImage2D(
gl::TEXTURE_2D, 0, format as i32, dim.0 as i32, gl::TEXTURE_2D,
0,
format as i32,
dim.0 as i32,
dim.1 as i32, dim.1 as i32,
0, 0,
format, format,