diff --git a/build.rs b/build.rs index a1c065d..156b048 100644 --- a/build.rs +++ b/build.rs @@ -58,9 +58,14 @@ fn main() { let mut compiler = shaderc::Compiler::new().expect("Unable to create shader compiler"); for shader in shaders { println!("cargo:rerun-if-changed={}", shader.src_path.as_os_str().to_str().unwrap()); - let compiled = compiler.compile_into_spirv(&shader.source, shader.kind, &shader.src_path.to_str().unwrap(), "main", Some(&options)) - .expect("Shader error:"); - write(shader.spv_path, compiled.as_binary_u8()).unwrap(); + + let result = compiler.compile_into_spirv(&shader.source, shader.kind, &shader.src_path.to_str().unwrap(), "main", Some(&options)); + match result { + Err(err) => { panic!("Failed: {}", err); }, + Ok(data) => { + write(shader.spv_path, data.as_binary_u8()).unwrap(); + } + } } // Rebuild if any of the cginc change diff --git a/src/app.rs b/src/app.rs index 56914a4..dad7791 100644 --- a/src/app.rs +++ b/src/app.rs @@ -13,10 +13,10 @@ use crate::camera_controller::CameraController; // A B // C D const VERTICES: &[Vertex] = &[ - Vertex { position: [-1.0, 1.0, 0.0], uv: [0.0, 0.0] }, // A - Vertex { position: [ 1.0, 1.0, 0.0], uv: [1.0, 0.0] }, // B - Vertex { position: [ 1.0,-1.0, 0.0], uv: [1.0, 1.0] }, // C - Vertex { position: [-1.0,-1.0, 0.0], uv: [0.0, 1.0] }, // D + Vertex { position: [-1.0, 1.0, 0.0], uv: [-1.0, 1.0] }, // A + Vertex { position: [ 1.0, 1.0, 0.0], uv: [1.0, 1.0] }, // B + Vertex { position: [ 1.0,-1.0, 0.0], uv: [1.0, -1.0] }, // C + Vertex { position: [-1.0,-1.0, 0.0], uv: [-1.0, -1.0] }, // D ]; const INDICES: &[u16] = &[ diff --git a/src/camera.rs b/src/camera.rs index 61b855f..bd95ffd 100644 --- a/src/camera.rs +++ b/src/camera.rs @@ -1,12 +1,12 @@ use cgmath::{Point3, Vector3, Matrix4, Deg}; -#[rustfmt::skip] -const OPENGL_TO_WGPU_MATRIX: cgmath::Matrix4 = cgmath::Matrix4::new( - 1.0, 0.0, 0.0, 0.0, - 0.0, 1.0, 0.0, 0.0, - 0.0, 0.0, 0.5, 0.0, - 0.0, 0.0, 0.5, 1.0, -); +// #[rustfmt::skip] +// const OPENGL_TO_WGPU_MATRIX: cgmath::Matrix4 = cgmath::Matrix4::new( +// 1.0, 0.0, 0.0, 0.0, +// 0.0, 1.0, 0.0, 0.0, +// 0.0, 0.0, 0.5, 0.0, +// 0.0, 0.0, 0.5, 1.0, +// ); pub struct Camera { pub position: Point3, @@ -22,7 +22,7 @@ impl Camera { pub fn new(aspect: f32, fov: f32) -> Self { Self { - position: (0.0, 0.0, 0.0).into(), + position: (0.0, 1.0, 0.0).into(), forward: (0.0, 0.0, 1.0).into(), up: Vector3::unit_y(), aspect, @@ -37,10 +37,17 @@ impl Camera { } pub fn proj_matrix(&self) -> Matrix4 { - return OPENGL_TO_WGPU_MATRIX * cgmath::perspective(Deg(self.fovy), self.aspect, self.znear, self.zfar); + return cgmath::perspective(Deg(self.fovy), self.aspect, self.znear, self.zfar); } pub fn view_matrix(&self) -> Matrix4 { - return OPENGL_TO_WGPU_MATRIX * Matrix4::look_to_rh(self.position, self.forward, self.up); + return Matrix4::look_to_rh((0.0,0.0,0.0).into(), self.forward, self.up); } + + // pub fn view_projection_matrix(&self) -> Matrix4 { + // let view = Matrix4::look_to_rh(self.position, self.forward, self.up); + // let proj = cgmath::perspective(Deg(self.fovy), self.aspect, self.znear, self.zfar); + // return proj; + // //return proj * view; + // } } diff --git a/src/camera_controller.rs b/src/camera_controller.rs index a180a92..36d6e11 100644 --- a/src/camera_controller.rs +++ b/src/camera_controller.rs @@ -11,6 +11,7 @@ struct CameraMovement { pub back: bool, pub left: bool, pub right: bool, + pub shift: bool, pub speed: f32, } @@ -59,6 +60,7 @@ impl CameraController { VirtualKeyCode::A | VirtualKeyCode::Left => { self.movement.left = is_pressed; true } VirtualKeyCode::S | VirtualKeyCode::Down => { self.movement.back = is_pressed; true } VirtualKeyCode::D | VirtualKeyCode::Right => { self.movement.right = is_pressed; true } + VirtualKeyCode::LShift => { self.movement.shift = is_pressed; true } _ => false, } } @@ -94,7 +96,8 @@ impl CameraController { camera.up = yaw_rot.transform_vector(pitch_rot.transform_vector(Vector3::unit_y())); // Movement amount - let amount = delta_time * self.movement.speed; + let mut amount = delta_time * self.movement.speed; + if self.movement.shift { amount *= 2.0; } // Forward and backwards if self.movement.fwd && camera.forward.magnitude() > amount { camera.position += camera.forward.normalize() * amount; } diff --git a/src/shaders/bin/shader.frag.spv b/src/shaders/bin/shader.frag.spv index 97ef2c5..a2508ce 100644 Binary files a/src/shaders/bin/shader.frag.spv and b/src/shaders/bin/shader.frag.spv differ diff --git a/src/shaders/bin/shader.vert.spv b/src/shaders/bin/shader.vert.spv index ffe964f..dd48e11 100644 Binary files a/src/shaders/bin/shader.vert.spv and b/src/shaders/bin/shader.vert.spv differ diff --git a/src/shaders/raymarching.cginc b/src/shaders/raymarching.cginc index ec311d8..c853868 100644 --- a/src/shaders/raymarching.cginc +++ b/src/shaders/raymarching.cginc @@ -3,8 +3,8 @@ vec2 map(in vec3 pos) { vec2 d1 = vec2(sdPlane(pos), 1); - vec2 d2 = vec2(sdSphere(pos - vec3(0.0, 2.0, 0.0)), 2); - vec2 d3 = vec2(sdSphere(pos - vec3(2.0, 1.2, 2.0)), 3); + vec2 d2 = vec2(sdSphere(pos - vec3(0.0, 2.0, 5.0)), 2); + vec2 d3 = vec2(sdSphere(pos - vec3(0.0, 3.0, 3.0)), 3); return sdUnion(sdUnion(d1, d2), d3); } diff --git a/src/shaders/shader.frag b/src/shaders/shader.frag index 7dc1d5d..b6b3a4f 100644 --- a/src/shaders/shader.frag +++ b/src/shaders/shader.frag @@ -7,6 +7,13 @@ layout(location=0) out vec4 out_color; layout(set=0, binding=0) uniform texture2D tex; layout(set=0, binding=1) uniform sampler tex_smp; +layout(set=1, binding=0) uniform Uniforms +{ + mat4 view_matrix; + mat4 proj_matrix_inv; + vec3 cam_pos; +}; + // Defines #define PI 3.141592 #define EPSILON 0.01 @@ -19,10 +26,10 @@ layout(set=0, binding=1) uniform sampler tex_smp; void main() { - // Calculate ray - vec3 ro = vec3(0.0, 2, -6.0); // Ray origin - vec3 rd = normalize(vec3(vert_uv - vec2(0.5, 0.5), 1.001)); // Ray direction - + vec3 view_dir = (proj_matrix_inv * vec4(vert_uv, 0, 1)).xyz; + vec3 ray_dir = normalize(view_matrix * vec4(view_dir,0)).xyz; + + // Render - out_color = vec4(raymarch(ro, rd), 1); + out_color = vec4(raymarch(cam_pos, ray_dir), 1); } \ No newline at end of file diff --git a/src/shaders/shader.vert b/src/shaders/shader.vert index d4459e9..00a9fbf 100644 --- a/src/shaders/shader.vert +++ b/src/shaders/shader.vert @@ -4,14 +4,6 @@ layout(location=0) in vec3 in_position; layout(location=1) in vec2 in_uv; layout(location=0) out vec2 vert_uv; -layout(set=1, binding=0) uniform Uniforms -{ - mat4 view; // world to camera - mat4 proj; // camera to screen - mat4 proj_inv; // screen to camera - vec3 cam_pos; // camera position -}; - void main() { vert_uv = in_uv; diff --git a/src/uniforms.rs b/src/uniforms.rs index c1ffb4e..e307e19 100644 --- a/src/uniforms.rs +++ b/src/uniforms.rs @@ -6,8 +6,7 @@ use crate::camera::Camera; #[repr(C)] #[derive(Default, Debug, Copy, Clone, bytemuck::Pod, bytemuck::Zeroable)] pub struct Uniforms { - view: [[f32; 4]; 4], - proj: [[f32; 4]; 4], + view_inv: [[f32; 4]; 4], proj_inv: [[f32; 4]; 4], cam_pos: [f32; 3] } @@ -19,14 +18,8 @@ impl Uniforms { } pub fn update(&mut self, camera: &Camera) { - let view = camera.view_matrix(); - let proj = camera.proj_matrix(); - let proj_inv = proj.invert().unwrap(); - let cam_pos = camera.position; - - self.view = view.into(); - self.proj = proj.into(); - self.proj_inv = proj_inv.into(); - self.cam_pos = cam_pos.into(); + self.view_inv = camera.view_matrix().invert().unwrap().into(); + self.proj_inv = camera.proj_matrix().invert().unwrap().into(); + self.cam_pos = camera.position.into(); } }