Multiple objects

This commit is contained in:
Piotr 2020-09-24 20:35:37 +02:00
parent 7047300ca8
commit 8bfaaa5d56
5 changed files with 75 additions and 27 deletions

View File

@ -8,7 +8,7 @@ use cgmath::{perspective, vec3, Deg, Matrix4, Point3};
use imgui_glfw_rs::glfw; use imgui_glfw_rs::glfw;
pub struct ExampleClient { pub struct ExampleClient {
model: ModelComponent, models: Vec<ModelComponent>,
camera: Camera, camera: Camera,
shader: shader::Shader, shader: shader::Shader,
show_object_info: bool, show_object_info: bool,
@ -17,14 +17,42 @@ pub struct ExampleClient {
impl ExampleClient { impl ExampleClient {
pub fn create() -> ExampleClient { pub fn create() -> ExampleClient {
let model = ModelComponent { let tree = ModelComponent {
transform: Transform{ position: vec3(10.0,0.0,26.0),
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")),
};
let tree2 = ModelComponent {
transform: Transform{ position: vec3(-9.0,0.0,-15.0),
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")),
};
let tree3 = ModelComponent {
transform: Transform{ position: vec3(15.0,0.0,-7.0),
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")),
};
let ground = ModelComponent {
transform: Transform {
scale: vec3(0.5, 0.5, 0.5),
..Transform::default()
},
model: model::Model::new("resources/objects/ground/ground.obj"),
};
let robot = ModelComponent {
transform: Transform::default(), transform: Transform::default(),
model: model::Model::new("resources/objects/robot/robot.obj"), model: model::Model::new("resources/objects/robot/robot.obj"),
}; };
ExampleClient { ExampleClient {
show_camera_info: true, show_camera_info: true,
show_object_info: false, show_object_info: false,
model: model, models: vec!(tree,tree2,tree3,ground, robot),
camera: Camera { camera: Camera {
position: Point3::new(0.0, 8.0, 13.0), position: Point3::new(0.0, 8.0, 13.0),
front: vec3(0.0, -0.4, -1.0), front: vec3(0.0, -0.4, -1.0),
@ -57,7 +85,9 @@ impl Client for ExampleClient {
self.shader.set_mat4(c_str!("projection"), &projection); self.shader.set_mat4(c_str!("projection"), &projection);
self.shader.set_mat4(c_str!("view"), &view); self.shader.set_mat4(c_str!("view"), &view);
self.model.draw(&self.shader); for model in self.models.iter() {
model.draw(&self.shader);
}
} }
fn update(&mut self, _engine: &mut Engine) {} fn update(&mut self, _engine: &mut Engine) {}
@ -79,7 +109,7 @@ impl Client for ExampleClient {
self.camera self.camera
.enable_mouse_movement(window.get_key(Key::LeftControl) != Action::Press); .enable_mouse_movement(window.get_key(Key::LeftControl) != Action::Press);
if window.get_key(Key::O) == Action::Press { if window.get_key(Key::O) == Action::Press {
println!("{:?}",self.camera); println!("{:?}", self.camera);
} }
} }
@ -125,20 +155,21 @@ impl Client for ExampleClient {
menu_bar.end(ui); menu_bar.end(ui);
} }
if self.show_camera_info { if self.show_camera_info {
let text = format!("{:?}", self.camera).replace("{","{\n").replace("}","\n}").replace("],","],\n"); let text = format!("{:?}", self.camera)
.replace("{", "{\n")
.replace("}", "\n}")
.replace("],", "],\n");
Window::new(im_str!("CameraInfo")) Window::new(im_str!("CameraInfo"))
.size([300.0,300.0], Condition::Always) .size([300.0, 300.0], Condition::Always)
.position( .position([50.0, 50.0], Condition::Always)
[50.0, 50.0], .title_bar(false)
Condition::Always, .scroll_bar(false)
).title_bar(false) .collapsible(false)
.scroll_bar(false) .build(&ui, || {
.collapsible(false) ui.text(im_str!("Camera info"));
.build(&ui, || { ui.separator();
ui.text(im_str!("Camera info")); ui.text(text);
ui.separator(); });
ui.text(text);
});
} }
if self.show_object_info { if self.show_object_info {
let mut show_window = self.show_object_info; let mut show_window = self.show_object_info;
@ -146,7 +177,7 @@ impl Client for ExampleClient {
.size([250.0, 250.0], Condition::FirstUseEver) .size([250.0, 250.0], Condition::FirstUseEver)
.opened(&mut show_window) .opened(&mut show_window)
.build(&ui, || { .build(&ui, || {
let mut selected_mut = vec![&mut self.model.transform]; let mut selected_mut = vec![&mut self.models[0].transform];
<Transform as imgui_inspect::InspectRenderStruct<Transform>>::render_mut( <Transform as imgui_inspect::InspectRenderStruct<Transform>>::render_mut(
&mut selected_mut, &mut selected_mut,
"Object info", "Object info",

View File

@ -25,7 +25,7 @@ const SPEED: f32 = 2.5;
const SENSITIVTY: f32 = 0.1; const SENSITIVTY: f32 = 0.1;
const ZOOM: f32 = 45.0; const ZOOM: f32 = 45.0;
#[derive(Debug,Clone,Copy)] #[derive(Debug, Clone, Copy)]
pub struct Camera { pub struct Camera {
// Camera Attributes // Camera Attributes
pub position: Point3, pub position: Point3,

View File

@ -1,6 +1,6 @@
use crate::gaia::model::Model; use crate::gaia::model::Model;
use crate::gaia::shader::Shader; use crate::gaia::shader::Shader;
use cgmath::{vec3, Matrix4, Vector3, Rad}; use cgmath::{vec3, Matrix4, Rad, Vector3};
use imgui_glfw_rs::imgui; use imgui_glfw_rs::imgui;
use imgui_inspect::InspectArgsDefault; use imgui_inspect::InspectArgsDefault;
use imgui_inspect::InspectRenderDefault; use imgui_inspect::InspectRenderDefault;
@ -89,7 +89,7 @@ pub struct ModelComponent {
} }
impl ModelComponent { impl ModelComponent {
pub unsafe fn draw(&mut self, shader: &Shader) { pub unsafe fn draw(&self, shader: &Shader) {
let matrix = self.transform.get_matrix(); let matrix = self.transform.get_matrix();
shader.set_mat4(c_str!("model"), &matrix); shader.set_mat4(c_str!("model"), &matrix);

View File

@ -70,7 +70,10 @@ impl Engine {
Window::new(im_str!("EngineInfo")) Window::new(im_str!("EngineInfo"))
.size(size, Condition::Always) .size(size, Condition::Always)
.position( .position(
[self.window_size.0 - size[0] - offset, self.window_size.1 - size[1] - offset], [
self.window_size.0 - size[0] - offset,
self.window_size.1 - size[1] - offset,
],
Condition::Always, Condition::Always,
) )
.no_decoration() .no_decoration()

View File

@ -18,7 +18,7 @@ pub struct Model {
impl Model { impl Model {
/// constructor, expects a filepath to a 3D model. /// constructor, expects a filepath to a 3D model.
pub fn new(path: &str) -> Model { pub fn new_ext(path: &str, diff_texture: Option<&str>) -> Model {
let pathObj = Path::new(path); let pathObj = Path::new(path);
let mut model = Model { let mut model = Model {
meshes: Vec::<Mesh>::new(), meshes: Vec::<Mesh>::new(),
@ -30,10 +30,14 @@ impl Model {
.unwrap() .unwrap()
.into(), .into(),
}; };
model.load_model(path); model.load_model(path, diff_texture);
model model
} }
pub fn new(path: &str) -> Model {
Model::new_ext(path, None)
}
pub fn Draw(&self, shader: &Shader) { pub fn Draw(&self, shader: &Shader) {
for mesh in &self.meshes { for mesh in &self.meshes {
unsafe { unsafe {
@ -43,7 +47,7 @@ impl Model {
} }
// 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(&mut self, path: &str) { fn load_model(&mut self, path: &str, diffuse_path: Option<&str>) {
let path = Path::new(path); let path = Path::new(path);
println!("Started loading model from path: {}", path.display()); println!("Started loading model from path: {}", path.display());
@ -98,7 +102,17 @@ impl Model {
self.load_material_texture(&material.normal_texture, "texture_normal"); self.load_material_texture(&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() {
println!("Loading {}", &diffuse_path.unwrap());
let texture = self.load_material_texture(&diffuse_path.unwrap(), "texture_diffuse");
textures.push(texture);
} else {
println!(
"There are no materials with id {} for: {}",
mesh.material_id.unwrap_or(404usize),
path.display()
);
} }
self.meshes.push(Mesh::new(vertices, indices, textures)); self.meshes.push(Mesh::new(vertices, indices, textures));