From a708e5a1c165ad62c3c1d59115577bfd0548b5bf Mon Sep 17 00:00:00 2001 From: Piotr Date: Wed, 23 Sep 2020 22:15:01 +0200 Subject: [PATCH] Handle input in client --- src/example_client.rs | 31 +++++++++++++++++++++ src/gaia/client.rs | 4 +++ src/gaia/engine.rs | 64 ++----------------------------------------- 3 files changed, 38 insertions(+), 61 deletions(-) diff --git a/src/example_client.rs b/src/example_client.rs index 649f1be..2e5a511 100644 --- a/src/example_client.rs +++ b/src/example_client.rs @@ -3,6 +3,7 @@ use crate::gaia::camera::*; use crate::gaia::consts; use crate::gaia::*; #[macro_use] +use imgui_glfw_rs::glfw; use crate::gaia::macros; use crate::gaia::engine::Engine; use crate::gaia::client::Client; @@ -58,4 +59,34 @@ impl Client for ExampleClient { fn update(&mut self, engine: &mut Engine) { } + + + fn process_input(&mut self, window: &glfw::Window, delta: f32) { + use imgui_glfw_rs::glfw::{Action, Key}; + if window.get_key(Key::W) == Action::Press { + self.camera + .process_keyboard(Camera_Movement::FORWARD, delta); + } + if window.get_key(Key::S) == Action::Press { + self.camera + .process_keyboard(Camera_Movement::BACKWARD, delta); + } + if window.get_key(Key::A) == Action::Press { + self.camera + .process_keyboard(Camera_Movement::LEFT, delta); + } + if window.get_key(Key::D) == Action::Press { + self.camera + .process_keyboard(Camera_Movement::RIGHT, delta); + } + self.camera + .enable_mouse_movement(window.get_key(Key::LeftControl) != Action::Press); + } + + fn on_mouse_scroll(&mut self, yoffset: f32){ + self.camera.process_mouse_scroll(yoffset as f32); + } + fn on_mouse_move(&mut self, x: f32, y: f32){ + self.camera.process_mouse_movement(x, y, true); + } } \ No newline at end of file diff --git a/src/gaia/client.rs b/src/gaia/client.rs index e3c92f7..bfe07c1 100644 --- a/src/gaia/client.rs +++ b/src/gaia/client.rs @@ -1,7 +1,11 @@ use crate::gaia::engine::Engine; +use imgui_glfw_rs::glfw; pub trait Client { fn update(&mut self, engine: &mut Engine); + fn process_input(&mut self, window: &glfw::Window, delta: f32); + fn on_mouse_scroll(&mut self, yoffset: f32); + fn on_mouse_move(&mut self, x: f32, y: f32); // fn draw(&mut self, engine: &mut Engine) where T: Client; unsafe fn draw(&mut self); } \ No newline at end of file diff --git a/src/gaia/engine.rs b/src/gaia/engine.rs index c9cadeb..e3eced6 100644 --- a/src/gaia/engine.rs +++ b/src/gaia/engine.rs @@ -20,8 +20,6 @@ pub struct Engine { pub glfw: imgui_glfw_rs::glfw::Glfw, pub imgui: imgui::Context, pub imgui_glfw: ImguiGLFW, - pub shader: shader::Shader, - pub models: Vec, pub client: Box, } @@ -34,20 +32,6 @@ impl Engine { // timing let mut delta_time: f32; // time between current frame and last frame let mut last_frame: f32 = 0.0; - { - // build and compile shaders - // ------------------------- - self.shader = shader::Shader::from_file( - "resources/shaders/model_loading.vs", - "resources/shaders/model_loading.fs", - ); - - // load models - // ----------- - for _ in 0..10 { - self.models.push(model::Model::new("resources/objects/nanosuit/nanosuit.obj")); - } - }; // render loop // ----------- while !self.window.should_close() { @@ -71,29 +55,6 @@ impl Engine { gl::ClearColor(self.bg_info.r, self.bg_info.g, self.bg_info.b, 1.0); gl::Clear(gl::COLOR_BUFFER_BIT | gl::DEPTH_BUFFER_BIT); - // don't forget to enable shader before setting uniforms - self.shader.useProgram(); - - // view/projection transformations - let projection: Matrix4 = perspective( - Deg(self.camera.Zoom), - self.window_size.0 / self.window_size.1, - 0.1, - 100.0, - ); - let view = self.camera.get_view_matrix(); - self.shader.setMat4(c_str!("projection"), &projection); - self.shader.setMat4(c_str!("view"), &view); - - let mut i = 0; - for m in &self.models { - // render the loaded model - let mut model = Matrix4::::from_translation(vec3(0.0, -1.75, -1.25 * (i as f32))); // translate it down so it's at the center of the scene - model = model * Matrix4::from_scale(0.2); // it's a bit too big for our scene, so scale it down - self.shader.setMat4(c_str!("model"), &model); - m.Draw(&self.shader); - i = i + 1; - } self.client.draw(); } @@ -142,24 +103,7 @@ impl Engine { if self.window.get_key(Key::Escape) == Action::Press { self.window.set_should_close(true) } - if self.window.get_key(Key::W) == Action::Press { - self.camera - .process_keyboard(Camera_Movement::FORWARD, delta_time); - } - if self.window.get_key(Key::S) == Action::Press { - self.camera - .process_keyboard(Camera_Movement::BACKWARD, delta_time); - } - if self.window.get_key(Key::A) == Action::Press { - self.camera - .process_keyboard(Camera_Movement::LEFT, delta_time); - } - if self.window.get_key(Key::D) == Action::Press { - self.camera - .process_keyboard(Camera_Movement::RIGHT, delta_time); - } - self.camera - .enable_mouse_movement(self.window.get_key(Key::LeftControl) != Action::Press); + self.client.process_input(&self.window,delta_time); } pub fn process_events( @@ -195,13 +139,13 @@ impl Engine { *last_x = xpos; *last_y = ypos; - self.camera.process_mouse_movement(xoffset, yoffset, true); + self.client.on_mouse_move(xoffset, yoffset); } glfw::WindowEvent::Scroll(_xoffset, yoffset) => { if skip_input { return; } - self.camera.process_mouse_scroll(yoffset as f32); + self.client.on_mouse_scroll(yoffset as f32); } _ => {} } @@ -262,8 +206,6 @@ impl Default for Engine { position: Point3::new(0.0, 0.0, 3.0), ..Camera::default() }, - shader: shader::Shader::default(), - models: vec![], client: Box::new(ExampleClient::create()), } }