From 1544f55bbddbb0ce105080a91a96acf36f0faa45 Mon Sep 17 00:00:00 2001 From: Piotr Date: Sat, 26 Sep 2020 22:19:38 +0200 Subject: [PATCH] Debug light info --- Cargo.toml | 5 +- src/example_client.rs | 101 ++++++++++++++++++--------------------- src/gaia/components.rs | 48 +------------------ src/gaia/imgui_helper.rs | 49 +++++++++++++++++++ src/gaia/light.rs | 83 ++++++++++++++++++++++++++++++++ src/gaia/mod.rs | 1 + 6 files changed, 181 insertions(+), 106 deletions(-) create mode 100644 src/gaia/imgui_helper.rs diff --git a/Cargo.toml b/Cargo.toml index f7836c9..6419545 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -10,18 +10,15 @@ panic = 'abort' lto = true [dependencies] -cgmath = "0.17.0" gl = "0.14.0" imgui-glfw-rs = { git = "https://github.com/Leinnan/imgui-glfw-rs" } +cgmath = "0.17.0" imgui-inspect = "0.5.0" imgui-inspect-derive = "0.5.0" tobj = "2.0.2" human-panic = "1.0.3" inline_tweak = "1.0.8" image2 = { git = "https://github.com/Leinnan/image2-rs", branch="legacy", default-features = false, features=["io"] } -# serde = "1.0.116" -# serde_derive = "1.0.116" -# assets_manager ={ version="0.3.2", features= ["ron", "bincode"]} [profile.dev.package."*"] opt-level = 2 \ No newline at end of file diff --git a/src/example_client.rs b/src/example_client.rs index f929bb9..4934571 100644 --- a/src/example_client.rs +++ b/src/example_client.rs @@ -14,30 +14,18 @@ use imgui_glfw_rs::glfw; pub struct ExampleClient { models: Vec, camera: Camera, - shader: shader::Shader, + lighting_system: LightingSystem, sky: Sky, - point_lights: Vec, show_object_info: bool, show_camera_info: bool, object_info_id: i32, + show_light_info: bool, + light_info_id: i32, } impl ExampleClient { pub fn create() -> ExampleClient { let sky = unsafe { Sky::new() }; - let pointLightPositions: [Vector3; 4] = [ - vec3(0.7, 5.0, 2.0), - vec3(2.3, 3.3, -4.0), - vec3(-4.0, 4.0, -12.0), - vec3(0.0, 2.0, -3.0), - ]; - let mut point_lights = vec![]; - for light in pointLightPositions.iter() { - point_lights.push(PointLight { - pos: *light, - ..PointLight::default() - }) - } ExampleClient { object_info_id: 0, show_camera_info: true, @@ -52,12 +40,10 @@ impl ExampleClient { pitch: -20.0, ..Camera::default() }, - shader: shader::Shader::from_file( - "resources/shaders/multiple_lights.vs", - "resources/shaders/multiple_lights.fs", - ), + lighting_system: LightingSystem::default(), sky: sky, - point_lights: point_lights, + show_light_info: false, + light_info_id: 0, } } } @@ -107,9 +93,6 @@ impl Client for ExampleClient { } unsafe fn draw(&mut self) { - use inline_tweak::*; - self.shader.use_program(); - // view/projection transformations let projection: Matrix4 = perspective( Deg(self.camera.zoom), @@ -118,39 +101,12 @@ impl Client for ExampleClient { 1000.0, ); let view = self.camera.get_view_matrix(); - self.shader.set_mat4(c_str!("projection"), &projection); - self.shader.set_mat4(c_str!("view"), &view); - self.shader - .set_vector3(c_str!("viewPos"), &self.camera.position.to_vec()); - self.shader.setFloat(c_str!("material.shininess"), 32.0); - - self.shader.set_vec3( - c_str!("dirLight.direction"), - tweak!(-0.3), - tweak!(-1.0), - tweak!(-0.3), - ); - self.shader.set_vec3( - c_str!("dirLight.ambient"), - tweak!(0.14), - tweak!(0.14), - tweak!(0.14), - ); - self.shader.set_vec3( - c_str!("dirLight.diffuse"), - tweak!(0.4), - tweak!(0.4), - tweak!(0.4), - ); - self.shader - .set_vec3(c_str!("dirLight.specular"), 0.5, 0.5, 0.5); - // point light 1 - for (i, v) in self.point_lights.iter().enumerate() { - v.shader_update(i, &self.shader); - } + let view_pos = self.camera.position.to_vec(); + self.lighting_system + .prepare_for_draw(&projection, &view, &view_pos); for model in self.models.iter() { - model.draw(&self.shader); + model.draw(&self.lighting_system.shader); } self.sky.draw(view, projection); } @@ -189,6 +145,12 @@ impl Client for ExampleClient { { self.show_object_info = !self.show_object_info; } + if MenuItem::new(im_str!("Show lights info")) + .selected(self.show_light_info) + .build(ui) + { + self.show_light_info = !self.show_light_info; + } if MenuItem::new(im_str!("Show camera info")) .selected(self.show_camera_info) .build(ui) @@ -219,8 +181,8 @@ impl Client for ExampleClient { if self.show_object_info { let mut id = self.object_info_id; let max: i32 = self.models.len() as i32 - 1; - println!("max: {}", max); let mut show_window = self.show_object_info; + Window::new(im_str!("Object info")) .size([250.0, 250.0], Condition::FirstUseEver) .opened(&mut show_window) @@ -238,6 +200,35 @@ impl Client for ExampleClient { self.object_info_id = id; self.show_object_info = show_window; } + if self.show_light_info { + let mut id = self.light_info_id; + let max: i32 = self.lighting_system.point_lights.len() as i32 - 1; + let mut show_window = self.show_light_info; + + Window::new(im_str!("Lights info")) + .size([250.0, 250.0], Condition::FirstUseEver) + .opened(&mut show_window) + .build(&ui, || { + ui.drag_int(im_str!("Light ID"), &mut id) + .min(0) + .max(max) + .build(); + // id = if id < 0 { 0 } else if id > max { max } else { id }; + let mut selected_mut = + vec![&mut self.lighting_system.point_lights[id as usize]]; + >::render_mut( + &mut selected_mut, + "PointLightInfo", + &ui, + &InspectArgsStruct::default(), + ); + if ui.button(im_str!("Print light info"), [0.0, 0.0]) { + println!("{:?}",selected_mut); + } + }); + self.light_info_id = id; + self.show_light_info = show_window; + } } fn on_mouse_scroll(&mut self, yoffset: f32) { diff --git a/src/gaia/components.rs b/src/gaia/components.rs index c73b269..14accdf 100644 --- a/src/gaia/components.rs +++ b/src/gaia/components.rs @@ -1,56 +1,10 @@ +use crate::gaia::imgui_helper::*; use crate::gaia::model::Model; use crate::gaia::shader::Shader; use cgmath::{vec3, Matrix4, Rad, Vector3}; use imgui_glfw_rs::imgui; -use imgui_inspect::InspectArgsDefault; -use imgui_inspect::InspectRenderDefault; use imgui_inspect_derive::Inspect; -struct CgmathVec3f32; -impl InspectRenderDefault> for CgmathVec3f32 { - fn render( - data: &[&Vector3], - label: &'static str, - ui: &imgui::Ui, - _args: &InspectArgsDefault, - ) { - ui.text(label); - ui.text(format!("{:?}", data)); - } - - fn render_mut( - data: &mut [&mut Vector3], - label: &'static str, - ui: &imgui::Ui, - _args: &InspectArgsDefault, - ) -> bool { - use imgui::*; - let id_x = im_str!("x##{}", label); - let id_y = im_str!("y##{}", label); - let id_z = im_str!("z##{}", label); - ui.text(label); - let mut change = false; - for el in data.iter_mut() { - change |= ui - .input_float(&id_x, &mut el.x) - .step(0.01) - .step_fast(1.0) - .build(); - change |= ui - .input_float(&id_y, &mut el.y) - .step(0.01) - .step_fast(1.0) - .build(); - change |= ui - .input_float(&id_z, &mut el.z) - .step(0.01) - .step_fast(1.0) - .build(); - } - change - } -} - #[derive(Inspect, Clone, Copy, Debug)] pub struct Transform { #[inspect(proxy_type = "CgmathVec3f32")] diff --git a/src/gaia/imgui_helper.rs b/src/gaia/imgui_helper.rs new file mode 100644 index 0000000..662ef1d --- /dev/null +++ b/src/gaia/imgui_helper.rs @@ -0,0 +1,49 @@ +use cgmath::{vec3, Matrix4, Rad, Vector3}; +use imgui_glfw_rs::imgui; +use imgui_inspect::InspectArgsDefault; +use imgui_inspect::InspectRenderDefault; + +pub struct CgmathVec3f32; +impl InspectRenderDefault> for CgmathVec3f32 { + fn render( + data: &[&Vector3], + label: &'static str, + ui: &imgui::Ui, + _args: &InspectArgsDefault, + ) { + ui.text(label); + ui.text(format!("{:?}", data)); + } + + fn render_mut( + data: &mut [&mut Vector3], + label: &'static str, + ui: &imgui::Ui, + _args: &InspectArgsDefault, + ) -> bool { + use imgui::*; + let id_x = im_str!("x##{}", label); + let id_y = im_str!("y##{}", label); + let id_z = im_str!("z##{}", label); + ui.text(label); + let mut change = false; + for el in data.iter_mut() { + change |= ui + .input_float(&id_x, &mut el.x) + .step(0.01) + .step_fast(1.0) + .build(); + change |= ui + .input_float(&id_y, &mut el.y) + .step(0.01) + .step_fast(1.0) + .build(); + change |= ui + .input_float(&id_z, &mut el.z) + .step(0.01) + .step_fast(1.0) + .build(); + } + change + } +} diff --git a/src/gaia/light.rs b/src/gaia/light.rs index db74ac9..35e54a0 100644 --- a/src/gaia/light.rs +++ b/src/gaia/light.rs @@ -1,10 +1,18 @@ +use crate::gaia::imgui_helper::*; use crate::gaia::shader::Shader; use cgmath::{perspective, vec3, Deg, Matrix4, Point3, Vector3}; +use imgui_glfw_rs::imgui; +use imgui_inspect_derive::Inspect; +#[derive(Inspect, Clone, Copy, Debug)] pub struct PointLight { + #[inspect(proxy_type = "CgmathVec3f32")] pub pos: Vector3, + #[inspect(proxy_type = "CgmathVec3f32")] pub ambient: Vector3, + #[inspect(proxy_type = "CgmathVec3f32")] pub diffuse: Vector3, + #[inspect(proxy_type = "CgmathVec3f32")] pub specular: Vector3, pub constant: f32, pub linear: f32, @@ -70,3 +78,78 @@ impl Default for PointLight { } } } + +pub struct LightingSystem { + pub shader: Shader, + pub point_lights: [PointLight; 4], +} + +impl LightingSystem { + pub unsafe fn prepare_for_draw( + &self, + projection: &Matrix4, + view: &Matrix4, + view_pos: &Vector3, + ) { + self.shader.use_program(); + + use inline_tweak::*; + self.shader.set_mat4(c_str!("projection"), projection); + self.shader.set_mat4(c_str!("view"), view); + self.shader.set_vector3(c_str!("viewPos"), view_pos); + self.shader.setFloat(c_str!("material.shininess"), 32.0); + + self.shader.set_vec3( + c_str!("dirLight.direction"), + tweak!(-0.3), + tweak!(-1.0), + tweak!(-0.3), + ); + self.shader.set_vec3( + c_str!("dirLight.ambient"), + tweak!(0.14), + tweak!(0.14), + tweak!(0.14), + ); + self.shader.set_vec3( + c_str!("dirLight.diffuse"), + tweak!(0.4), + tweak!(0.4), + tweak!(0.4), + ); + self.shader + .set_vec3(c_str!("dirLight.specular"), 0.5, 0.5, 0.5); + for (i, v) in self.point_lights.iter().enumerate() { + v.shader_update(i, &self.shader); + } + } +} + +impl Default for LightingSystem { + fn default() -> Self { + LightingSystem { + point_lights: [ + PointLight { + pos: vec3(0.7, 5.0, 2.0), + ..PointLight::default() + }, + PointLight { + pos: vec3(2.3, 3.3, -4.0), + ..PointLight::default() + }, + PointLight { + pos: vec3(-4.0, 4.0, -12.0), + ..PointLight::default() + }, + PointLight { + pos: vec3(0.0, 2.0, -3.0), + ..PointLight::default() + }, + ], + shader: Shader::from_file( + "resources/shaders/multiple_lights.vs", + "resources/shaders/multiple_lights.fs", + ), + } + } +} diff --git a/src/gaia/mod.rs b/src/gaia/mod.rs index 3094a15..b5317db 100644 --- a/src/gaia/mod.rs +++ b/src/gaia/mod.rs @@ -7,6 +7,7 @@ pub mod client; pub mod components; pub mod consts; pub mod engine; +pub mod imgui_helper; pub mod light; pub mod mesh; pub mod model;