Handle input in client

This commit is contained in:
Piotr 2020-09-23 22:15:01 +02:00
parent 3cea406fd9
commit a708e5a1c1
3 changed files with 38 additions and 61 deletions

View File

@ -3,6 +3,7 @@ use crate::gaia::camera::*;
use crate::gaia::consts; use crate::gaia::consts;
use crate::gaia::*; use crate::gaia::*;
#[macro_use] #[macro_use]
use imgui_glfw_rs::glfw;
use crate::gaia::macros; use crate::gaia::macros;
use crate::gaia::engine::Engine; use crate::gaia::engine::Engine;
use crate::gaia::client::Client; use crate::gaia::client::Client;
@ -58,4 +59,34 @@ impl Client for ExampleClient {
fn update(&mut self, engine: &mut Engine) { 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);
}
} }

View File

@ -1,7 +1,11 @@
use crate::gaia::engine::Engine; use crate::gaia::engine::Engine;
use imgui_glfw_rs::glfw;
pub trait Client { pub trait Client {
fn update(&mut self, engine: &mut Engine); 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<T>(&mut self, engine: &mut Engine<T>) where T: Client; // fn draw<T>(&mut self, engine: &mut Engine<T>) where T: Client;
unsafe fn draw(&mut self); unsafe fn draw(&mut self);
} }

View File

@ -20,8 +20,6 @@ pub struct Engine {
pub glfw: imgui_glfw_rs::glfw::Glfw, pub glfw: imgui_glfw_rs::glfw::Glfw,
pub imgui: imgui::Context, pub imgui: imgui::Context,
pub imgui_glfw: ImguiGLFW, pub imgui_glfw: ImguiGLFW,
pub shader: shader::Shader,
pub models: Vec<model::Model>,
pub client: Box<dyn Client>, pub client: Box<dyn Client>,
} }
@ -34,20 +32,6 @@ impl Engine {
// timing // timing
let mut delta_time: f32; // time between current frame and last frame let mut delta_time: f32; // time between current frame and last frame
let mut last_frame: f32 = 0.0; 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 // render loop
// ----------- // -----------
while !self.window.should_close() { 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::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); 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<f32> = 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::<f32>::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(); self.client.draw();
} }
@ -142,24 +103,7 @@ impl Engine {
if self.window.get_key(Key::Escape) == Action::Press { if self.window.get_key(Key::Escape) == Action::Press {
self.window.set_should_close(true) self.window.set_should_close(true)
} }
if self.window.get_key(Key::W) == Action::Press { self.client.process_input(&self.window,delta_time);
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);
} }
pub fn process_events( pub fn process_events(
@ -195,13 +139,13 @@ impl Engine {
*last_x = xpos; *last_x = xpos;
*last_y = ypos; *last_y = ypos;
self.camera.process_mouse_movement(xoffset, yoffset, true); self.client.on_mouse_move(xoffset, yoffset);
} }
glfw::WindowEvent::Scroll(_xoffset, yoffset) => { glfw::WindowEvent::Scroll(_xoffset, yoffset) => {
if skip_input { if skip_input {
return; 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), position: Point3::new(0.0, 0.0, 3.0),
..Camera::default() ..Camera::default()
}, },
shader: shader::Shader::default(),
models: vec![],
client: Box::new(ExampleClient::create()), client: Box::new(ExampleClient::create()),
} }
} }