Move camera out of renderer
This commit is contained in:
parent
c6932e6646
commit
4f8602c48e
60
src/app.rs
60
src/app.rs
|
@ -5,13 +5,12 @@ use winit::{
|
||||||
event_loop::{ControlFlow, EventLoop},
|
event_loop::{ControlFlow, EventLoop},
|
||||||
};
|
};
|
||||||
|
|
||||||
use crate::{render::Context, renderer};
|
use crate::{camera, render, renderer};
|
||||||
|
|
||||||
pub(crate) struct App {
|
pub(crate) struct App {
|
||||||
window: winit::window::Window,
|
window: winit::window::Window,
|
||||||
event_loop: EventLoop<()>,
|
event_loop: EventLoop<()>,
|
||||||
render_ctx: Context,
|
render_ctx: render::Context,
|
||||||
renderer: renderer::Renderer,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl App {
|
impl App {
|
||||||
|
@ -25,18 +24,58 @@ impl App {
|
||||||
.build(&event_loop)
|
.build(&event_loop)
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
let render_ctx = Context::new(&window).await;
|
let render_ctx = render::Context::new(&window).await;
|
||||||
let renderer = renderer::Renderer::new(&render_ctx);
|
|
||||||
|
|
||||||
Self {
|
Self {
|
||||||
window,
|
window,
|
||||||
event_loop,
|
event_loop,
|
||||||
render_ctx,
|
render_ctx,
|
||||||
renderer,
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn run(mut self) {
|
pub fn run(self) {
|
||||||
|
let mut camera_controller = camera::CameraController::new(
|
||||||
|
&self.render_ctx,
|
||||||
|
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(
|
||||||
|
self.render_ctx.size.width,
|
||||||
|
self.render_ctx.size.height,
|
||||||
|
90.0_f32.to_radians(),
|
||||||
|
0.01,
|
||||||
|
100.0,
|
||||||
|
),
|
||||||
|
10.0,
|
||||||
|
0.25,
|
||||||
|
);
|
||||||
|
|
||||||
|
let camera_bind_group_layout = render::BindGroupLayoutBuilder::new()
|
||||||
|
.with_label("camera_bind_group_layout")
|
||||||
|
.with_entry(
|
||||||
|
wgpu::ShaderStages::COMPUTE,
|
||||||
|
wgpu::BindingType::Buffer {
|
||||||
|
ty: wgpu::BufferBindingType::Uniform,
|
||||||
|
has_dynamic_offset: false,
|
||||||
|
min_binding_size: None,
|
||||||
|
},
|
||||||
|
None,
|
||||||
|
)
|
||||||
|
.build(&self.render_ctx);
|
||||||
|
let camera_bind_group = render::BindGroupBuilder::new()
|
||||||
|
.with_label("camera_bind_group")
|
||||||
|
.with_layout(&camera_bind_group_layout)
|
||||||
|
.with_entry(camera_controller.get_buffer().as_entire_binding())
|
||||||
|
.build(&self.render_ctx);
|
||||||
|
|
||||||
|
let renderer = renderer::Renderer::new(&self.render_ctx, &camera_bind_group_layout);
|
||||||
|
|
||||||
let mut last_render_time = Instant::now();
|
let mut last_render_time = Instant::now();
|
||||||
self.event_loop
|
self.event_loop
|
||||||
.run(move |event, _, control_flow| match event {
|
.run(move |event, _, control_flow| match event {
|
||||||
|
@ -46,7 +85,7 @@ impl App {
|
||||||
} 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);
|
camera_controller.process_events(&event);
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
Event::MainEventsCleared => {
|
Event::MainEventsCleared => {
|
||||||
|
@ -56,8 +95,9 @@ 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);
|
camera_controller.update(dt);
|
||||||
self.renderer.render(&self.render_ctx);
|
camera_controller.update_buffer(&self.render_ctx);
|
||||||
|
renderer.render(&self.render_ctx, &camera_bind_group);
|
||||||
}
|
}
|
||||||
_ => {}
|
_ => {}
|
||||||
});
|
});
|
||||||
|
|
|
@ -13,12 +13,10 @@ 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 {
|
||||||
pub fn new(context: &Context) -> Self {
|
pub fn new(context: &Context, camera_bind_group_layout: &wgpu::BindGroupLayout) -> Self {
|
||||||
log::info!("Creating render shader...");
|
log::info!("Creating render shader...");
|
||||||
let shader_descriptor = wgpu::include_wgsl!("../assets/shaders/shader.wgsl");
|
let shader_descriptor = wgpu::include_wgsl!("../assets/shaders/shader.wgsl");
|
||||||
let shader = context.device.create_shader_module(shader_descriptor);
|
let shader = context.device.create_shader_module(shader_descriptor);
|
||||||
|
@ -71,45 +69,6 @@ impl Renderer {
|
||||||
voxel_texture.update(&context, &data);
|
voxel_texture.update(&context, &data);
|
||||||
|
|
||||||
log::info!("Creating camera...");
|
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 = BindGroupLayoutBuilder::new()
|
|
||||||
.with_label("camera_bind_group_layout")
|
|
||||||
.with_entry(
|
|
||||||
wgpu::ShaderStages::COMPUTE,
|
|
||||||
wgpu::BindingType::Buffer {
|
|
||||||
ty: wgpu::BufferBindingType::Uniform,
|
|
||||||
has_dynamic_offset: false,
|
|
||||||
min_binding_size: None,
|
|
||||||
},
|
|
||||||
None,
|
|
||||||
)
|
|
||||||
.build(context);
|
|
||||||
let camera_bind_group = BindGroupBuilder::new()
|
|
||||||
.with_label("camera_bind_group")
|
|
||||||
.with_layout(&camera_bind_group_layout)
|
|
||||||
.with_entry(camera_controller.get_buffer().as_entire_binding())
|
|
||||||
.build(context);
|
|
||||||
|
|
||||||
log::info!("Creating render pipeline...");
|
log::info!("Creating render pipeline...");
|
||||||
let render_pipeline =
|
let render_pipeline =
|
||||||
|
@ -192,13 +151,10 @@ impl Renderer {
|
||||||
render_pipeline,
|
render_pipeline,
|
||||||
render_texture,
|
render_texture,
|
||||||
voxel_texture,
|
voxel_texture,
|
||||||
camera_controller,
|
|
||||||
camera_bind_group,
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// pub fn render(&mut self, context: &Context, encoders: &[wgpu::CommandEncoder]) {
|
pub fn render(&self, context: &Context, camera_bind_group: &wgpu::BindGroup) {
|
||||||
pub fn render(&mut self, context: &Context) {
|
|
||||||
let frame = context.surface.get_current_texture().unwrap();
|
let frame = context.surface.get_current_texture().unwrap();
|
||||||
let view = frame
|
let view = frame
|
||||||
.texture
|
.texture
|
||||||
|
@ -213,7 +169,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.set_bind_group(2, 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);
|
||||||
|
|
||||||
|
@ -246,13 +202,4 @@ impl Renderer {
|
||||||
// context.queue.submit(command_buffers);
|
// context.queue.submit(command_buffers);
|
||||||
// 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: &Context) {
|
|
||||||
self.camera_controller.update(dt);
|
|
||||||
self.camera_controller.update_buffer(render_ctx)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue