This commit is contained in:
Piotr 2020-09-26 00:04:55 +02:00
parent 9607498459
commit 54b6938c30
7 changed files with 63 additions and 41 deletions

View File

@ -1,3 +1,4 @@
use crate::gaia::assets_cache::AssetsCache;
use crate::gaia::camera::*;
use crate::gaia::client::Client;
use crate::gaia::components::{ModelComponent, Transform};
@ -6,8 +7,6 @@ use crate::gaia::engine::Engine;
use crate::gaia::*;
use cgmath::{perspective, vec3, Deg, Matrix4, Point3};
use imgui_glfw_rs::glfw;
use crate::gaia::assets_cache::AssetsCache;
pub struct ExampleClient {
models: Vec<ModelComponent>,
@ -49,11 +48,11 @@ impl Client for ExampleClient {
scale: vec3(0.5, 0.5, 0.5),
..Transform::default()
},
model: model::Model::new("resources/objects/ground/ground.obj",cache),
model: model::Model::new("resources/objects/ground/ground.obj", cache),
};
let robot = ModelComponent {
transform: Transform::default(),
model: model::Model::new("resources/objects/robot/robot.obj",cache),
model: model::Model::new("resources/objects/robot/robot.obj", cache),
};
let tree = ModelComponent {
transform: Transform {
@ -61,7 +60,11 @@ impl Client for ExampleClient {
scale: vec3(2.5, 2.5, 2.5),
..Transform::default()
},
model: model::Model::new_ext("resources/objects/tree/tree_6_d.obj", Some("tree_e.png"),cache),
model: model::Model::new_ext(
"resources/objects/tree/tree_6_d.obj",
Some("tree_e.png"),
cache,
),
};
let tree2 = ModelComponent {
transform: Transform {
@ -69,7 +72,11 @@ impl Client for ExampleClient {
scale: vec3(2.5, 2.5, 2.5),
..Transform::default()
},
model: model::Model::new_ext("resources/objects/tree/tree_6_c.obj", Some("tree_e.png"),cache),
model: model::Model::new_ext(
"resources/objects/tree/tree_6_c.obj",
Some("tree_e.png"),
cache,
),
};
let tree3 = ModelComponent {
transform: Transform {
@ -77,7 +84,11 @@ impl Client for ExampleClient {
scale: vec3(2.5, 2.5, 2.5),
..Transform::default()
},
model: model::Model::new_ext("resources/objects/tree/tree_6_c.obj", Some("tree_e.png"),cache),
model: model::Model::new_ext(
"resources/objects/tree/tree_6_c.obj",
Some("tree_e.png"),
cache,
),
};
self.models = vec![tree, tree2, tree3, ground, robot];
}

View File

@ -1,11 +1,11 @@
use crate::gaia::mesh::Texture;
use crate::gaia::utils::load_texture_from_dir;
use std::collections::HashMap;
use crate::gaia::mesh::Texture;
use std::path::Path;
#[derive(Default)]
pub struct AssetsCache {
textures: HashMap<String, Texture>
textures: HashMap<String, Texture>,
}
impl AssetsCache {
@ -13,14 +13,14 @@ impl AssetsCache {
match self.textures.get(path) {
Some(texture) => texture.clone(),
None => {
let directory : String = dir.into();
println!("{}",directory);
let directory: String = dir.into();
println!("{}", directory);
let texture = Texture {
id: unsafe { load_texture_from_dir(path, &directory) },
type_: type_name.into(),
path: path.into(),
};
self.textures.insert(path.to_string(),texture.clone());
self.textures.insert(path.to_string(), texture.clone());
texture
}
}

View File

@ -1,6 +1,6 @@
use crate::gaia::assets_cache::AssetsCache;
use crate::gaia::engine::Engine;
use imgui_glfw_rs::glfw;
use crate::gaia::assets_cache::AssetsCache;
pub trait Client {
fn load_assets(&mut self, cache: &mut AssetsCache);

View File

@ -1,9 +1,9 @@
use crate::example_client::ExampleClient;
use crate::gaia::assets_cache::AssetsCache;
use crate::gaia::bg_info::BgInfo;
use crate::gaia::camera::*;
use crate::gaia::client::Client;
use crate::gaia::consts;
use crate::gaia::assets_cache::AssetsCache;
use cgmath::Point3;
use imgui_glfw_rs::glfw;
use imgui_glfw_rs::glfw::{Action, Context, Key};

View File

@ -1,5 +1,6 @@
pub mod macros;
pub mod assets_cache;
pub mod bg_info;
pub mod camera;
pub mod client;
@ -7,7 +8,6 @@ pub mod components;
pub mod consts;
pub mod engine;
pub mod mesh;
pub mod assets_cache;
pub mod model;
pub mod shader;
pub mod utils;

View File

@ -1,10 +1,10 @@
#![allow(non_snake_case)]
#![allow(dead_code)]
use crate::gaia::assets_cache::AssetsCache;
use crate::gaia::mesh::{Mesh, Texture, Vertex};
use crate::gaia::shader::Shader;
use crate::gaia::utils::*;
use crate::gaia::assets_cache::AssetsCache;
use cgmath::{vec2, vec3};
use std::path::Path;
use tobj;
@ -87,25 +87,39 @@ impl Model {
// 1. diffuse map
if !material.diffuse_texture.is_empty() {
let texture = cache.load_material_texture(&self.directory, &material.diffuse_texture, "texture_diffuse");
let texture = cache.load_material_texture(
&self.directory,
&material.diffuse_texture,
"texture_diffuse",
);
textures.push(texture);
}
// 2. specular map
if !material.specular_texture.is_empty() {
let texture =
cache.load_material_texture(&self.directory, &material.specular_texture, "texture_specular");
let texture = cache.load_material_texture(
&self.directory,
&material.specular_texture,
"texture_specular",
);
textures.push(texture);
}
// 3. normal map
if !material.normal_texture.is_empty() {
let texture =
cache.load_material_texture(&self.directory, &material.normal_texture, "texture_normal");
let texture = cache.load_material_texture(
&self.directory,
&material.normal_texture,
"texture_normal",
);
textures.push(texture);
}
// NOTE: no height maps
} else if diffuse_path.is_some() {
println!("Loading {}", &diffuse_path.unwrap());
let texture = cache.load_material_texture(&self.directory, &diffuse_path.unwrap(), "texture_diffuse");
let texture = cache.load_material_texture(
&self.directory,
&diffuse_path.unwrap(),
"texture_diffuse",
);
textures.push(texture);
} else {
println!("There are no materials for: {}", path.display());

View File

@ -1,36 +1,33 @@
use gl;
use image2::{
ImagePtr,
Rgb,Rgba,
io,
};
use image2::{io, ImagePtr, Rgb, Rgba};
use std::os::raw::c_void;
use std::path::Path;
pub unsafe fn load_texture(path: &str, file_format: &str) -> u32 {
println!("[stb]Loading texture from path: {} with format {}", path, file_format);
println!(
"[stb]Loading texture from path: {} with format {}",
path, file_format
);
let mut id = 0;
use image2::image::Image;
gl::GenTextures(1, &mut id);
let (data, dim, format) = match file_format {
"png" => {
let img : ImagePtr<u8, Rgba> = io::read_u8(path).unwrap();
let img: ImagePtr<u8, Rgba> = io::read_u8(path).unwrap();
let img_data = img.data().to_vec();
let (x,y,_) = img.shape();
let (x, y, _) = img.shape();
(img_data,(x as i32,y as i32),gl::RGBA)
},
(img_data, (x as i32, y as i32), gl::RGBA)
}
_ => {
let img : ImagePtr<u8, Rgb> = io::read_u8(path).unwrap();
let img: ImagePtr<u8, Rgb> = io::read_u8(path).unwrap();
let img_data = img.data().to_vec();
let (x,y,_) = img.shape();
let (x, y, _) = img.shape();
(img_data,(x as i32,y as i32),gl::RGB)
},
(img_data, (x as i32, y as i32), gl::RGB)
}
};
gl::BindTexture(gl::TEXTURE_2D, id);
@ -64,5 +61,5 @@ pub unsafe fn load_texture_from_dir(filename: &str, directory: &str) -> u32 {
let dot = filename.find(".").unwrap_or_default() + 1usize;
let (_, format) = filename.split_at(dot);
load_texture(&fullpath,format)
load_texture(&fullpath, format)
}