Adding transforms and spatials

This commit is contained in:
Dane Johnson 2022-04-11 19:21:47 -05:00
parent 99f100c812
commit 6a33b25d81
2 changed files with 34 additions and 8 deletions

21
src/gameobject.rs Normal file
View File

@ -0,0 +1,21 @@
use nalgebra_glm as glm;
pub struct Transform {
pub position: glm::Vec3,
pub rotation: glm::Vec3,
pub scale: glm::Vec3,
}
impl Default for Transform {
fn default() -> Self {
Transform {
position: glm::zero(),
rotation: glm::zero(),
scale: glm::vec3(1.0, 1.0, 1.0),
}
}
}
pub trait Spatial {
fn get_transform() -> Transform;
}

View File

@ -1,6 +1,7 @@
mod device; mod device;
mod model; mod model;
mod scripting; mod scripting;
mod gameobject;
#[macro_use] #[macro_use]
extern crate glium; extern crate glium;
@ -27,9 +28,12 @@ fn main() {
let mut device_manager = crate::device::DeviceManager::new(); let mut device_manager = crate::device::DeviceManager::new();
let mut offset = 0.0; struct Cube {
let mut rotate = 0.0; t: gameobject::Transform,
}
let mut cube = Cube { t: Default::default() };
let model = Model::new(&display, "Box.glb"); let model = Model::new(&display, "Box.glb");
let mut script_lang = Lua::new(); let mut script_lang = Lua::new();
@ -56,21 +60,22 @@ fn main() {
last_draw = now; last_draw = now;
if device_manager.is_pressed(&device::Key::W) { if device_manager.is_pressed(&device::Key::W) {
offset += 1.0 * delta; cube.t.position.z += 1.0 * delta;
} else if device_manager.is_pressed(&device::Key::S) { } else if device_manager.is_pressed(&device::Key::S) {
offset -= 1.0 * delta; cube.t.position.z -= 1.0 * delta;
} }
if device_manager.is_pressed(&device::Key::A) { if device_manager.is_pressed(&device::Key::A) {
rotate += 1.0 * delta; cube.t.rotation.x += 1.0 * delta;
} else if device_manager.is_pressed(&device::Key::D) { } else if device_manager.is_pressed(&device::Key::D) {
rotate -= 1.0 * delta; cube.t.rotation.x -= 1.0 * delta;
} }
script_lang.update(delta); script_lang.update(delta);
let mvp = glm::perspective::<f32>(4.0/3.0, 45.0_f32.to_radians(), 0.2, 100.0); let mvp = glm::perspective::<f32>(4.0/3.0, 45.0_f32.to_radians(), 0.2, 100.0);
let mvp = glm::translate::<f32>(&mvp, &glm::vec3(0.0, 0.0, -10.0 + offset)); let mvp = glm::translate::<f32>(&mvp, &(glm::vec3(0.0, 0.0, -10.0) + cube.t.position));
let mvp = glm::rotate::<f32>(&mvp, 15.0_f32.to_radians() + rotate, &glm::vec3(1.0, 0.0, 0.0)); let mvp = glm::rotate::<f32>(&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 uniforms = uniform! { let uniforms = uniform! {
MVP: *mvp.as_ref(), MVP: *mvp.as_ref(),
}; };