Integrate camera system
This commit is contained in:
parent
1d26c78b3e
commit
9554f3ba7f
|
@ -1,13 +1,14 @@
|
||||||
@group(0) @binding(0) var output: texture_storage_2d<rgba8unorm, write>;
|
@group(0) @binding(0) var output: texture_storage_2d<rgba8unorm, write>;
|
||||||
@group(1) @binding(0) var voxels_t: texture_3d<f32>;
|
@group(1) @binding(0) var voxels_t: texture_3d<f32>;
|
||||||
@group(1) @binding(1) var voxels_s: sampler;
|
@group(1) @binding(1) var voxels_s: sampler;
|
||||||
// @group(2) @binding(0) var<uniform> camera: Camera;
|
@group(2) @binding(0) var<uniform> camera: Camera;
|
||||||
|
|
||||||
// struct Camera {
|
struct Camera {
|
||||||
// projection: mat4x4<f32>,
|
projection: mat4x4<f32>,
|
||||||
// view: mat4x4<f32>,
|
view: mat4x4<f32>,
|
||||||
// pos: vec3<f32>,
|
pos: vec3<f32>,
|
||||||
// };
|
_pad: f32,
|
||||||
|
};
|
||||||
|
|
||||||
struct HitInfo {
|
struct HitInfo {
|
||||||
hit: bool,
|
hit: bool,
|
||||||
|
@ -115,15 +116,10 @@ fn compute(@builtin(global_invocation_id) global_id: vec3<u32>) {
|
||||||
// Construct ray
|
// Construct ray
|
||||||
let img_coord_frac = vec2<f32>(img_coord) / vec2<f32>(img_dims);
|
let img_coord_frac = vec2<f32>(img_coord) / vec2<f32>(img_dims);
|
||||||
let screen_pos = img_coord_frac * 2.0 - vec2<f32>(1.0);
|
let screen_pos = img_coord_frac * 2.0 - vec2<f32>(1.0);
|
||||||
// var ray_eye = camera.projection * vec4<f32>(screen_pos, -1.0, 0.0);
|
var ray_eye = camera.projection * vec4<f32>(screen_pos, -1.0, 0.0);
|
||||||
// ray_eye = vec4<f32>(ray_eye.xy, -1.0, 0.0);
|
ray_eye = vec4<f32>(ray_eye.xy, -1.0, 0.0);
|
||||||
// let ray_dir = normalize((camera.view * ray_eye).xyz);
|
let ray_dir = normalize((camera.view * ray_eye).xyz);
|
||||||
// let ray_pos = camera.pos;
|
let ray_pos = camera.pos;
|
||||||
let camera_dir = vec3<f32>(0.01, 0.0, 0.8);
|
|
||||||
let camera_plane_u = vec3<f32>(1.0, 0.0, 0.0);
|
|
||||||
let camera_plane_v = vec3<f32>(0.0, 1.0, 0.0) * f32(img_dims.y) / f32(img_dims.x);
|
|
||||||
let ray_dir = camera_dir + screen_pos.x * camera_plane_u + screen_pos.y * camera_plane_v;
|
|
||||||
let ray_pos = vec3<f32>(-4.01, 5.0, -10.0);
|
|
||||||
|
|
||||||
// Cast the ray
|
// Cast the ray
|
||||||
var hit_info = cast_ray(ray_pos, ray_dir);
|
var hit_info = cast_ray(ray_pos, ray_dir);
|
||||||
|
|
|
@ -45,7 +45,9 @@ impl App {
|
||||||
window_id,
|
window_id,
|
||||||
} if window_id == self.window.id() => match event {
|
} if window_id == self.window.id() => match event {
|
||||||
WindowEvent::CloseRequested => *control_flow = ControlFlow::Exit,
|
WindowEvent::CloseRequested => *control_flow = ControlFlow::Exit,
|
||||||
_ => {}
|
_ => {
|
||||||
|
self.renderer.input(&event);
|
||||||
|
}
|
||||||
},
|
},
|
||||||
Event::MainEventsCleared => {
|
Event::MainEventsCleared => {
|
||||||
self.window.request_redraw();
|
self.window.request_redraw();
|
||||||
|
@ -54,6 +56,7 @@ impl App {
|
||||||
let now = Instant::now();
|
let now = Instant::now();
|
||||||
let dt = now - last_render_time;
|
let dt = now - last_render_time;
|
||||||
last_render_time = now;
|
last_render_time = now;
|
||||||
|
self.renderer.update(dt, &self.render_ctx);
|
||||||
self.renderer.render(&self.render_ctx);
|
self.renderer.render(&self.render_ctx);
|
||||||
}
|
}
|
||||||
_ => {}
|
_ => {}
|
||||||
|
|
|
@ -1,5 +1,10 @@
|
||||||
|
use std::time::Duration;
|
||||||
|
|
||||||
|
use wgpu::util::DeviceExt;
|
||||||
|
use winit::event::WindowEvent;
|
||||||
use winit::{dpi::PhysicalSize, window::Window};
|
use winit::{dpi::PhysicalSize, window::Window};
|
||||||
|
|
||||||
|
use crate::camera;
|
||||||
use crate::texture::{Texture, TextureBuilder};
|
use crate::texture::{Texture, TextureBuilder};
|
||||||
|
|
||||||
pub(crate) struct RenderContext {
|
pub(crate) struct RenderContext {
|
||||||
|
@ -77,6 +82,8 @@ pub(crate) struct Renderer {
|
||||||
render_pipeline: wgpu::RenderPipeline,
|
render_pipeline: wgpu::RenderPipeline,
|
||||||
render_texture: Texture,
|
render_texture: Texture,
|
||||||
voxel_texture: Texture,
|
voxel_texture: Texture,
|
||||||
|
camera_controller: camera::CameraController,
|
||||||
|
camera_bind_group: wgpu::BindGroup,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Renderer {
|
impl Renderer {
|
||||||
|
@ -132,6 +139,56 @@ impl Renderer {
|
||||||
}
|
}
|
||||||
voxel_texture.update(&context, &data);
|
voxel_texture.update(&context, &data);
|
||||||
|
|
||||||
|
log::info!("Creating camera...");
|
||||||
|
let camera_controller = camera::CameraController::new(
|
||||||
|
context,
|
||||||
|
camera::Camera::new(
|
||||||
|
glam::Vec3 {
|
||||||
|
x: 4.01,
|
||||||
|
y: 4.01,
|
||||||
|
z: 20.0,
|
||||||
|
},
|
||||||
|
-90.0_f32.to_radians(),
|
||||||
|
0.0_f32.to_radians(),
|
||||||
|
),
|
||||||
|
camera::Projection::new(
|
||||||
|
context.size.width,
|
||||||
|
context.size.height,
|
||||||
|
90.0_f32.to_radians(),
|
||||||
|
0.01,
|
||||||
|
100.0,
|
||||||
|
),
|
||||||
|
10.0,
|
||||||
|
0.25,
|
||||||
|
);
|
||||||
|
|
||||||
|
let camera_bind_group_layout =
|
||||||
|
context
|
||||||
|
.device
|
||||||
|
.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
|
||||||
|
entries: &[wgpu::BindGroupLayoutEntry {
|
||||||
|
binding: 0,
|
||||||
|
visibility: wgpu::ShaderStages::COMPUTE,
|
||||||
|
ty: wgpu::BindingType::Buffer {
|
||||||
|
ty: wgpu::BufferBindingType::Uniform,
|
||||||
|
has_dynamic_offset: false,
|
||||||
|
min_binding_size: None,
|
||||||
|
},
|
||||||
|
count: None,
|
||||||
|
}],
|
||||||
|
label: Some("camera_bind_group_layout"),
|
||||||
|
});
|
||||||
|
let camera_bind_group = context
|
||||||
|
.device
|
||||||
|
.create_bind_group(&wgpu::BindGroupDescriptor {
|
||||||
|
layout: &camera_bind_group_layout,
|
||||||
|
entries: &[wgpu::BindGroupEntry {
|
||||||
|
binding: 0,
|
||||||
|
resource: camera_controller.get_buffer().as_entire_binding(),
|
||||||
|
}],
|
||||||
|
label: Some("camera_bind_group"),
|
||||||
|
});
|
||||||
|
|
||||||
log::info!("Creating render pipeline...");
|
log::info!("Creating render pipeline...");
|
||||||
let render_pipeline =
|
let render_pipeline =
|
||||||
context
|
context
|
||||||
|
@ -201,6 +258,7 @@ impl Renderer {
|
||||||
bind_group_layouts: &[
|
bind_group_layouts: &[
|
||||||
&compute_layout,
|
&compute_layout,
|
||||||
&voxel_texture.bind_group_layout,
|
&voxel_texture.bind_group_layout,
|
||||||
|
&camera_bind_group_layout,
|
||||||
],
|
],
|
||||||
push_constant_ranges: &[],
|
push_constant_ranges: &[],
|
||||||
},
|
},
|
||||||
|
@ -223,6 +281,8 @@ impl Renderer {
|
||||||
render_pipeline,
|
render_pipeline,
|
||||||
render_texture,
|
render_texture,
|
||||||
voxel_texture,
|
voxel_texture,
|
||||||
|
camera_controller,
|
||||||
|
camera_bind_group,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -241,6 +301,7 @@ impl Renderer {
|
||||||
compute_pass.set_pipeline(&self.compute_pipeline);
|
compute_pass.set_pipeline(&self.compute_pipeline);
|
||||||
compute_pass.set_bind_group(0, &self.compute_bind_group, &[]);
|
compute_pass.set_bind_group(0, &self.compute_bind_group, &[]);
|
||||||
compute_pass.set_bind_group(1, &self.voxel_texture.bind_group, &[]);
|
compute_pass.set_bind_group(1, &self.voxel_texture.bind_group, &[]);
|
||||||
|
compute_pass.set_bind_group(2, &self.camera_bind_group, &[]);
|
||||||
compute_pass.dispatch_workgroups(size.width / 8, size.height / 8, 1);
|
compute_pass.dispatch_workgroups(size.width / 8, size.height / 8, 1);
|
||||||
drop(compute_pass);
|
drop(compute_pass);
|
||||||
|
|
||||||
|
@ -265,4 +326,13 @@ impl Renderer {
|
||||||
context.queue.submit(Some(encoder.finish()));
|
context.queue.submit(Some(encoder.finish()));
|
||||||
frame.present();
|
frame.present();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn input(&mut self, event: &WindowEvent) -> bool {
|
||||||
|
self.camera_controller.process_events(event)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn update(&mut self, dt: Duration, render_ctx: &RenderContext) {
|
||||||
|
self.camera_controller.update(dt);
|
||||||
|
self.camera_controller.update_buffer(render_ctx)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue