working generating camera rays from view and proj matrices
This commit is contained in:
+4
-4
@@ -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] = &[
|
||||
|
||||
+17
-10
@@ -1,12 +1,12 @@
|
||||
use cgmath::{Point3, Vector3, Matrix4, Deg};
|
||||
|
||||
#[rustfmt::skip]
|
||||
const OPENGL_TO_WGPU_MATRIX: cgmath::Matrix4<f32> = 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<f32> = 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<f32>,
|
||||
@@ -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<f32> {
|
||||
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<f32> {
|
||||
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<f32> {
|
||||
// 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;
|
||||
// }
|
||||
}
|
||||
|
||||
@@ -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; }
|
||||
|
||||
Binary file not shown.
Binary file not shown.
@@ -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);
|
||||
}
|
||||
|
||||
|
||||
+12
-5
@@ -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);
|
||||
}
|
||||
@@ -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;
|
||||
|
||||
+4
-11
@@ -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();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user