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::camera::*;
use crate::gaia::client::Client; use crate::gaia::client::Client;
use crate::gaia::components::{ModelComponent, Transform}; use crate::gaia::components::{ModelComponent, Transform};
@ -6,8 +7,6 @@ use crate::gaia::engine::Engine;
use crate::gaia::*; use crate::gaia::*;
use cgmath::{perspective, vec3, Deg, Matrix4, Point3}; use cgmath::{perspective, vec3, Deg, Matrix4, Point3};
use imgui_glfw_rs::glfw; use imgui_glfw_rs::glfw;
use crate::gaia::assets_cache::AssetsCache;
pub struct ExampleClient { pub struct ExampleClient {
models: Vec<ModelComponent>, models: Vec<ModelComponent>,
@ -49,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: model::Model::new("resources/objects/ground/ground.obj", cache),
}; };
let robot = ModelComponent { let robot = ModelComponent {
transform: Transform::default(), 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 { let tree = ModelComponent {
transform: Transform { transform: Transform {
@ -61,7 +60,11 @@ 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("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 { let tree2 = ModelComponent {
transform: Transform { transform: Transform {
@ -69,7 +72,11 @@ 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("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 { let tree3 = ModelComponent {
transform: Transform { transform: Transform {
@ -77,7 +84,11 @@ 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("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]; 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 crate::gaia::utils::load_texture_from_dir;
use std::collections::HashMap; use std::collections::HashMap;
use crate::gaia::mesh::Texture;
use std::path::Path; use std::path::Path;
#[derive(Default)] #[derive(Default)]
pub struct AssetsCache { pub struct AssetsCache {
textures: HashMap<String, Texture> textures: HashMap<String, Texture>,
} }
impl AssetsCache { impl AssetsCache {
@ -13,14 +13,14 @@ impl AssetsCache {
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!("{}", directory);
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(),
path: path.into(), path: path.into(),
}; };
self.textures.insert(path.to_string(),texture.clone()); self.textures.insert(path.to_string(), texture.clone());
texture texture
} }
} }

View File

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

View File

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

View File

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

View File

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

View File

@ -1,36 +1,33 @@
use gl; use gl;
use image2::{ use image2::{io, ImagePtr, Rgb, Rgba};
ImagePtr,
Rgb,Rgba,
io,
};
use std::os::raw::c_void; use std::os::raw::c_void;
use std::path::Path;
pub unsafe fn load_texture(path: &str, file_format: &str) -> u32 {
pub unsafe fn load_texture(path: &str, file_format: &str) -> u32 { println!(
println!("[stb]Loading texture from path: {} with format {}", path, file_format); "[stb]Loading texture from path: {} with format {}",
path, file_format
);
let mut id = 0; let mut id = 0;
use image2::image::Image; use image2::image::Image;
gl::GenTextures(1, &mut id); gl::GenTextures(1, &mut id);
let (data, dim, format) = match file_format { let (data, dim, format) = match file_format {
"png" => { "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 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 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); 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 dot = filename.find(".").unwrap_or_default() + 1usize;
let (_, format) = filename.split_at(dot); let (_, format) = filename.split_at(dot);
load_texture(&fullpath,format) load_texture(&fullpath, format)
} }