Components
This commit is contained in:
2
.idea/runConfigurations/debug.xml
generated
2
.idea/runConfigurations/debug.xml
generated
@@ -9,7 +9,7 @@
|
|||||||
<option name="workingDirectory" value="file://$PROJECT_DIR$" />
|
<option name="workingDirectory" value="file://$PROJECT_DIR$" />
|
||||||
<envs />
|
<envs />
|
||||||
<method v="2">
|
<method v="2">
|
||||||
<option name="RunConfigurationTask" enabled="true" run_configuration_name="D:\Code\rust-engine\build\quick-clean.ps1" run_configuration_type="PowerShellRunType" />
|
<option name="RunConfigurationTask" enabled="true" />
|
||||||
<option name="CARGO.BUILD_TASK_PROVIDER" enabled="true" />
|
<option name="CARGO.BUILD_TASK_PROVIDER" enabled="true" />
|
||||||
</method>
|
</method>
|
||||||
</configuration>
|
</configuration>
|
||||||
|
|||||||
@@ -1,51 +1,57 @@
|
|||||||
use cgmath::{Matrix4, SquareMatrix, Vector3, vec3};
|
use cgmath::{Matrix4, Vector3, Quaternion, Euler, Deg};
|
||||||
use crate::vulkan::{MeshHandle, VulkanRenderer};
|
use crate::vulkan::{MeshHandle, VulkanRenderer};
|
||||||
use crate::input::InputState;
|
use crate::input::InputState;
|
||||||
|
|
||||||
|
#[derive(Debug, Clone)]
|
||||||
pub struct GameObject {
|
pub struct GameObject {
|
||||||
pub mesh_index: usize,
|
pub mesh_index: usize,
|
||||||
pub texture_index: usize,
|
pub texture_index: usize,
|
||||||
pub model_matrix: Matrix4<f32>,
|
pub position: Vector3<f32>,
|
||||||
|
pub rotation: Quaternion<f32>,
|
||||||
|
pub scale: Vector3<f32>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl GameObject {
|
impl GameObject {
|
||||||
pub fn new(mesh: MeshHandle, texture_index: usize) -> GameObject {
|
pub fn new(mesh: MeshHandle, texture_index: usize) -> GameObject {
|
||||||
GameObject { mesh_index: mesh, texture_index, model_matrix: Matrix4::identity() }
|
GameObject { mesh_index: mesh, texture_index, position: Vector3::new(0.0, 0.0, 0.0),
|
||||||
|
rotation: Quaternion::new(1.0, 0.0, 0.0, 0.0), scale: Vector3::new(1.0, 1.0, 1.0) }
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn set_position(&mut self, pos: (f32, f32, f32)) {
|
pub fn set_position(&mut self, x: f32, y: f32, z: f32) {
|
||||||
self.model_matrix.w.x = pos.0;
|
self.position.x = x;
|
||||||
self.model_matrix.w.y = pos.1;
|
self.position.y = y;
|
||||||
self.model_matrix.w.z = pos.2;
|
self.position.z = z;
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn _get_position(&self) -> Vector3<f32> {
|
pub fn set_scale(&mut self, x: f32, y: f32, z: f32) {
|
||||||
vec3(self.model_matrix.w.x, self.model_matrix.w.y, self.model_matrix.w.z)
|
self.scale.x = x;
|
||||||
|
self.scale.y = y;
|
||||||
|
self.scale.z = z;
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn set_scale(&mut self, scale: (f32, f32, f32)) {
|
pub fn set_rotation(&mut self, euler_x: f32, euler_y: f32, euler_z: f32) {
|
||||||
self.model_matrix.x.x = scale.0;
|
self.rotation = Quaternion::from(Euler::new(Deg(euler_x), Deg(euler_y), Deg(euler_z)));
|
||||||
self.model_matrix.y.y = scale.1;
|
|
||||||
self.model_matrix.z.z = scale.2;
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn _get_scale(&mut self) -> Vector3<f32> {
|
|
||||||
vec3(self.model_matrix.x.x, self.model_matrix.y.y, self.model_matrix.z.z)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn translate(&mut self, x: f32, y: f32, z: f32) {
|
pub fn translate(&mut self, x: f32, y: f32, z: f32) {
|
||||||
self.model_matrix.w.x += x;
|
self.position.x += x;
|
||||||
self.model_matrix.w.y += y;
|
self.position.y += y;
|
||||||
self.model_matrix.w.z += z;
|
self.position.z += z;
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn update(&mut self, delta_time: f32, input: &InputState) {
|
pub fn rotate(&mut self, x: f32, y: f32, z: f32) {
|
||||||
if input.button_down("test") {
|
self.rotation = self.rotation * Quaternion::from(Euler::new(Deg(x), Deg(y), Deg(z)));
|
||||||
self.translate(delta_time as f32, 0.0, 0.0);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn get_model_matrix(&self) -> Matrix4<f32> {
|
||||||
|
let translation = Matrix4::from_translation(self.position);
|
||||||
|
let rotation: Matrix4<f32> = self.rotation.into();
|
||||||
|
let scale = Matrix4::from_nonuniform_scale(self.scale.x, self.scale.y, self.scale.z);
|
||||||
|
translation * rotation * scale
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone)]
|
||||||
pub struct GameObjectHandle {
|
pub struct GameObjectHandle {
|
||||||
pub object_index: usize
|
pub object_index: usize
|
||||||
}
|
}
|
||||||
@@ -55,3 +61,27 @@ impl GameObjectHandle {
|
|||||||
renderer.game_data.game_objects.get_mut(self.object_index)
|
renderer.game_data.game_objects.get_mut(self.object_index)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub trait Component {
|
||||||
|
fn update(&mut self, delta_time: f32, input: &InputState, renderer: &mut VulkanRenderer);
|
||||||
|
fn add_game_object(&mut self, handle: GameObjectHandle);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub struct TestComponent {
|
||||||
|
pub game_objects: Vec<GameObjectHandle>
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Component for TestComponent {
|
||||||
|
fn update(&mut self, delta_time: f32, input: &InputState, renderer: &mut VulkanRenderer) {
|
||||||
|
self.game_objects.iter_mut().for_each(|handle| {
|
||||||
|
if input.button_down("test") {
|
||||||
|
handle.get_game_object(renderer).unwrap().rotate(90.0 * delta_time, 90.0 * delta_time, 90.0 * delta_time);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
fn add_game_object(&mut self, handle: GameObjectHandle) {
|
||||||
|
self.game_objects.push(handle);
|
||||||
|
}
|
||||||
|
}
|
||||||
32
src/main.rs
32
src/main.rs
@@ -5,7 +5,7 @@ use crate::config::LogConfig;
|
|||||||
use crate::input::InputState;
|
use crate::input::InputState;
|
||||||
use crate::vulkan::{Game, LinePoint, MeshHandle, VulkanRenderer};
|
use crate::vulkan::{Game, LinePoint, MeshHandle, VulkanRenderer};
|
||||||
use crate::vulkan::vs::ty::ObjectUniformData;
|
use crate::vulkan::vs::ty::ObjectUniformData;
|
||||||
use crate::gameobject::{GameObject, GameObjectHandle};
|
use crate::gameobject::{GameObject, GameObjectHandle, Component, TestComponent};
|
||||||
|
|
||||||
mod vulkan;
|
mod vulkan;
|
||||||
mod input;
|
mod input;
|
||||||
@@ -22,6 +22,7 @@ struct TestGame {
|
|||||||
log_config: LogConfig,
|
log_config: LogConfig,
|
||||||
texture_index_counter: usize,
|
texture_index_counter: usize,
|
||||||
last_time: f32,
|
last_time: f32,
|
||||||
|
components: Vec<Box<dyn Component>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Game for TestGame {
|
impl Game for TestGame {
|
||||||
@@ -35,9 +36,9 @@ impl Game for TestGame {
|
|||||||
let frame_time = time - self.last_time;
|
let frame_time = time - self.last_time;
|
||||||
|
|
||||||
let input = &self.input;
|
let input = &self.input;
|
||||||
let objects = &mut self.game_objects;
|
let components = &mut self.components;
|
||||||
objects.iter_mut().for_each(|game_object| {
|
components.iter_mut().for_each(|component| {
|
||||||
game_object.get_game_object(renderer).unwrap().update(frame_time, &input);
|
component.update(frame_time, &input, renderer);
|
||||||
});
|
});
|
||||||
|
|
||||||
// User interaction
|
// User interaction
|
||||||
@@ -45,6 +46,10 @@ impl Game for TestGame {
|
|||||||
renderer.game_data.shutdown = true;
|
renderer.game_data.shutdown = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if self.input.button_just_released("test") {
|
||||||
|
println!("{:?}", self.game_objects[0].get_game_object(renderer).unwrap().position);
|
||||||
|
}
|
||||||
|
|
||||||
if self.input.button_just_pressed("reload_shaders") {
|
if self.input.button_just_pressed("reload_shaders") {
|
||||||
renderer.game_data.recreate_pipeline = true;
|
renderer.game_data.recreate_pipeline = true;
|
||||||
}
|
}
|
||||||
@@ -54,8 +59,7 @@ impl Game for TestGame {
|
|||||||
}
|
}
|
||||||
|
|
||||||
let light_pos = vec3(f32::sin(time) * 2.0 + 2.0, f32::cos(time) * 2.0 + 2.0, 2.0);
|
let light_pos = vec3(f32::sin(time) * 2.0 + 2.0, f32::cos(time) * 2.0 + 2.0, 2.0);
|
||||||
self.game_objects[2].get_game_object(renderer).unwrap().set_position(light_pos.into());
|
self.game_objects[2].get_game_object(renderer).unwrap().position = light_pos;
|
||||||
|
|
||||||
|
|
||||||
self.cam_rotation = self.cam_rotation * Quaternion::from_angle_y(Deg(self.input.get_axis("look_horizontal") * 0.05));
|
self.cam_rotation = self.cam_rotation * Quaternion::from_angle_y(Deg(self.input.get_axis("look_horizontal") * 0.05));
|
||||||
self.cam_rotation = Quaternion::from_angle_x(Deg(self.input.get_axis("look_vertical") * 0.05)) * self.cam_rotation;
|
self.cam_rotation = Quaternion::from_angle_x(Deg(self.input.get_axis("look_vertical") * 0.05)) * self.cam_rotation;
|
||||||
@@ -100,15 +104,22 @@ fn _matrix_vector_mul(matrix: &Matrix4<f32>, vector: &Vector3<f32>) -> Vector3<f
|
|||||||
|
|
||||||
impl TestGame {
|
impl TestGame {
|
||||||
fn game_start(self: &mut Self, renderer: &mut VulkanRenderer) {
|
fn game_start(self: &mut Self, renderer: &mut VulkanRenderer) {
|
||||||
|
let mut test_comp = TestComponent { game_objects: vec![] };
|
||||||
|
|
||||||
let cube_meshes = self.load_gltf(renderer, "models/box.gltf.glb");
|
let cube_meshes = self.load_gltf(renderer, "models/box.gltf.glb");
|
||||||
let sphere_meshes = self.load_gltf(renderer, "models/sphere.glb");
|
let sphere_meshes = self.load_gltf(renderer, "models/sphere.glb");
|
||||||
|
|
||||||
let cube = self.add_game_object(renderer, *cube_meshes.first().unwrap(), 0);
|
let cube = self.add_game_object(renderer, *cube_meshes.first().unwrap(), 0);
|
||||||
cube.get_game_object(renderer).unwrap().set_position((3.0, 4.0, 5.0));
|
cube.get_game_object(renderer).unwrap().position = vec3(3.0, 4.0, 5.0);
|
||||||
|
test_comp.game_objects.push(cube.clone());
|
||||||
|
|
||||||
let sphere = self.add_game_object(renderer, *sphere_meshes.first().unwrap(), 0);
|
let sphere = self.add_game_object(renderer, *sphere_meshes.first().unwrap(), 0);
|
||||||
sphere.get_game_object(renderer).unwrap().set_position((0.0, 0.0, 0.0));
|
sphere.get_game_object(renderer).unwrap().position = vec3(0.0, 0.0, 0.0);
|
||||||
|
|
||||||
let light_sphere = self.add_game_object(renderer, *sphere_meshes.first().unwrap(), 0);
|
let light_sphere = self.add_game_object(renderer, *sphere_meshes.first().unwrap(), 0);
|
||||||
light_sphere.get_game_object(renderer).unwrap().set_scale((0.1, 0.1, 0.1));
|
light_sphere.get_game_object(renderer).unwrap().scale = vec3(0.1, 0.1, 0.1);
|
||||||
|
|
||||||
|
self.components.push(Box::new(test_comp));
|
||||||
println!("Game loaded!");
|
println!("Game loaded!");
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -148,7 +159,8 @@ fn main() {
|
|||||||
game_objects: vec![],
|
game_objects: vec![],
|
||||||
log_config,
|
log_config,
|
||||||
texture_index_counter: 1,
|
texture_index_counter: 1,
|
||||||
last_time: 0.0
|
last_time: 0.0,
|
||||||
|
components: vec![],
|
||||||
};
|
};
|
||||||
|
|
||||||
let line_count = 30;
|
let line_count = 30;
|
||||||
|
|||||||
@@ -330,7 +330,7 @@ impl VulkanRenderer {
|
|||||||
for i in 0..self.game_data.game_objects.len() {
|
for i in 0..self.game_data.game_objects.len() {
|
||||||
let game_object = &self.game_data.game_objects[i];
|
let game_object = &self.game_data.game_objects[i];
|
||||||
let mesh = &self.game_data.meshes[game_object.mesh_index];
|
let mesh = &self.game_data.meshes[game_object.mesh_index];
|
||||||
self.game_data.push_constants.model = game_object.model_matrix.into();
|
self.game_data.push_constants.model = game_object.get_model_matrix().into();
|
||||||
|
|
||||||
builder.draw_indexed(
|
builder.draw_indexed(
|
||||||
self.pipeline.clone(),
|
self.pipeline.clone(),
|
||||||
|
|||||||
Reference in New Issue
Block a user