From 3cea406fd92ac6a534ed42a4ceb4e1b79b3ea373 Mon Sep 17 00:00:00 2001 From: Piotr Date: Wed, 23 Sep 2020 16:33:46 +0200 Subject: [PATCH] Start creating client --- src/example_client.rs | 61 +++++++++++++++++++++++++++++++++++++++++++ src/gaia/client.rs | 7 +++++ src/gaia/engine.rs | 13 ++++++--- src/gaia/mod.rs | 1 + src/main.rs | 2 ++ 5 files changed, 80 insertions(+), 4 deletions(-) create mode 100644 src/example_client.rs create mode 100644 src/gaia/client.rs diff --git a/src/example_client.rs b/src/example_client.rs new file mode 100644 index 0000000..649f1be --- /dev/null +++ b/src/example_client.rs @@ -0,0 +1,61 @@ + +use crate::gaia::camera::*; +use crate::gaia::consts; +use crate::gaia::*; +#[macro_use] +use crate::gaia::macros; +use crate::gaia::engine::Engine; +use crate::gaia::client::Client; +use cgmath::{perspective, vec3, Deg, Matrix4, Point3, Rad, Vector3}; +macro_rules! c_str { + ($literal:expr) => { + std::ffi::CStr::from_bytes_with_nul_unchecked(concat!($literal, "\0").as_bytes()) + }; +} + +pub struct ExampleClient { + model: model::Model, + camera: Camera, + shader: shader::Shader, +} + +impl ExampleClient { + pub fn create() -> ExampleClient { + ExampleClient { + model: model::Model::new("resources/objects/nanosuit/nanosuit.obj"), + camera: Camera { + position: Point3::new(0.0, 0.0, 3.0), + ..Camera::default() + }, + shader: shader::Shader::from_file( + "resources/shaders/model_loading.vs", + "resources/shaders/model_loading.fs", + ), + } + } +} + +impl Client for ExampleClient { + unsafe fn draw(&mut self) { + self.shader.useProgram(); + + // view/projection transformations + let projection: Matrix4 = perspective( + Deg(self.camera.Zoom), + consts::SCR_WIDTH as f32 / consts::SCR_HEIGHT as f32, + 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 m = Matrix4::::from_translation(vec3(3.0, -1.75, -1.25)); // translate it down so it's at the center of the scene + m = m * Matrix4::from_scale(0.2); // it's a bit too big for our scene, so scale it down + self.shader.setMat4(c_str!("model"), &m); + self.model.Draw(&self.shader); + } + fn update(&mut self, engine: &mut Engine) { + + } +} \ No newline at end of file diff --git a/src/gaia/client.rs b/src/gaia/client.rs new file mode 100644 index 0000000..e3c92f7 --- /dev/null +++ b/src/gaia/client.rs @@ -0,0 +1,7 @@ +use crate::gaia::engine::Engine; + +pub trait Client { + fn update(&mut self, engine: &mut Engine); + // 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 d9eae62..c9cadeb 100644 --- a/src/gaia/engine.rs +++ b/src/gaia/engine.rs @@ -2,6 +2,8 @@ use crate::gaia::bg_info::BgInfo; use crate::gaia::camera::*; use crate::gaia::consts; use crate::gaia::*; +use crate::example_client::ExampleClient; +use crate::gaia::client::Client; use cgmath::{perspective, vec3, Deg, Matrix4, Point3, Rad, Vector3}; use imgui_glfw_rs::glfw; use imgui_glfw_rs::glfw::{Action, Context, Key}; @@ -20,6 +22,7 @@ pub struct Engine { pub imgui_glfw: ImguiGLFW, pub shader: shader::Shader, pub models: Vec, + pub client: Box, } impl Engine { @@ -91,6 +94,7 @@ impl Engine { m.Draw(&self.shader); i = i + 1; } + self.client.draw(); } // glfw: swap buffers and poll IO events (keys pressed/released, mouse moved etc.) @@ -247,10 +251,6 @@ impl Default for Engine { unsafe{gl::Enable(gl::DEPTH_TEST);} Engine { - camera: Camera { - position: Point3::new(0.0, 0.0, 3.0), - ..Camera::default() - }, bg_info: BgInfo::default(), window: window, window_size: (consts::SCR_WIDTH as f32, consts::SCR_HEIGHT as f32), @@ -258,8 +258,13 @@ impl Default for Engine { glfw: glfw, imgui: imgui, imgui_glfw: imgui_glfw, + camera: Camera { + position: Point3::new(0.0, 0.0, 3.0), + ..Camera::default() + }, shader: shader::Shader::default(), models: vec![], + client: Box::new(ExampleClient::create()), } } } diff --git a/src/gaia/mod.rs b/src/gaia/mod.rs index b26d114..5422105 100644 --- a/src/gaia/mod.rs +++ b/src/gaia/mod.rs @@ -1,4 +1,5 @@ pub mod camera; +pub mod client; pub mod consts; pub mod utils; #[macro_use] diff --git a/src/main.rs b/src/main.rs index 66906db..9546d3a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -5,7 +5,9 @@ extern crate image; extern crate imgui_glfw_rs; use human_panic::setup_panic; mod gaia; +mod example_client; use crate::gaia::engine::Engine; +use crate::example_client::ExampleClient; pub fn main() { setup_panic!();