diff --git a/config/input.toml b/config/input.toml index 3c583b7..653867c 100644 --- a/config/input.toml +++ b/config/input.toml @@ -64,12 +64,12 @@ name = "move_forward" controller_axis = "LeftStickY" [[axis]] -name = "move_sideways" +name = "move_right" positive_button = "button_right" negative_button = "button_left" [[axis]] -name = "move_sideways" +name = "move_right" controller_axis = "LeftStickX" [[axis]] diff --git a/levels/test.lvl b/levels/test.lvl index 2b2611f..9dbf896 100644 --- a/levels/test.lvl +++ b/levels/test.lvl @@ -50,15 +50,15 @@ "player": { "mesh_index": null, "position": [ - 2.5716693, - 1.304583, - -8.255934 + 0.0, + 0.0, + 10.0 ], "rotation": [ - -0.23996419, - -0.011724341, - 0.9695545, - 0.047371186 + 1.0, + 0.0, + 0.0, + 0.0 ], "scale": [ 1.0, diff --git a/shaders/text.vert b/shaders/text.vert index 1792918..45a9617 100644 --- a/shaders/text.vert +++ b/shaders/text.vert @@ -21,14 +21,6 @@ out gl_PerVertex { }; void main() { - float scale = 0.01; - mat4 invert = mat4( - scale, 0., 0., 0., - 0., scale, 0., 0., - 0., 0., scale, 0., - 0., 0., 0., 1. - ); - // Vertex position in camera gl_Position = ubo.ortho_projection * vec4(position, 1.0); diff --git a/src/game/player.rs b/src/game/player.rs index 0a1ab3e..fe87947 100644 --- a/src/game/player.rs +++ b/src/game/player.rs @@ -1,10 +1,9 @@ -use cgmath::{Deg, InnerSpace, Matrix4, One, Quaternion, Rad, Rotation, Rotation3, SquareMatrix, Vector2, Vector3, Vector4, vec3, vec4}; -use vulkano::buffer::TypedBufferAccess; +use cgmath::{Deg, InnerSpace, Matrix4, One, Quaternion, Rad, Rotation, Rotation3, SquareMatrix, Vector2, Vector3, vec3, vec4}; use crate::game::player::PlayerMovementMode::{FirstPerson, Flying}; use crate::input::InputState; -use crate::vulkan::Mesh; +use crate::util::{intersection_distance, print_matrix}; use crate::vulkan::gameobject::GameObject; use crate::vulkan::{ gameobject::Updatable, @@ -51,7 +50,6 @@ impl Camera { 0.1, 100.0 ); - // Why? self.proj.y.y *= -1.0; self.ortho_proj = cgmath::ortho(0., width, 0., height, -1., 1.); @@ -123,58 +121,6 @@ impl Player { } } -pub fn vec4_from_pos(pos: [f32; 3]) -> Vector4 { - vec4(pos[0], pos[1], pos[2], 1.0) -} - -pub fn intersection_distance(ray_origin: Vector3, ray_direction: Vector3, mesh: &Mesh, game_object: &GameObject) -> Option { - let index_lock = mesh.index_buffer.read().unwrap(); - let vertex_lock = mesh.vertex_buffer.read().unwrap(); - (0..mesh.index_buffer.len() / 3).map(|tri_idx| { - let vtx_a = game_object.get_model_matrix() * vec4_from_pos(vertex_lock[index_lock[tri_idx as usize * 3 ] as usize].position); - let vtx_b = game_object.get_model_matrix() * vec4_from_pos(vertex_lock[index_lock[tri_idx as usize * 3 + 1] as usize].position); - let vtx_c = game_object.get_model_matrix() * vec4_from_pos(vertex_lock[index_lock[tri_idx as usize * 3 + 2] as usize].position); - intersect_triangle(ray_origin, ray_direction, vtx_a.truncate().into(), vtx_b.truncate().into(), vtx_c.truncate().into()) - }).fold(None, |acc, x| { - if let Some(smallest) = acc { - if let Some(new) = x { - if new < smallest { - Some(new) - } else { - Some(smallest) - } - } else { - acc - } - } else { - x - } - }) -} - -// https://en.wikipedia.org/wiki/M%C3%B6ller%E2%80%93Trumbore_intersection_algorithm -pub fn intersect_triangle(ray_origin: Vector3, ray_direction: Vector3, vtx_a: [f32; 3], vtx_b: [f32; 3], vtx_c: [f32; 3]) -> Option { - let edge_1 = vec3(vtx_b[0] - vtx_a[0], vtx_b[1] - vtx_a[1], vtx_b[2] - vtx_a[2]); - let edge_2 = vec3(vtx_c[0] - vtx_a[0], vtx_c[1] - vtx_a[1], vtx_c[2] - vtx_a[2]); - let h = ray_direction.cross(edge_2); - let a = edge_1.dot(h); - if a > -f32::EPSILON && a < f32::EPSILON { return None; } - - let f = 1. / a; - let s: Vector3 = ray_origin - Vector3::from(vtx_a); - let u = f * s.dot(h); - if u < 0. || u > 1. { return None; } - - let q = s.cross(edge_1); - let v = f * ray_direction.dot(q); - if v < 0. || u + v > 1. { return None; } - - let t = f * edge_2.dot(q); - if t > f32::EPSILON { return Some(t); } - - None -} - impl Updatable for Player { fn update(&mut self, delta_time: f32, input: &InputState, _game_state: &mut GameState, game_objects: &mut Vec, renderer: &mut VulkanRenderer) { // Edit mode @@ -204,7 +150,7 @@ impl Updatable for Player { self.camera.rotation = x_rot * y_rot; // Movement - let local_input = vec3(input.get_axis("move_sideways"), 0.0, -input.get_axis("move_forward")) * self.movement_speed * delta_time; + let local_input = vec3(input.get_axis("move_right"), 0.0, -input.get_axis("move_forward")) * self.movement_speed * delta_time; if self.movement_mode == FirstPerson { let mut world_input = self.camera.local_to_world(local_input); @@ -217,4 +163,4 @@ impl Updatable for Player { // Update self.camera.update(renderer); } -} \ No newline at end of file +} diff --git a/src/main.rs b/src/main.rs index 40da5c9..8b258e8 100644 --- a/src/main.rs +++ b/src/main.rs @@ -12,6 +12,7 @@ mod config; mod game; mod tests; mod text; +mod util; fn main() { let log_config = LogConfig::from_file("config/log.toml"); diff --git a/src/util.rs b/src/util.rs new file mode 100644 index 0000000..0f63911 --- /dev/null +++ b/src/util.rs @@ -0,0 +1,80 @@ +use cgmath::{InnerSpace, Matrix4, SquareMatrix, Vector3, Vector4, vec3, vec4}; +use vulkano::buffer::TypedBufferAccess; + +use crate::vulkan::{Mesh, gameobject::GameObject}; + +pub fn print_matrix(mat: Matrix4) { + let cols = [ + [mat.x.x, mat.x.y, mat.x.z, mat.x.w], + [mat.y.x, mat.y.y, mat.y.z, mat.y.w], + [mat.z.x, mat.z.y, mat.z.z, mat.z.w], + [mat.w.x, mat.w.y, mat.w.z, mat.w.w], + ].map(|v| v.map(|e| format!("{:.2}", e))); + + let col_sizes: Vec = cols.iter().map(|col| col.iter().map(|s| s.len()).max().unwrap()).collect(); + + println!("Mat4: {}", mat.determinant()); + for row_index in 0..4 { + for col_index in 0..4 { + let mut str = format!("{}", cols[col_index][row_index]); + while str.len() < col_sizes[col_index] { + str.insert(0, ' '); + } + print!("{}", str); + if col_index < 3 { print!(", "); } + } + println!(); + } +} + +pub fn intersection_distance(ray_origin: Vector3, ray_direction: Vector3, mesh: &Mesh, game_object: &GameObject) -> Option { + let index_lock = mesh.index_buffer.read().unwrap(); + let vertex_lock = mesh.vertex_buffer.read().unwrap(); + (0..mesh.index_buffer.len() / 3).map(|tri_idx| { + let vtx_a = game_object.get_model_matrix() * vec4_from_pos(vertex_lock[index_lock[tri_idx as usize * 3 ] as usize].position); + let vtx_b = game_object.get_model_matrix() * vec4_from_pos(vertex_lock[index_lock[tri_idx as usize * 3 + 1] as usize].position); + let vtx_c = game_object.get_model_matrix() * vec4_from_pos(vertex_lock[index_lock[tri_idx as usize * 3 + 2] as usize].position); + intersect_triangle(ray_origin, ray_direction, vtx_a.truncate().into(), vtx_b.truncate().into(), vtx_c.truncate().into()) + }).fold(None, |acc, x| { + if let Some(smallest) = acc { + if let Some(new) = x { + if new < smallest { + Some(new) + } else { + Some(smallest) + } + } else { + acc + } + } else { + x + } + }) +} + +// https://en.wikipedia.org/wiki/M%C3%B6ller%E2%80%93Trumbore_intersection_algorithm +pub fn intersect_triangle(ray_origin: Vector3, ray_direction: Vector3, vtx_a: [f32; 3], vtx_b: [f32; 3], vtx_c: [f32; 3]) -> Option { + let edge_1 = vec3(vtx_b[0] - vtx_a[0], vtx_b[1] - vtx_a[1], vtx_b[2] - vtx_a[2]); + let edge_2 = vec3(vtx_c[0] - vtx_a[0], vtx_c[1] - vtx_a[1], vtx_c[2] - vtx_a[2]); + let h = ray_direction.cross(edge_2); + let a = edge_1.dot(h); + if a > -f32::EPSILON && a < f32::EPSILON { return None; } + + let f = 1. / a; + let s: Vector3 = ray_origin - Vector3::from(vtx_a); + let u = f * s.dot(h); + if u < 0. || u > 1. { return None; } + + let q = s.cross(edge_1); + let v = f * ray_direction.dot(q); + if v < 0. || u + v > 1. { return None; } + + let t = f * edge_2.dot(q); + if t > f32::EPSILON { return Some(t); } + + None +} + +pub fn vec4_from_pos(pos: [f32; 3]) -> Vector4 { + vec4(pos[0], pos[1], pos[2], 1.0) +} \ No newline at end of file