Some work on the "Spatial" trait

This commit is contained in:
Dane Johnson 2022-04-12 14:31:15 -05:00
parent 6a33b25d81
commit 18d231f4cf
3 changed files with 37 additions and 19 deletions

View File

@ -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);
}

View File

@ -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::<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) + cube.t.position));
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 mvp = glm::translate::<f32>(&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, &params);
cube.draw(&mut target, &program, &uniforms, &params);
target.finish().unwrap();
}
_ => (),

View File

@ -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<Vertex>,
ib: IndexBuffer<u32>,
transform: Transform,
}
impl Model {
@ -32,8 +35,10 @@ impl Model {
let indices: Vec<u32> = 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;
}
}