gameobjects
This commit is contained in:
BIN
models/sphere.blend
Normal file
BIN
models/sphere.blend
Normal file
Binary file not shown.
BIN
models/sphere.glb
Normal file
BIN
models/sphere.glb
Normal file
Binary file not shown.
47
src/gameobject.rs
Normal file
47
src/gameobject.rs
Normal file
@@ -0,0 +1,47 @@
|
|||||||
|
use cgmath::{Matrix4, SquareMatrix, Vector3, vec3};
|
||||||
|
use crate::vulkan::{MeshHandle, VulkanRenderer};
|
||||||
|
use crate::input::InputState;
|
||||||
|
|
||||||
|
pub struct GameObject {
|
||||||
|
pub mesh_index: usize,
|
||||||
|
pub texture_index: usize,
|
||||||
|
pub model_matrix: Matrix4<f32>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl GameObject {
|
||||||
|
pub fn new(mesh: MeshHandle, texture_index: usize) -> GameObject {
|
||||||
|
GameObject { mesh_index: mesh, texture_index, model_matrix: Matrix4::identity() }
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn set_position(&mut self, x: f32, y: f32, z: f32) {
|
||||||
|
self.model_matrix.w.x = x;
|
||||||
|
self.model_matrix.w.y = y;
|
||||||
|
self.model_matrix.w.z = z;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn get_position(&self) -> Vector3<f32> {
|
||||||
|
vec3(self.model_matrix.w.x, self.model_matrix.w.y, self.model_matrix.w.z)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn translate(&mut self, x: f32, y: f32, z: f32) {
|
||||||
|
self.model_matrix.w.x += x;
|
||||||
|
self.model_matrix.w.y += y;
|
||||||
|
self.model_matrix.w.z += z;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn update(&mut self, delta_time: f32, input: &InputState) {
|
||||||
|
if input.button_down("test") {
|
||||||
|
self.translate(delta_time as f32, 0.0, 0.0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct GameObjectHandle {
|
||||||
|
pub object_index: usize
|
||||||
|
}
|
||||||
|
|
||||||
|
impl GameObjectHandle {
|
||||||
|
pub fn get_game_object<'a>(&mut self, renderer: &'a mut VulkanRenderer) -> Option<&'a mut GameObject> {
|
||||||
|
renderer.game_data.game_objects.get_mut(self.object_index)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -194,9 +194,9 @@ impl InputState {
|
|||||||
if self.log_config.input_events {
|
if self.log_config.input_events {
|
||||||
let mods = mods_to_string(&KeyboardModifierState::from_deprecated_state(&input.modifiers));
|
let mods = mods_to_string(&KeyboardModifierState::from_deprecated_state(&input.modifiers));
|
||||||
if mods.len() > 0 {
|
if mods.len() > 0 {
|
||||||
println!("Keyboard {:?} {:?} {:?} + {:?}", device_id, input.state, &mods, input.scancode)
|
println!("Keyboard {:?} {:?} {:?} + {:?}", device_id, input.state, &mods, input.virtual_keycode)
|
||||||
} else {
|
} else {
|
||||||
println!("Keyboard {:?} {:?} {:?}", device_id, input.state, input.scancode)
|
println!("Keyboard {:?} {:?} {:?}", device_id, input.state, input.virtual_keycode)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -352,6 +352,7 @@ impl InputState {
|
|||||||
let input = DigitalInput::Keyboard(KeyboardInput { scan_code, modifiers });
|
let input = DigitalInput::Keyboard(KeyboardInput { scan_code, modifiers });
|
||||||
match state {
|
match state {
|
||||||
ElementState::Pressed => {
|
ElementState::Pressed => {
|
||||||
|
if self.pressed_scan_codes.contains(&scan_code) { return; }
|
||||||
self.input_events.insert(DigitalInputEvent::Pressed(input));
|
self.input_events.insert(DigitalInputEvent::Pressed(input));
|
||||||
self.pressed_scan_codes.insert(scan_code);
|
self.pressed_scan_codes.insert(scan_code);
|
||||||
},
|
},
|
||||||
|
|||||||
65
src/main.rs
65
src/main.rs
@@ -3,21 +3,25 @@ use winit::event::Event;
|
|||||||
|
|
||||||
use crate::config::LogConfig;
|
use crate::config::LogConfig;
|
||||||
use crate::input::InputState;
|
use crate::input::InputState;
|
||||||
use crate::vulkan::{Game, GameObject, GameObjectHandle, 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};
|
||||||
|
|
||||||
mod vulkan;
|
mod vulkan;
|
||||||
mod input;
|
mod input;
|
||||||
mod config;
|
mod config;
|
||||||
mod mesh;
|
mod mesh;
|
||||||
|
mod gameobject;
|
||||||
|
|
||||||
struct TestGame {
|
struct TestGame {
|
||||||
input: InputState,
|
input: InputState,
|
||||||
cam_position: Vector3<f32>,
|
cam_position: Vector3<f32>,
|
||||||
cam_rotation: Quaternion<f32>,
|
cam_rotation: Quaternion<f32>,
|
||||||
test_meshes: Vec<(MeshHandle, usize)>,
|
meshes: Vec<(MeshHandle, usize)>,
|
||||||
cubes: Vec<GameObjectHandle>,
|
game_objects: Vec<GameObjectHandle>,
|
||||||
log_config: LogConfig,
|
log_config: LogConfig,
|
||||||
|
texture_index_counter: usize,
|
||||||
|
last_time: f32,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Game for TestGame {
|
impl Game for TestGame {
|
||||||
@@ -28,6 +32,13 @@ impl Game for TestGame {
|
|||||||
fn update(self: &mut Self, renderer: &mut VulkanRenderer) -> ObjectUniformData {
|
fn update(self: &mut Self, renderer: &mut VulkanRenderer) -> ObjectUniformData {
|
||||||
self.input.frame_start();
|
self.input.frame_start();
|
||||||
let time = (renderer.game_data.start_time.elapsed().unwrap().as_micros() as f64 / 1000000.0) as f32;
|
let time = (renderer.game_data.start_time.elapsed().unwrap().as_micros() as f64 / 1000000.0) as f32;
|
||||||
|
let frame_time = time - self.last_time;
|
||||||
|
|
||||||
|
let input = &self.input;
|
||||||
|
let objects = &mut self.game_objects;
|
||||||
|
objects.iter_mut().for_each(|game_object| {
|
||||||
|
game_object.get_game_object(renderer).unwrap().update(frame_time, &input);
|
||||||
|
});
|
||||||
|
|
||||||
// User interaction
|
// User interaction
|
||||||
if self.input.button_just_released("quit") {
|
if self.input.button_just_released("quit") {
|
||||||
@@ -39,12 +50,7 @@ impl Game for TestGame {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if self.input.button_down("print_framerate") {
|
if self.input.button_down("print_framerate") {
|
||||||
// println!("{:.0} ms / {:.0} FPS", frame_time * 1000.0, 1.0 / frame_time);
|
println!("{:.0} ms / {:.0} FPS", frame_time * 1000.0, 1.0 / frame_time);
|
||||||
}
|
|
||||||
|
|
||||||
if self.input.button_just_pressed("test") {
|
|
||||||
println!("test");
|
|
||||||
self.cubes = self.test_meshes.iter().map(|(mesh, tex_id)| renderer.add_game_object(GameObject::new(*mesh, *tex_id))).collect();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
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));
|
||||||
@@ -69,6 +75,7 @@ impl Game for TestGame {
|
|||||||
renderer.game_data.line_push_constants.view = view.into();
|
renderer.game_data.line_push_constants.view = view.into();
|
||||||
renderer.game_data.line_push_constants.projection = proj.into();
|
renderer.game_data.line_push_constants.projection = proj.into();
|
||||||
|
|
||||||
|
self.last_time = time;
|
||||||
self.input.frame_end();
|
self.input.frame_end();
|
||||||
|
|
||||||
ObjectUniformData {
|
ObjectUniformData {
|
||||||
@@ -81,16 +88,34 @@ impl Game for TestGame {
|
|||||||
|
|
||||||
impl TestGame {
|
impl TestGame {
|
||||||
fn game_start(self: &mut Self, renderer: &mut VulkanRenderer) {
|
fn game_start(self: &mut Self, renderer: &mut VulkanRenderer) {
|
||||||
let (meshes, textures) = mesh::load_mesh("models/box.gltf.glb", self.log_config.mesh_load_info).unwrap();
|
self.load_gltf(renderer, "models/box.gltf.glb");
|
||||||
self.test_meshes = meshes.into_iter().map(|m| {
|
self.load_gltf(renderer, "models/sphere.glb");
|
||||||
let id = match m.texture_index {
|
|
||||||
Some(tex_id) => tex_id + 1,
|
let cube = self.add_game_object(renderer, 0, 0);
|
||||||
|
cube.get_game_object(renderer).unwrap().set_position(0.0, 2.0, 0.0);
|
||||||
|
let sphere = self.add_game_object(renderer, 1, 0);
|
||||||
|
sphere.get_game_object(renderer).unwrap().set_position(2.0, 0.0, 0.0);
|
||||||
|
println!("Game loaded!");
|
||||||
|
}
|
||||||
|
|
||||||
|
fn load_gltf(&mut self, renderer: &mut VulkanRenderer, gltf_path: &str) {
|
||||||
|
let (meshes, textures) = mesh::load_mesh(gltf_path, self.log_config.mesh_load_info).unwrap();
|
||||||
|
for cpu_mesh in meshes.into_iter() {
|
||||||
|
let id = match cpu_mesh.texture_index {
|
||||||
|
Some(tex_id) => tex_id + self.texture_index_counter,
|
||||||
None => 0,
|
None => 0,
|
||||||
};
|
};
|
||||||
(renderer.upload_mesh(m), id)
|
self.texture_index_counter += 1;
|
||||||
}).collect();
|
self.meshes.push((renderer.upload_mesh(cpu_mesh), id));
|
||||||
|
}
|
||||||
textures.iter().for_each(|tex| renderer.upload_texture(tex));
|
textures.iter().for_each(|tex| renderer.upload_texture(tex));
|
||||||
println!("Game loaded!");
|
}
|
||||||
|
|
||||||
|
fn add_game_object(&mut self, renderer: &mut VulkanRenderer, mesh: MeshHandle, texture_index: usize) -> &mut GameObjectHandle {
|
||||||
|
let obj = GameObject::new(mesh, texture_index);
|
||||||
|
let obj_handle = renderer.add_game_object(obj);
|
||||||
|
self.game_objects.push(obj_handle);
|
||||||
|
self.game_objects.last_mut().unwrap()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -101,9 +126,11 @@ fn main() {
|
|||||||
input: InputState::new("config/input.toml", log_config),
|
input: InputState::new("config/input.toml", log_config),
|
||||||
cam_rotation: Quaternion::one(),
|
cam_rotation: Quaternion::one(),
|
||||||
cam_position: Vector3::new(0.0, 3.0, -10.0),
|
cam_position: Vector3::new(0.0, 3.0, -10.0),
|
||||||
test_meshes: vec![],
|
meshes: vec![],
|
||||||
cubes: vec![],
|
game_objects: vec![],
|
||||||
log_config
|
log_config,
|
||||||
|
texture_index_counter: 1,
|
||||||
|
last_time: 0.0
|
||||||
};
|
};
|
||||||
|
|
||||||
let line_count = 30;
|
let line_count = 30;
|
||||||
|
|||||||
@@ -31,6 +31,7 @@ use line_vs::ty::LinePushConstants;
|
|||||||
use vs::ty::PushConstants;
|
use vs::ty::PushConstants;
|
||||||
|
|
||||||
use crate::mesh::CPUMesh;
|
use crate::mesh::CPUMesh;
|
||||||
|
use crate::gameobject::{GameObject, GameObjectHandle};
|
||||||
|
|
||||||
const VALIDATION_LAYERS: &[&str] = &[
|
const VALIDATION_LAYERS: &[&str] = &[
|
||||||
"VK_LAYER_LUNARG_standard_validation"
|
"VK_LAYER_LUNARG_standard_validation"
|
||||||
@@ -62,13 +63,6 @@ pub struct Mesh {
|
|||||||
index_buffer: Arc<CpuAccessibleBuffer<[u32]>>,
|
index_buffer: Arc<CpuAccessibleBuffer<[u32]>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct GameObject {
|
|
||||||
pub mesh_index: usize,
|
|
||||||
pub texture_index: usize,
|
|
||||||
pub model_matrix: Matrix4<f32>,
|
|
||||||
}
|
|
||||||
|
|
||||||
pub(crate) type GameObjectHandle = usize;
|
|
||||||
pub(crate) type MeshHandle = usize;
|
pub(crate) type MeshHandle = usize;
|
||||||
|
|
||||||
pub struct GameData {
|
pub struct GameData {
|
||||||
@@ -457,9 +451,11 @@ impl VulkanRenderer {
|
|||||||
self.game_data.textures.push(image_view);
|
self.game_data.textures.push(image_view);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn add_game_object(self: &mut Self, game_object: GameObject) -> usize {
|
pub fn add_game_object(self: &mut Self, game_object: GameObject) -> GameObjectHandle {
|
||||||
self.game_data.game_objects.push(game_object);
|
self.game_data.game_objects.push(game_object);
|
||||||
self.game_data.game_objects.len() - 1
|
GameObjectHandle {
|
||||||
|
object_index: self.game_data.game_objects.len() - 1
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -573,9 +569,3 @@ fn create_pipeline<V: vulkano::pipeline::vertex::Vertex>(device: Arc<Device>, re
|
|||||||
|
|
||||||
return Some(pipeline);
|
return Some(pipeline);
|
||||||
}
|
}
|
||||||
|
|
||||||
impl GameObject {
|
|
||||||
pub fn new(mesh: MeshHandle, texture_index: usize) -> GameObject {
|
|
||||||
GameObject { mesh_index: mesh, texture_index, model_matrix: Matrix4::identity() }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user