Node tree
This commit is contained in:
parent
18d231f4cf
commit
4febab44d9
@ -1,5 +1,34 @@
|
|||||||
use nalgebra_glm as glm;
|
use nalgebra_glm as glm;
|
||||||
|
|
||||||
|
use crate::model::Model;
|
||||||
|
|
||||||
|
pub struct Node {
|
||||||
|
pub name: String,
|
||||||
|
pub gameobject: GameObject,
|
||||||
|
pub children: Vec<Node>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Node {
|
||||||
|
pub fn new(name: String, gameobject: GameObject) -> Self {
|
||||||
|
Node {
|
||||||
|
name,
|
||||||
|
gameobject,
|
||||||
|
children: Vec::new(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
pub fn add_child(&mut self, child: Node) {
|
||||||
|
self.children.push(child)
|
||||||
|
}
|
||||||
|
pub fn find_node(&mut self, name: &str) -> Option<&mut Node> {
|
||||||
|
self.children.iter_mut().find(|node| node.name == name)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub enum GameObject {
|
||||||
|
Null,
|
||||||
|
Mesh(Box<Mesh>),
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Clone, Copy)]
|
#[derive(Clone, Copy)]
|
||||||
pub struct Transform {
|
pub struct Transform {
|
||||||
pub position: glm::Vec3,
|
pub position: glm::Vec3,
|
||||||
@ -16,8 +45,31 @@ impl Default for Transform {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub trait Spatial {
|
pub trait Spatial {
|
||||||
fn get_transform(&self) -> Transform;
|
fn get_transform(&self) -> Transform;
|
||||||
fn set_transform(&mut self, transform: Transform);
|
fn set_transform(&mut self, transform: Transform);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub struct Mesh {
|
||||||
|
pub model: Model,
|
||||||
|
transform: Transform,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Mesh {
|
||||||
|
pub fn new(model: Model) -> Self {
|
||||||
|
Mesh {
|
||||||
|
model,
|
||||||
|
transform: Default::default(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Spatial for Mesh {
|
||||||
|
fn get_transform(&self) -> Transform {
|
||||||
|
self.transform
|
||||||
|
}
|
||||||
|
fn set_transform(&mut self, transform: Transform) {
|
||||||
|
self.transform = transform;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
54
src/main.rs
54
src/main.rs
@ -11,7 +11,7 @@ use model::Model;
|
|||||||
|
|
||||||
use scripting::{ ScriptLang, Lua };
|
use scripting::{ ScriptLang, Lua };
|
||||||
|
|
||||||
use gameobject::Spatial;
|
use gameobject::{ Spatial, Mesh, Node, GameObject };
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
use glium::{glutin, Surface};
|
use glium::{glutin, Surface};
|
||||||
@ -30,12 +30,15 @@ fn main() {
|
|||||||
|
|
||||||
let mut device_manager = crate::device::DeviceManager::new();
|
let mut device_manager = crate::device::DeviceManager::new();
|
||||||
|
|
||||||
let mut cube = Model::new(&display, "Box.glb");
|
let mut root = Node::new("_root_".to_string(), GameObject::Null);
|
||||||
|
|
||||||
|
let mut cube = Mesh::new(Model::new(&display, "Box.glb"));
|
||||||
cube.set_transform(gameobject::Transform {
|
cube.set_transform(gameobject::Transform {
|
||||||
position: glm::vec3(0.0, 0.0, -10.0),
|
position: glm::vec3(0.0, 0.0, -10.0),
|
||||||
rotation: glm::vec3(0.0, 15.0_f32.to_radians(), 0.0),
|
rotation: glm::vec3(0.0, 15.0_f32.to_radians(), 0.0),
|
||||||
scale: glm::vec3(1.0, 1.0, 1.0),
|
scale: glm::vec3(1.0, 1.0, 1.0),
|
||||||
});
|
});
|
||||||
|
root.add_child(Node::new("cube".to_string(), GameObject::Mesh(Box::new(cube))));
|
||||||
|
|
||||||
let mut script_lang = Lua::new();
|
let mut script_lang = Lua::new();
|
||||||
script_lang.init();
|
script_lang.init();
|
||||||
@ -60,27 +63,7 @@ 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) {
|
|
||||||
transform.position.z += 1.0 * delta;
|
|
||||||
} else if device_manager.is_pressed(&device::Key::S) {
|
|
||||||
transform.position.z -= 1.0 * delta;
|
|
||||||
}
|
|
||||||
if device_manager.is_pressed(&device::Key::A) {
|
|
||||||
transform.rotation.x += 1.0 * delta;
|
|
||||||
} else if device_manager.is_pressed(&device::Key::D) {
|
|
||||||
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::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(),
|
|
||||||
};
|
|
||||||
|
|
||||||
let mut target = display.draw();
|
let mut target = display.draw();
|
||||||
let params = glium::DrawParameters {
|
let params = glium::DrawParameters {
|
||||||
@ -93,7 +76,32 @@ 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);
|
||||||
cube.draw(&mut target, &program, &uniforms, ¶ms);
|
|
||||||
|
let cube: &mut Node = root.find_node("cube").unwrap();
|
||||||
|
if let GameObject::Mesh(cube) = &mut cube.gameobject {
|
||||||
|
let mut transform = cube.get_transform();
|
||||||
|
if device_manager.is_pressed(&device::Key::W) {
|
||||||
|
transform.position.z += 1.0 * delta;
|
||||||
|
} else if device_manager.is_pressed(&device::Key::S) {
|
||||||
|
transform.position.z -= 1.0 * delta;
|
||||||
|
}
|
||||||
|
if device_manager.is_pressed(&device::Key::A) {
|
||||||
|
transform.rotation.x += 1.0 * delta;
|
||||||
|
} else if device_manager.is_pressed(&device::Key::D) {
|
||||||
|
transform.rotation.x -= 1.0 * delta;
|
||||||
|
}
|
||||||
|
cube.set_transform(transform);
|
||||||
|
let mvp = glm::perspective::<f32>(4.0/3.0, 45.0_f32.to_radians(), 0.2, 100.0);
|
||||||
|
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(),
|
||||||
|
};
|
||||||
|
cube.model.draw(&mut target, &program, &uniforms, ¶ms);
|
||||||
|
}
|
||||||
target.finish().unwrap();
|
target.finish().unwrap();
|
||||||
}
|
}
|
||||||
_ => (),
|
_ => (),
|
||||||
|
16
src/model.rs
16
src/model.rs
@ -1,7 +1,5 @@
|
|||||||
extern crate gltf;
|
extern crate gltf;
|
||||||
|
|
||||||
use crate::gameobject::*;
|
|
||||||
|
|
||||||
use glium::{ VertexBuffer, IndexBuffer };
|
use glium::{ VertexBuffer, IndexBuffer };
|
||||||
|
|
||||||
#[derive(Copy, Clone)]
|
#[derive(Copy, Clone)]
|
||||||
@ -15,7 +13,6 @@ 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 {
|
||||||
@ -36,9 +33,7 @@ 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();
|
||||||
|
|
||||||
let transform = Default::default();
|
Model { vb, ib }
|
||||||
|
|
||||||
Model { vb, ib, transform }
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn draw(&self,
|
pub fn draw(&self,
|
||||||
@ -50,12 +45,3 @@ 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