Some work on the "Spatial" trait
This commit is contained in:
parent
6a33b25d81
commit
18d231f4cf
@ -1,5 +1,6 @@
|
|||||||
use nalgebra_glm as glm;
|
use nalgebra_glm as glm;
|
||||||
|
|
||||||
|
#[derive(Clone, Copy)]
|
||||||
pub struct Transform {
|
pub struct Transform {
|
||||||
pub position: glm::Vec3,
|
pub position: glm::Vec3,
|
||||||
pub rotation: glm::Vec3,
|
pub rotation: glm::Vec3,
|
||||||
@ -17,5 +18,6 @@ impl Default for Transform {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub trait Spatial {
|
pub trait Spatial {
|
||||||
fn get_transform() -> Transform;
|
fn get_transform(&self) -> Transform;
|
||||||
|
fn set_transform(&mut self, transform: Transform);
|
||||||
}
|
}
|
||||||
|
32
src/main.rs
32
src/main.rs
@ -11,6 +11,8 @@ use model::Model;
|
|||||||
|
|
||||||
use scripting::{ ScriptLang, Lua };
|
use scripting::{ ScriptLang, Lua };
|
||||||
|
|
||||||
|
use gameobject::Spatial;
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
use glium::{glutin, Surface};
|
use glium::{glutin, Surface};
|
||||||
|
|
||||||
@ -28,13 +30,12 @@ fn main() {
|
|||||||
|
|
||||||
let mut device_manager = crate::device::DeviceManager::new();
|
let mut device_manager = crate::device::DeviceManager::new();
|
||||||
|
|
||||||
struct Cube {
|
let mut cube = Model::new(&display, "Box.glb");
|
||||||
t: gameobject::Transform,
|
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),
|
||||||
let mut cube = Cube { t: Default::default() };
|
scale: glm::vec3(1.0, 1.0, 1.0),
|
||||||
|
});
|
||||||
let model = Model::new(&display, "Box.glb");
|
|
||||||
|
|
||||||
let mut script_lang = Lua::new();
|
let mut script_lang = Lua::new();
|
||||||
script_lang.init();
|
script_lang.init();
|
||||||
@ -59,23 +60,24 @@ fn main() {
|
|||||||
let delta = (now - last_draw).as_secs_f32();
|
let delta = (now - last_draw).as_secs_f32();
|
||||||
last_draw = now;
|
last_draw = now;
|
||||||
|
|
||||||
|
let mut transform = cube.get_transform();
|
||||||
if device_manager.is_pressed(&device::Key::W) {
|
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) {
|
} 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) {
|
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) {
|
} 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);
|
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) + cube.t.position));
|
let mvp = glm::translate::<f32>(&mvp, &cube.get_transform().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.get_transform().rotation.x);
|
||||||
let mvp = glm::rotate_x(&mvp, cube.t.rotation.x);
|
|
||||||
let uniforms = uniform! {
|
let uniforms = uniform! {
|
||||||
MVP: *mvp.as_ref(),
|
MVP: *mvp.as_ref(),
|
||||||
};
|
};
|
||||||
@ -91,7 +93,7 @@ fn main() {
|
|||||||
.. Default::default()
|
.. Default::default()
|
||||||
};
|
};
|
||||||
target.clear_color_and_depth((0.0, 0.0, 0.0, 1.0), 1.0);
|
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();
|
target.finish().unwrap();
|
||||||
}
|
}
|
||||||
_ => (),
|
_ => (),
|
||||||
|
16
src/model.rs
16
src/model.rs
@ -1,5 +1,7 @@
|
|||||||
extern crate gltf;
|
extern crate gltf;
|
||||||
|
|
||||||
|
use crate::gameobject::*;
|
||||||
|
|
||||||
use glium::{ VertexBuffer, IndexBuffer };
|
use glium::{ VertexBuffer, IndexBuffer };
|
||||||
|
|
||||||
#[derive(Copy, Clone)]
|
#[derive(Copy, Clone)]
|
||||||
@ -13,6 +15,7 @@ implement_vertex!(Vertex, position, normal);
|
|||||||
pub struct Model {
|
pub struct Model {
|
||||||
vb: VertexBuffer<Vertex>,
|
vb: VertexBuffer<Vertex>,
|
||||||
ib: IndexBuffer<u32>,
|
ib: IndexBuffer<u32>,
|
||||||
|
transform: Transform,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Model {
|
impl Model {
|
||||||
@ -33,7 +36,9 @@ impl Model {
|
|||||||
let indices: Vec<u32> = reader.read_indices().unwrap().into_u32().collect();
|
let indices: Vec<u32> = reader.read_indices().unwrap().into_u32().collect();
|
||||||
let ib = IndexBuffer::new(display, glium::index::PrimitiveType::TrianglesList, &indices).unwrap();
|
let ib = IndexBuffer::new(display, glium::index::PrimitiveType::TrianglesList, &indices).unwrap();
|
||||||
|
|
||||||
Model { vb, ib }
|
let transform = Default::default();
|
||||||
|
|
||||||
|
Model { vb, ib, transform }
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn draw(&self,
|
pub fn draw(&self,
|
||||||
@ -45,3 +50,12 @@ impl Model {
|
|||||||
target.draw(&self.vb, &self.ib, program, uniforms, params).unwrap()
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user