3d camera

This commit is contained in:
Werner
2021-11-03 19:05:49 -03:00
parent 4d734dc099
commit 19b9c8c148
3 changed files with 244 additions and 5 deletions

View File

@ -1,8 +1,12 @@
#![allow(non_snake_case)]
#[path = "Camera.rs"]
mod _Camera;
use _Camera::*;
#[path = "Texture.rs"]
mod _Texture;
use _Texture::Texture;
use _Texture::*;
use bytemuck::{Pod, Zeroable};
use image::GenericImageView;
@ -83,6 +87,11 @@ struct State {
num_indices: u32,
diffuse_bind_group: wgpu::BindGroup,
diffuse_texture: Texture,
camera: Camera,
camera_uniform: CameraUniform,
camera_buffer: wgpu::Buffer,
camera_bind_group: wgpu::BindGroup,
camera_controller: CameraController,
}
impl State {
@ -223,6 +232,57 @@ impl State {
label: Some("diffuse_bind_group"),
});
// Camera
let camera = Camera {
// position the camera one unit up and 2 units back
// +z is out of the screen
eye: (0.0, 1.0, 2.0).into(),
// have it look at the origin
target: (0.0, 0.0, 0.0).into(),
// which way is "up"
up: cgmath::Vector3::unit_y(),
aspect: config.width as f32 / config.height as f32,
fovy: 45.0,
znear: 0.1,
zfar: 100.0,
};
let camera_controller = CameraController::new(0.2);
let mut camera_uniform = CameraUniform::new();
camera_uniform.update_view_proj(&camera);
let camera_buffer = device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
label: Some("Camera Buffer"),
contents: bytemuck::cast_slice(&[camera_uniform]),
usage: wgpu::BufferUsages::UNIFORM | wgpu::BufferUsages::COPY_DST,
});
let camera_bind_group_layout =
device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
entries: &[wgpu::BindGroupLayoutEntry {
binding: 0,
visibility: wgpu::ShaderStages::VERTEX,
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 = device.create_bind_group(&wgpu::BindGroupDescriptor {
layout: &camera_bind_group_layout,
entries: &[wgpu::BindGroupEntry {
binding: 0,
resource: camera_buffer.as_entire_binding(),
}],
label: Some("camera_bind_group"),
});
// Shader
let shader = device.create_shader_module(&wgpu::ShaderModuleDescriptor {
@ -235,7 +295,7 @@ impl State {
let render_pipeline_layout =
device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
label: Some("Render Pipeline Layout"),
bind_group_layouts: &[&texture_bind_group_layout],
bind_group_layouts: &[&texture_bind_group_layout, &camera_bind_group_layout],
push_constant_ranges: &[],
});
@ -306,6 +366,11 @@ impl State {
num_indices,
diffuse_bind_group,
diffuse_texture,
camera,
camera_uniform,
camera_buffer,
camera_bind_group,
camera_controller,
}
}
@ -319,10 +384,18 @@ impl State {
}
fn input(&mut self, event: &WindowEvent) -> bool {
false
self.camera_controller.process_events(event)
}
fn update(&mut self) {}
fn update(&mut self) {
self.camera_controller.update_camera(&mut self.camera);
self.camera_uniform.update_view_proj(&self.camera);
self.queue.write_buffer(
&self.camera_buffer,
0,
bytemuck::cast_slice(&[self.camera_uniform]),
);
}
fn render(&mut self) -> Result<(), wgpu::SurfaceError> {
let output = self.surface.get_current_texture()?;
@ -359,9 +432,13 @@ impl State {
});
render_pass.set_pipeline(&self.render_pipeline);
render_pass.set_bind_group(0, &self.diffuse_bind_group, &[]);
render_pass.set_bind_group(1, &self.camera_bind_group, &[]);
render_pass.set_vertex_buffer(0, self.vertex_buffer.slice(..));
render_pass.set_index_buffer(self.index_buffer.slice(..), wgpu::IndexFormat::Uint16);
render_pass.draw_indexed(0..self.num_indices, 0, 0..1);
}