diff --git a/src/example_client.rs b/src/example_client.rs index 2e5a511..b5de798 100644 --- a/src/example_client.rs +++ b/src/example_client.rs @@ -1,29 +1,30 @@ use crate::gaia::camera::*; -use crate::gaia::consts; +use crate::gaia::{consts,macros}; use crate::gaia::*; -#[macro_use] use imgui_glfw_rs::glfw; -use crate::gaia::macros; +use crate::gaia::components::{Transform,ModelComponent}; 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, + model: ModelComponent, camera: Camera, shader: shader::Shader, } impl ExampleClient { pub fn create() -> ExampleClient { - ExampleClient { + let mut t = Transform::default(); + t.position = vec3(3.0, -1.75, -1.25); + t.scale = vec3(0.2,0.1,0.2); + let model = ModelComponent { + transform: t, model: model::Model::new("resources/objects/nanosuit/nanosuit.obj"), + }; + ExampleClient { + model: model, camera: Camera { position: Point3::new(0.0, 0.0, 3.0), ..Camera::default() @@ -50,11 +51,8 @@ impl Client for ExampleClient { 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); + + self.model.draw(&self.shader); } fn update(&mut self, engine: &mut Engine) { diff --git a/src/gaia/components.rs b/src/gaia/components.rs new file mode 100644 index 0000000..dc1968a --- /dev/null +++ b/src/gaia/components.rs @@ -0,0 +1,44 @@ +use crate::gaia::*; +use crate::gaia::macros; +use cgmath::{perspective, vec3, Deg, Matrix4, Point3, Rad, Vector3}; +use crate::gaia::model::Model; +use crate::gaia::shader::Shader; + +pub struct Transform { + pub position: Vector3, + pub scale: Vector3, + pub rotation: Vector3, +} + +impl Default for Transform { + fn default() -> Transform { + Transform { + position: vec3(0.0,0.0,0.0), + scale: vec3(1.0,1.0,1.0), + rotation: vec3(0.0,0.0,0.0), + } + } +} + +impl Transform { + pub fn get_matrix(&self) -> Matrix4 { + let mut m = Matrix4::::from_translation(self.position); + m = m * Matrix4::from_nonuniform_scale(self.scale.x,self.scale.y,self.scale.z); + + m + } +} + +pub struct ModelComponent { + pub model: Model, + pub transform: Transform, +} + +impl ModelComponent { + pub unsafe fn draw(&mut self, shader: &Shader ) { + let matrix = self.transform.get_matrix(); + + shader.setMat4(c_str!("model"), &matrix); + self.model.Draw(&shader); + } +} \ No newline at end of file diff --git a/src/gaia/engine.rs b/src/gaia/engine.rs index d71ab98..0a9a1e5 100644 --- a/src/gaia/engine.rs +++ b/src/gaia/engine.rs @@ -65,12 +65,13 @@ impl Engine { { use imgui::*; + let fps = 1.0 / delta_time; let mut info = self.bg_info; Window::new(im_str!("Hello world")) .size([300.0, 110.0], Condition::FirstUseEver) .build(&ui, || { ui.text(im_str!("Hello world!")); - ui.text(im_str!("こんにちは世界!")); + ui.text(format!("{:.0}", fps)); ui.text(im_str!("This...is...imgui-rs!")); ui.separator(); ui.text(format!("Mouse position: ({:.1},{:.1})", last_x, last_y)); diff --git a/src/gaia/macros.rs b/src/gaia/macros.rs index 1959e16..6a8ac30 100644 --- a/src/gaia/macros.rs +++ b/src/gaia/macros.rs @@ -1,3 +1,5 @@ +#![macro_use] + /// Macro to get c strings from literals without runtime overhead /// Literal must not contain any interior nul bytes! macro_rules! c_str { diff --git a/src/gaia/mod.rs b/src/gaia/mod.rs index 5422105..b8d7b38 100644 --- a/src/gaia/mod.rs +++ b/src/gaia/mod.rs @@ -1,9 +1,9 @@ +pub mod macros; pub mod camera; +pub mod components; pub mod client; pub mod consts; pub mod utils; -#[macro_use] -pub mod macros; pub mod bg_info; pub mod engine; pub mod mesh; diff --git a/src/main.rs b/src/main.rs index 9546d3a..d564d0f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -4,6 +4,7 @@ extern crate gl; extern crate image; extern crate imgui_glfw_rs; use human_panic::setup_panic; +#[macro_use] mod gaia; mod example_client; use crate::gaia::engine::Engine;