From 18d231f4cf3b00f7de2b07a8d90a48bab3e5f65a Mon Sep 17 00:00:00 2001 From: Dane Johnson Date: Tue, 12 Apr 2022 14:31:15 -0500 Subject: [PATCH] Some work on the "Spatial" trait --- src/gameobject.rs | 4 +++- src/main.rs | 36 +++++++++++++++++++----------------- src/model.rs | 16 +++++++++++++++- 3 files changed, 37 insertions(+), 19 deletions(-) diff --git a/src/gameobject.rs b/src/gameobject.rs index 5f64487..a671f42 100644 --- a/src/gameobject.rs +++ b/src/gameobject.rs @@ -1,5 +1,6 @@ use nalgebra_glm as glm; +#[derive(Clone, Copy)] pub struct Transform { pub position: glm::Vec3, pub rotation: glm::Vec3, @@ -17,5 +18,6 @@ impl Default for Transform { } pub trait Spatial { - fn get_transform() -> Transform; + fn get_transform(&self) -> Transform; + fn set_transform(&mut self, transform: Transform); } diff --git a/src/main.rs b/src/main.rs index 9486ecd..7cd2420 100644 --- a/src/main.rs +++ b/src/main.rs @@ -11,6 +11,8 @@ use model::Model; use scripting::{ ScriptLang, Lua }; +use gameobject::Spatial; + fn main() { use glium::{glutin, Surface}; @@ -28,14 +30,13 @@ fn main() { let mut device_manager = crate::device::DeviceManager::new(); - struct Cube { - t: gameobject::Transform, - } - - let mut cube = Cube { t: Default::default() }; - - let model = Model::new(&display, "Box.glb"); - + let mut cube = Model::new(&display, "Box.glb"); + cube.set_transform(gameobject::Transform { + position: glm::vec3(0.0, 0.0, -10.0), + rotation: glm::vec3(0.0, 15.0_f32.to_radians(), 0.0), + scale: glm::vec3(1.0, 1.0, 1.0), + }); + let mut script_lang = Lua::new(); script_lang.init(); @@ -58,24 +59,25 @@ fn main() { let now = std::time::Instant::now(); let delta = (now - last_draw).as_secs_f32(); last_draw = now; - + + let mut transform = cube.get_transform(); if device_manager.is_pressed(&device::Key::W) { - cube.t.position.z += 1.0 * delta; + transform.position.z += 1.0 * delta; } else if device_manager.is_pressed(&device::Key::S) { - cube.t.position.z -= 1.0 * delta; + transform.position.z -= 1.0 * delta; } if device_manager.is_pressed(&device::Key::A) { - cube.t.rotation.x += 1.0 * delta; + transform.rotation.x += 1.0 * delta; } else if device_manager.is_pressed(&device::Key::D) { - cube.t.rotation.x -= 1.0 * delta; + transform.rotation.x -= 1.0 * delta; } + cube.set_transform(transform); script_lang.update(delta); let mvp = glm::perspective::(4.0/3.0, 45.0_f32.to_radians(), 0.2, 100.0); - let mvp = glm::translate::(&mvp, &(glm::vec3(0.0, 0.0, -10.0) + cube.t.position)); - let mvp = glm::rotate::(&mvp, 15.0_f32.to_radians(), &glm::vec3(1.0, 0.0, 0.0)); - let mvp = glm::rotate_x(&mvp, cube.t.rotation.x); + let mvp = glm::translate::(&mvp, &cube.get_transform().position); + let mvp = glm::rotate_x(&mvp, cube.get_transform().rotation.x); let uniforms = uniform! { MVP: *mvp.as_ref(), }; @@ -91,7 +93,7 @@ fn main() { .. Default::default() }; target.clear_color_and_depth((0.0, 0.0, 0.0, 1.0), 1.0); - model.draw(&mut target, &program, &uniforms, ¶ms); + cube.draw(&mut target, &program, &uniforms, ¶ms); target.finish().unwrap(); } _ => (), diff --git a/src/model.rs b/src/model.rs index 40b0d00..002a74c 100644 --- a/src/model.rs +++ b/src/model.rs @@ -1,5 +1,7 @@ extern crate gltf; +use crate::gameobject::*; + use glium::{ VertexBuffer, IndexBuffer }; #[derive(Copy, Clone)] @@ -13,6 +15,7 @@ implement_vertex!(Vertex, position, normal); pub struct Model { vb: VertexBuffer, ib: IndexBuffer, + transform: Transform, } impl Model { @@ -32,8 +35,10 @@ impl Model { let indices: Vec = reader.read_indices().unwrap().into_u32().collect(); let ib = IndexBuffer::new(display, glium::index::PrimitiveType::TrianglesList, &indices).unwrap(); + + let transform = Default::default(); - Model { vb, ib } + Model { vb, ib, transform } } pub fn draw(&self, @@ -45,3 +50,12 @@ impl Model { target.draw(&self.vb, &self.ib, program, uniforms, params).unwrap() } } + +impl Spatial for Model { + fn get_transform(&self) -> Transform { + self.transform + } + fn set_transform(&mut self, transform: Transform) { + self.transform = transform; + } +}