instancing, camera movement
This commit is contained in:
parent
f88df1efd0
commit
4ab10fe790
12 changed files with 599 additions and 78 deletions
27
Cargo.lock
generated
27
Cargo.lock
generated
|
@ -37,6 +37,21 @@ dependencies = [
|
||||||
"libc",
|
"libc",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "anyhow"
|
||||||
|
version = "1.0.65"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "98161a4e3e2184da77bb14f02184cdd111e83bbbcc9979dfee3c44b9a85f5602"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "approx"
|
||||||
|
version = "0.4.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "3f2a05fd1bd10b2527e20a2cd32d8873d115b8b39fe219ee25f42a8aca6ba278"
|
||||||
|
dependencies = [
|
||||||
|
"num-traits",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "arrayvec"
|
name = "arrayvec"
|
||||||
version = "0.7.2"
|
version = "0.7.2"
|
||||||
|
@ -168,6 +183,16 @@ version = "0.1.1"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "fd16c4719339c4530435d38e511904438d07cce7950afa3718a84ac36c10e89e"
|
checksum = "fd16c4719339c4530435d38e511904438d07cce7950afa3718a84ac36c10e89e"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "cgmath"
|
||||||
|
version = "0.18.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "1a98d30140e3296250832bbaaff83b27dcd6fa3cc70fb6f1f3e5c9c0023b5317"
|
||||||
|
dependencies = [
|
||||||
|
"approx",
|
||||||
|
"num-traits",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "cocoa"
|
name = "cocoa"
|
||||||
version = "0.24.0"
|
version = "0.24.0"
|
||||||
|
@ -1257,7 +1282,9 @@ checksum = "f1382d1f0a252c4bf97dc20d979a2fdd05b024acd7c2ed0f7595d7817666a157"
|
||||||
name = "renderer"
|
name = "renderer"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
|
"anyhow",
|
||||||
"bytemuck",
|
"bytemuck",
|
||||||
|
"cgmath",
|
||||||
"env_logger",
|
"env_logger",
|
||||||
"image",
|
"image",
|
||||||
"log",
|
"log",
|
||||||
|
|
|
@ -13,3 +13,5 @@ wgpu = "0.13"
|
||||||
pollster = "0.2"
|
pollster = "0.2"
|
||||||
bytemuck = { version = "1.4", features = [ "derive" ] }
|
bytemuck = { version = "1.4", features = [ "derive" ] }
|
||||||
image = { version = "0.24", features = [ "png" ] }
|
image = { version = "0.24", features = [ "png" ] }
|
||||||
|
anyhow = "1.0"
|
||||||
|
cgmath = "0.18"
|
||||||
|
|
236
src/core/camera.rs
Normal file
236
src/core/camera.rs
Normal file
|
@ -0,0 +1,236 @@
|
||||||
|
use std::time::Duration;
|
||||||
|
|
||||||
|
use cgmath::num_traits::clamp;
|
||||||
|
use winit::event::*;
|
||||||
|
|
||||||
|
pub struct Camera {
|
||||||
|
pub position: cgmath::Point3<f32>,
|
||||||
|
pub pitch: f32,
|
||||||
|
pub yaw: f32,
|
||||||
|
pub projection: Projection,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct Projection {
|
||||||
|
pub aspect: f32,
|
||||||
|
pub fovy: f32,
|
||||||
|
pub znear: f32,
|
||||||
|
pub zfar: f32,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Projection {
|
||||||
|
pub fn resize(&mut self, width: u32, height: u32) {
|
||||||
|
self.aspect = width as f32 / height as f32;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn get_matrix(&self) -> cgmath::Matrix4<f32> {
|
||||||
|
return cgmath::perspective(cgmath::Deg(self.fovy), self.aspect, self.znear, self.zfar);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Camera {
|
||||||
|
pub fn new(
|
||||||
|
position: cgmath::Point3<f32>,
|
||||||
|
pitch: f32,
|
||||||
|
yaw: f32,
|
||||||
|
fovy: f32,
|
||||||
|
aspect: f32,
|
||||||
|
) -> Self {
|
||||||
|
Self {
|
||||||
|
position: position,
|
||||||
|
pitch: pitch,
|
||||||
|
yaw: yaw,
|
||||||
|
projection: Projection {
|
||||||
|
aspect: aspect,
|
||||||
|
fovy: fovy,
|
||||||
|
znear: 0.01,
|
||||||
|
zfar: 1000.0,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn get_view_matrix(&self) -> cgmath::Matrix4<f32> {
|
||||||
|
let (forward, _right, up) = self.get_vecs();
|
||||||
|
return cgmath::Matrix4::look_to_rh(self.position, forward, up);
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn build_view_projection_matrix(&self) -> cgmath::Matrix4<f32> {
|
||||||
|
let view = self.get_view_matrix();
|
||||||
|
let proj = self.projection.get_matrix();
|
||||||
|
return proj * view;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn get_vecs(
|
||||||
|
&self,
|
||||||
|
) -> (
|
||||||
|
cgmath::Vector3<f32>,
|
||||||
|
cgmath::Vector3<f32>,
|
||||||
|
cgmath::Vector3<f32>,
|
||||||
|
) {
|
||||||
|
use cgmath::InnerSpace;
|
||||||
|
let (yaw_sin, yaw_cos) = cgmath::Rad::from(cgmath::Deg(self.yaw)).0.sin_cos();
|
||||||
|
let (pitch_sin, pitch_cos) = cgmath::Rad::from(cgmath::Deg(self.pitch)).0.sin_cos();
|
||||||
|
let forward =
|
||||||
|
cgmath::Vector3::new(pitch_cos * yaw_cos, pitch_sin, pitch_cos * yaw_sin).normalize();
|
||||||
|
let right = cgmath::Vector3::new(-yaw_sin, 0.0, yaw_cos).normalize();
|
||||||
|
let up = forward.cross(right);
|
||||||
|
return (forward, right, up);
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn update(&mut self, dt: Duration, controller: &CameraController) {
|
||||||
|
let dt = dt.as_secs_f32();
|
||||||
|
self.pitch = clamp(
|
||||||
|
self.pitch + controller.deltay * controller.sensitivity * 0.022,
|
||||||
|
-89.0,
|
||||||
|
89.0,
|
||||||
|
);
|
||||||
|
self.yaw -= controller.deltax * controller.sensitivity * 0.022;
|
||||||
|
self.yaw = self.yaw % 360.0;
|
||||||
|
if self.yaw < 0.0 {
|
||||||
|
self.yaw = 360.0 + self.yaw;
|
||||||
|
}
|
||||||
|
println!(
|
||||||
|
"pitch: {:.6}, yaw: {:.6}, dt: {:.6}",
|
||||||
|
self.pitch, self.yaw, dt
|
||||||
|
);
|
||||||
|
|
||||||
|
let (forward, right, up) = self.get_vecs();
|
||||||
|
self.position +=
|
||||||
|
forward * (controller.move_forward - controller.move_backward) * controller.speed * dt;
|
||||||
|
// FIXME -right
|
||||||
|
self.position +=
|
||||||
|
-right * (controller.move_right - controller.move_left) * controller.speed * dt;
|
||||||
|
self.position += up * (controller.move_up - controller.move_down) * controller.speed * dt;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[repr(C)]
|
||||||
|
#[derive(Debug, Copy, Clone, bytemuck::Pod, bytemuck::Zeroable)]
|
||||||
|
pub struct CameraUniform {
|
||||||
|
pub view_proj: [[f32; 4]; 4],
|
||||||
|
}
|
||||||
|
|
||||||
|
impl CameraUniform {
|
||||||
|
pub fn new() -> Self {
|
||||||
|
use cgmath::SquareMatrix;
|
||||||
|
Self {
|
||||||
|
view_proj: cgmath::Matrix4::identity().into(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn update_view_proj(&mut self, camera: &Camera) {
|
||||||
|
self.view_proj = camera.build_view_projection_matrix().into();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct CameraController {
|
||||||
|
pub speed: f32,
|
||||||
|
pub sensitivity: f32,
|
||||||
|
pub move_forward: f32,
|
||||||
|
pub move_backward: f32,
|
||||||
|
pub move_left: f32,
|
||||||
|
pub move_right: f32,
|
||||||
|
pub move_up: f32,
|
||||||
|
pub move_down: f32,
|
||||||
|
pub deltax: f32,
|
||||||
|
pub deltay: f32,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl CameraController {
|
||||||
|
pub fn new(speed: f32, sensitivity: f32) -> Self {
|
||||||
|
Self {
|
||||||
|
speed,
|
||||||
|
sensitivity,
|
||||||
|
move_forward: 0.0,
|
||||||
|
move_backward: 0.0,
|
||||||
|
move_left: 0.0,
|
||||||
|
move_right: 0.0,
|
||||||
|
move_up: 0.0,
|
||||||
|
move_down: 0.0,
|
||||||
|
deltax: 0.0,
|
||||||
|
deltay: 0.0,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn reset(&mut self, held_keys: bool) {
|
||||||
|
if held_keys {
|
||||||
|
self.move_forward = 0.0;
|
||||||
|
self.move_backward = 0.0;
|
||||||
|
self.move_left = 0.0;
|
||||||
|
self.move_right = 0.0;
|
||||||
|
self.move_up = 0.0;
|
||||||
|
self.move_down = 0.0;
|
||||||
|
}
|
||||||
|
self.deltax = 0.0;
|
||||||
|
self.deltay = 0.0;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn process_events(
|
||||||
|
&mut self,
|
||||||
|
window_event: Option<&WindowEvent>,
|
||||||
|
device_event: Option<&DeviceEvent>,
|
||||||
|
) -> bool {
|
||||||
|
let mut handled = match window_event {
|
||||||
|
None => false,
|
||||||
|
Some(event) => match event {
|
||||||
|
WindowEvent::KeyboardInput {
|
||||||
|
input:
|
||||||
|
KeyboardInput {
|
||||||
|
state,
|
||||||
|
virtual_keycode: Some(keycode),
|
||||||
|
..
|
||||||
|
},
|
||||||
|
..
|
||||||
|
} => {
|
||||||
|
let is_pressed = *state == ElementState::Pressed;
|
||||||
|
let amount = if is_pressed { 1.0 } else { 0.0 };
|
||||||
|
match keycode {
|
||||||
|
VirtualKeyCode::W | VirtualKeyCode::Up => {
|
||||||
|
self.move_forward = amount;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
VirtualKeyCode::A | VirtualKeyCode::Left => {
|
||||||
|
self.move_left = amount;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
VirtualKeyCode::S | VirtualKeyCode::Down => {
|
||||||
|
self.move_backward = amount;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
VirtualKeyCode::D | VirtualKeyCode::Right => {
|
||||||
|
self.move_right = amount;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
VirtualKeyCode::Space => {
|
||||||
|
self.move_up = amount;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
VirtualKeyCode::LControl => {
|
||||||
|
self.move_down = amount;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
_ => false,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
_ => false,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
if handled {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
handled = match device_event {
|
||||||
|
None => false,
|
||||||
|
Some(event) => match event {
|
||||||
|
DeviceEvent::MouseMotion { delta } => {
|
||||||
|
self.deltax += delta.0 as f32;
|
||||||
|
self.deltay += delta.1 as f32;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
_ => false,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
return handled;
|
||||||
|
}
|
||||||
|
}
|
60
src/core/instance.rs
Normal file
60
src/core/instance.rs
Normal file
|
@ -0,0 +1,60 @@
|
||||||
|
pub struct Instance {
|
||||||
|
pub position: cgmath::Vector3<f32>,
|
||||||
|
pub rotation: cgmath::Quaternion<f32>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[repr(C)]
|
||||||
|
#[derive(Copy, Clone, bytemuck::Pod, bytemuck::Zeroable)]
|
||||||
|
pub struct InstanceRaw {
|
||||||
|
pub model: [[f32; 4]; 4],
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Instance {
|
||||||
|
pub fn to_raw(&self) -> InstanceRaw {
|
||||||
|
return InstanceRaw {
|
||||||
|
model: (cgmath::Matrix4::from_translation(self.position)
|
||||||
|
* cgmath::Matrix4::from(self.rotation))
|
||||||
|
.into(),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl InstanceRaw {
|
||||||
|
pub fn desc<'a>() -> wgpu::VertexBufferLayout<'a> {
|
||||||
|
use std::mem;
|
||||||
|
wgpu::VertexBufferLayout {
|
||||||
|
array_stride: mem::size_of::<InstanceRaw>() as wgpu::BufferAddress,
|
||||||
|
// We need to switch from using a step mode of Vertex to Instance
|
||||||
|
// This means that our shaders will only change to use the next
|
||||||
|
// instance when the shader starts processing a new instance
|
||||||
|
step_mode: wgpu::VertexStepMode::Instance,
|
||||||
|
attributes: &[
|
||||||
|
wgpu::VertexAttribute {
|
||||||
|
offset: 0,
|
||||||
|
// While our vertex shader only uses locations 0, and 1 now, in later tutorials we'll
|
||||||
|
// be using 2, 3, and 4, for Vertex. We'll start at slot 5 not conflict with them later
|
||||||
|
shader_location: 5,
|
||||||
|
format: wgpu::VertexFormat::Float32x4,
|
||||||
|
},
|
||||||
|
// A mat4 takes up 4 vertex slots as it is technically 4 vec4s. We need to define a slot
|
||||||
|
// for each vec4. We'll have to reassemble the mat4 in
|
||||||
|
// the shader.
|
||||||
|
wgpu::VertexAttribute {
|
||||||
|
offset: mem::size_of::<[f32; 4]>() as wgpu::BufferAddress,
|
||||||
|
shader_location: 6,
|
||||||
|
format: wgpu::VertexFormat::Float32x4,
|
||||||
|
},
|
||||||
|
wgpu::VertexAttribute {
|
||||||
|
offset: mem::size_of::<[f32; 8]>() as wgpu::BufferAddress,
|
||||||
|
shader_location: 7,
|
||||||
|
format: wgpu::VertexFormat::Float32x4,
|
||||||
|
},
|
||||||
|
wgpu::VertexAttribute {
|
||||||
|
offset: mem::size_of::<[f32; 12]>() as wgpu::BufferAddress,
|
||||||
|
shader_location: 8,
|
||||||
|
format: wgpu::VertexFormat::Float32x4,
|
||||||
|
},
|
||||||
|
],
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
6
src/core/mod.rs
Normal file
6
src/core/mod.rs
Normal file
|
@ -0,0 +1,6 @@
|
||||||
|
pub mod camera;
|
||||||
|
pub mod instance;
|
||||||
|
pub mod state;
|
||||||
|
pub mod texture;
|
||||||
|
pub mod updater;
|
||||||
|
pub mod vertex;
|
|
@ -1,17 +1,44 @@
|
||||||
|
use cgmath::prelude::*;
|
||||||
|
use std::time::Duration;
|
||||||
|
|
||||||
use wgpu::{include_wgsl, util::DeviceExt};
|
use wgpu::{include_wgsl, util::DeviceExt};
|
||||||
use winit::{event::*, window::Window};
|
use winit::{event::*, window::Window};
|
||||||
|
|
||||||
use super::types::Vertex;
|
use super::camera::{Camera, CameraController, CameraUniform};
|
||||||
|
use super::instance::{Instance, InstanceRaw};
|
||||||
|
use super::texture::Texture;
|
||||||
|
use super::vertex::Vertex;
|
||||||
|
|
||||||
// test data
|
// test data
|
||||||
const VERTICES: &[Vertex] = &[
|
const VERTICES: &[Vertex] = &[
|
||||||
Vertex { position: [-0.0868241, 0.49240386, 0.0], tex_coords: [0.4131759, 0.99240386], }, // A
|
Vertex {
|
||||||
Vertex { position: [-0.49513406, 0.06958647, 0.0], tex_coords: [0.0048659444, 0.56958647], }, // B
|
position: [-0.0868241, 0.49240386, 0.0],
|
||||||
Vertex { position: [-0.21918549, -0.44939706, 0.0], tex_coords: [0.28081453, 0.05060294], }, // C
|
tex_coords: [0.4131759, 0.99240386],
|
||||||
Vertex { position: [0.35966998, -0.3473291, 0.0], tex_coords: [0.85967, 0.1526709], }, // D
|
}, // A
|
||||||
Vertex { position: [0.44147372, 0.2347359, 0.0], tex_coords: [0.9414737, 0.7347359], }, // E
|
Vertex {
|
||||||
|
position: [-0.49513406, 0.06958647, 0.0],
|
||||||
|
tex_coords: [0.0048659444, 0.56958647],
|
||||||
|
}, // B
|
||||||
|
Vertex {
|
||||||
|
position: [-0.21918549, -0.44939706, 0.0],
|
||||||
|
tex_coords: [0.28081453, 0.05060294],
|
||||||
|
}, // C
|
||||||
|
Vertex {
|
||||||
|
position: [0.35966998, -0.3473291, 0.0],
|
||||||
|
tex_coords: [0.85967, 0.1526709],
|
||||||
|
}, // D
|
||||||
|
Vertex {
|
||||||
|
position: [0.44147372, 0.2347359, 0.0],
|
||||||
|
tex_coords: [0.9414737, 0.7347359],
|
||||||
|
}, // E
|
||||||
];
|
];
|
||||||
const INDICES: &[u16] = &[0, 1, 4, 1, 2, 4, 2, 3, 4];
|
const INDICES: &[u16] = &[0, 1, 4, 1, 2, 4, 2, 3, 4];
|
||||||
|
const NUM_INSTANCES_PER_ROW: u32 = 10;
|
||||||
|
const INSTANCE_DISPLACEMENT: cgmath::Vector3<f32> = cgmath::Vector3::new(
|
||||||
|
NUM_INSTANCES_PER_ROW as f32 * 0.5,
|
||||||
|
0.0,
|
||||||
|
NUM_INSTANCES_PER_ROW as f32 * 0.5,
|
||||||
|
);
|
||||||
|
|
||||||
pub struct State {
|
pub struct State {
|
||||||
pub size: winit::dpi::PhysicalSize<u32>,
|
pub size: winit::dpi::PhysicalSize<u32>,
|
||||||
|
@ -25,6 +52,13 @@ pub struct State {
|
||||||
index_buffer: wgpu::Buffer,
|
index_buffer: wgpu::Buffer,
|
||||||
num_indices: u32,
|
num_indices: u32,
|
||||||
diffuse_bind_group: wgpu::BindGroup,
|
diffuse_bind_group: wgpu::BindGroup,
|
||||||
|
camera: Camera,
|
||||||
|
camera_uniform: CameraUniform,
|
||||||
|
camera_buffer: wgpu::Buffer,
|
||||||
|
camera_bind_group: wgpu::BindGroup,
|
||||||
|
camera_controller: CameraController,
|
||||||
|
instances: Vec<Instance>,
|
||||||
|
instance_buffer: wgpu::Buffer,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl State {
|
impl State {
|
||||||
|
@ -65,60 +99,54 @@ impl State {
|
||||||
|
|
||||||
surface.configure(&device, &config);
|
surface.configure(&device, &config);
|
||||||
|
|
||||||
// Test image
|
// Camera
|
||||||
let diffuse_bytes = include_bytes!("../../assets/test.png");
|
let camera = Camera::new(
|
||||||
let diffuse_image = image::load_from_memory(diffuse_bytes).unwrap();
|
(0.0, 0.0, 0.0).into(),
|
||||||
let diffuse_rgba = diffuse_image.to_rgba8();
|
0.0,
|
||||||
use image::GenericImageView;
|
0.0,
|
||||||
let dimensions = diffuse_image.dimensions();
|
60.0,
|
||||||
let texture_size = wgpu::Extent3d {
|
config.width as f32 / config.height as f32,
|
||||||
width: dimensions.0,
|
|
||||||
height: dimensions.1,
|
|
||||||
depth_or_array_layers: 1,
|
|
||||||
};
|
|
||||||
let diffuse_texture = device.create_texture(&wgpu::TextureDescriptor {
|
|
||||||
// All textures are stored as 3D, we represent our 2D texture
|
|
||||||
// by setting depth to 1.
|
|
||||||
size: texture_size,
|
|
||||||
mip_level_count: 1, // We'll talk about this a little later
|
|
||||||
sample_count: 1,
|
|
||||||
dimension: wgpu::TextureDimension::D2,
|
|
||||||
// Most images are stored using sRGB so we need to reflect that here.
|
|
||||||
format: wgpu::TextureFormat::Rgba8UnormSrgb,
|
|
||||||
// TEXTURE_BINDING tells wgpu that we want to use this texture in shaders
|
|
||||||
// COPY_DST means that we want to copy data to this texture
|
|
||||||
usage: wgpu::TextureUsages::TEXTURE_BINDING | wgpu::TextureUsages::COPY_DST,
|
|
||||||
label: Some("diffuse_texture"),
|
|
||||||
});
|
|
||||||
queue.write_texture(
|
|
||||||
// Tells wgpu where to copy the pixel data
|
|
||||||
wgpu::ImageCopyTexture {
|
|
||||||
texture: &diffuse_texture,
|
|
||||||
mip_level: 0,
|
|
||||||
origin: wgpu::Origin3d::ZERO,
|
|
||||||
aspect: wgpu::TextureAspect::All,
|
|
||||||
},
|
|
||||||
// The actual pixel data
|
|
||||||
&diffuse_rgba,
|
|
||||||
// The layout of the texture
|
|
||||||
wgpu::ImageDataLayout {
|
|
||||||
offset: 0,
|
|
||||||
bytes_per_row: std::num::NonZeroU32::new(4 * dimensions.0),
|
|
||||||
rows_per_image: std::num::NonZeroU32::new(dimensions.1),
|
|
||||||
},
|
|
||||||
texture_size,
|
|
||||||
);
|
);
|
||||||
let diffuse_texture_view =
|
|
||||||
diffuse_texture.create_view(&wgpu::TextureViewDescriptor::default());
|
let mut camera_uniform = CameraUniform::new();
|
||||||
let diffuse_sampler = device.create_sampler(&wgpu::SamplerDescriptor {
|
camera_uniform.update_view_proj(&camera);
|
||||||
address_mode_u: wgpu::AddressMode::Repeat,
|
|
||||||
address_mode_v: wgpu::AddressMode::Repeat,
|
let camera_buffer = device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
|
||||||
address_mode_w: wgpu::AddressMode::Repeat,
|
label: Some("Camera Buffer"),
|
||||||
mag_filter: wgpu::FilterMode::Linear,
|
contents: bytemuck::cast_slice(&[camera_uniform]),
|
||||||
min_filter: wgpu::FilterMode::Linear,
|
usage: wgpu::BufferUsages::UNIFORM | wgpu::BufferUsages::COPY_DST,
|
||||||
mipmap_filter: wgpu::FilterMode::Linear,
|
|
||||||
..Default::default()
|
|
||||||
});
|
});
|
||||||
|
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"),
|
||||||
|
});
|
||||||
|
|
||||||
|
let camera_controller = CameraController::new(1.0, 2.0);
|
||||||
|
|
||||||
|
// Test image
|
||||||
|
surface.configure(&device, &config);
|
||||||
|
let diffuse_bytes = include_bytes!("../../assets/test.png");
|
||||||
|
let diffuse_texture =
|
||||||
|
Texture::from_bytes(&device, &queue, diffuse_bytes, "../../assets/test.png").unwrap();
|
||||||
|
|
||||||
let texture_bind_group_layout =
|
let texture_bind_group_layout =
|
||||||
device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
|
device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
|
||||||
entries: &[
|
entries: &[
|
||||||
|
@ -148,16 +176,47 @@ impl State {
|
||||||
entries: &[
|
entries: &[
|
||||||
wgpu::BindGroupEntry {
|
wgpu::BindGroupEntry {
|
||||||
binding: 0,
|
binding: 0,
|
||||||
resource: wgpu::BindingResource::TextureView(&diffuse_texture_view),
|
resource: wgpu::BindingResource::TextureView(&diffuse_texture.view),
|
||||||
},
|
},
|
||||||
wgpu::BindGroupEntry {
|
wgpu::BindGroupEntry {
|
||||||
binding: 1,
|
binding: 1,
|
||||||
resource: wgpu::BindingResource::Sampler(&diffuse_sampler),
|
resource: wgpu::BindingResource::Sampler(&diffuse_texture.sampler),
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
label: Some("diffuse_bind_group"),
|
label: Some("diffuse_bind_group"),
|
||||||
});
|
});
|
||||||
|
|
||||||
|
let instances = (0..NUM_INSTANCES_PER_ROW)
|
||||||
|
.flat_map(|z| {
|
||||||
|
(0..NUM_INSTANCES_PER_ROW).map(move |x| {
|
||||||
|
let position = cgmath::Vector3 {
|
||||||
|
x: x as f32,
|
||||||
|
y: 0.0,
|
||||||
|
z: z as f32,
|
||||||
|
} - INSTANCE_DISPLACEMENT;
|
||||||
|
|
||||||
|
let rotation = if position.is_zero() {
|
||||||
|
// this is needed so an object at (0, 0, 0) won't get scaled to zero
|
||||||
|
// as Quaternions can effect scale if they're not created correctly
|
||||||
|
cgmath::Quaternion::from_axis_angle(
|
||||||
|
cgmath::Vector3::unit_z(),
|
||||||
|
cgmath::Deg(0.0),
|
||||||
|
)
|
||||||
|
} else {
|
||||||
|
cgmath::Quaternion::from_axis_angle(position.normalize(), cgmath::Deg(45.0))
|
||||||
|
};
|
||||||
|
|
||||||
|
Instance { position, rotation }
|
||||||
|
})
|
||||||
|
})
|
||||||
|
.collect::<Vec<_>>();
|
||||||
|
let instance_data = instances.iter().map(Instance::to_raw).collect::<Vec<_>>();
|
||||||
|
let instance_buffer = device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
|
||||||
|
label: Some("Instance Buffer"),
|
||||||
|
contents: bytemuck::cast_slice(&instance_data),
|
||||||
|
usage: wgpu::BufferUsages::VERTEX,
|
||||||
|
});
|
||||||
|
|
||||||
// let shader = device.create_shader_module(wgpu::ShaderModuleDescriptor {
|
// let shader = device.create_shader_module(wgpu::ShaderModuleDescriptor {
|
||||||
// label: Some("Shader"),
|
// label: Some("Shader"),
|
||||||
// source: wgpu::ShaderSource::Wgsl(include_str!("../shaders/test.wgsl").into()),
|
// source: wgpu::ShaderSource::Wgsl(include_str!("../shaders/test.wgsl").into()),
|
||||||
|
@ -167,7 +226,7 @@ impl State {
|
||||||
let render_pipeline_layout =
|
let render_pipeline_layout =
|
||||||
device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
|
device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
|
||||||
label: Some("Render Pipeline Layout"),
|
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: &[],
|
push_constant_ranges: &[],
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -177,7 +236,7 @@ impl State {
|
||||||
vertex: wgpu::VertexState {
|
vertex: wgpu::VertexState {
|
||||||
module: &shader,
|
module: &shader,
|
||||||
entry_point: "vs_main",
|
entry_point: "vs_main",
|
||||||
buffers: &[Vertex::desc()],
|
buffers: &[Vertex::desc(), InstanceRaw::desc()],
|
||||||
},
|
},
|
||||||
fragment: Some(wgpu::FragmentState {
|
fragment: Some(wgpu::FragmentState {
|
||||||
// 3.
|
// 3.
|
||||||
|
@ -234,6 +293,13 @@ impl State {
|
||||||
index_buffer,
|
index_buffer,
|
||||||
num_indices,
|
num_indices,
|
||||||
diffuse_bind_group,
|
diffuse_bind_group,
|
||||||
|
camera,
|
||||||
|
camera_uniform,
|
||||||
|
camera_buffer,
|
||||||
|
camera_bind_group,
|
||||||
|
camera_controller,
|
||||||
|
instances,
|
||||||
|
instance_buffer,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -243,14 +309,32 @@ impl State {
|
||||||
self.config.width = new_size.width;
|
self.config.width = new_size.width;
|
||||||
self.config.height = new_size.height;
|
self.config.height = new_size.height;
|
||||||
self.surface.configure(&self.device, &self.config);
|
self.surface.configure(&self.device, &self.config);
|
||||||
|
self.camera
|
||||||
|
.projection
|
||||||
|
.resize(new_size.width, new_size.height);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn input(&mut self, event: &WindowEvent) -> bool {
|
pub fn input(
|
||||||
return false;
|
&mut self,
|
||||||
|
window_event: Option<&WindowEvent>,
|
||||||
|
device_event: Option<&DeviceEvent>,
|
||||||
|
) -> bool {
|
||||||
|
return self
|
||||||
|
.camera_controller
|
||||||
|
.process_events(window_event, device_event);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn update(&mut self) {}
|
pub fn update(&mut self, dt: Duration) {
|
||||||
|
self.camera.update(dt, &self.camera_controller);
|
||||||
|
self.camera_controller.reset(false);
|
||||||
|
self.camera_uniform.update_view_proj(&self.camera);
|
||||||
|
self.queue.write_buffer(
|
||||||
|
&self.camera_buffer,
|
||||||
|
0,
|
||||||
|
bytemuck::cast_slice(&[self.camera_uniform]),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
pub fn render(&mut self) -> Result<(), wgpu::SurfaceError> {
|
pub fn render(&mut self) -> Result<(), wgpu::SurfaceError> {
|
||||||
let output = self.surface.get_current_texture()?;
|
let output = self.surface.get_current_texture()?;
|
||||||
|
@ -286,10 +370,15 @@ impl State {
|
||||||
});
|
});
|
||||||
|
|
||||||
render_pass.set_pipeline(&self.render_pipeline);
|
render_pass.set_pipeline(&self.render_pipeline);
|
||||||
|
|
||||||
render_pass.set_bind_group(0, &self.diffuse_bind_group, &[]);
|
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_vertex_buffer(0, self.vertex_buffer.slice(..));
|
||||||
|
render_pass.set_vertex_buffer(1, self.instance_buffer.slice(..));
|
||||||
render_pass.set_index_buffer(self.index_buffer.slice(..), wgpu::IndexFormat::Uint16);
|
render_pass.set_index_buffer(self.index_buffer.slice(..), wgpu::IndexFormat::Uint16);
|
||||||
render_pass.draw_indexed(0..self.num_indices, 0, 0..1);
|
|
||||||
|
render_pass.draw_indexed(0..self.num_indices, 0, 0..self.instances.len() as _);
|
||||||
}
|
}
|
||||||
|
|
||||||
// submit will accept anything that implements IntoIter
|
// submit will accept anything that implements IntoIter
|
78
src/core/texture.rs
Normal file
78
src/core/texture.rs
Normal file
|
@ -0,0 +1,78 @@
|
||||||
|
use anyhow::*;
|
||||||
|
use image::GenericImageView;
|
||||||
|
|
||||||
|
pub struct Texture {
|
||||||
|
pub texture: wgpu::Texture,
|
||||||
|
pub view: wgpu::TextureView,
|
||||||
|
pub sampler: wgpu::Sampler,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Texture {
|
||||||
|
pub fn from_bytes(
|
||||||
|
device: &wgpu::Device,
|
||||||
|
queue: &wgpu::Queue,
|
||||||
|
bytes: &[u8],
|
||||||
|
label: &str,
|
||||||
|
) -> Result<Self> {
|
||||||
|
let img = image::load_from_memory(bytes)?;
|
||||||
|
Self::from_image(device, queue, &img, Some(label))
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn from_image(
|
||||||
|
device: &wgpu::Device,
|
||||||
|
queue: &wgpu::Queue,
|
||||||
|
img: &image::DynamicImage,
|
||||||
|
label: Option<&str>,
|
||||||
|
) -> Result<Self> {
|
||||||
|
let rgba = img.to_rgba8();
|
||||||
|
let dimensions = img.dimensions();
|
||||||
|
|
||||||
|
let size = wgpu::Extent3d {
|
||||||
|
width: dimensions.0,
|
||||||
|
height: dimensions.1,
|
||||||
|
depth_or_array_layers: 1,
|
||||||
|
};
|
||||||
|
let texture = device.create_texture(&wgpu::TextureDescriptor {
|
||||||
|
label,
|
||||||
|
size,
|
||||||
|
mip_level_count: 1,
|
||||||
|
sample_count: 1,
|
||||||
|
dimension: wgpu::TextureDimension::D2,
|
||||||
|
format: wgpu::TextureFormat::Rgba8UnormSrgb,
|
||||||
|
usage: wgpu::TextureUsages::TEXTURE_BINDING | wgpu::TextureUsages::COPY_DST,
|
||||||
|
});
|
||||||
|
|
||||||
|
queue.write_texture(
|
||||||
|
wgpu::ImageCopyTexture {
|
||||||
|
aspect: wgpu::TextureAspect::All,
|
||||||
|
texture: &texture,
|
||||||
|
mip_level: 0,
|
||||||
|
origin: wgpu::Origin3d::ZERO,
|
||||||
|
},
|
||||||
|
&rgba,
|
||||||
|
wgpu::ImageDataLayout {
|
||||||
|
offset: 0,
|
||||||
|
bytes_per_row: std::num::NonZeroU32::new(4 * dimensions.0),
|
||||||
|
rows_per_image: std::num::NonZeroU32::new(dimensions.1),
|
||||||
|
},
|
||||||
|
size,
|
||||||
|
);
|
||||||
|
|
||||||
|
let view = texture.create_view(&wgpu::TextureViewDescriptor::default());
|
||||||
|
let sampler = device.create_sampler(&wgpu::SamplerDescriptor {
|
||||||
|
address_mode_u: wgpu::AddressMode::ClampToEdge,
|
||||||
|
address_mode_v: wgpu::AddressMode::ClampToEdge,
|
||||||
|
address_mode_w: wgpu::AddressMode::ClampToEdge,
|
||||||
|
mag_filter: wgpu::FilterMode::Linear,
|
||||||
|
min_filter: wgpu::FilterMode::Nearest,
|
||||||
|
mipmap_filter: wgpu::FilterMode::Nearest,
|
||||||
|
..Default::default()
|
||||||
|
});
|
||||||
|
|
||||||
|
return Ok(Self {
|
||||||
|
texture,
|
||||||
|
view,
|
||||||
|
sampler,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,3 +1,5 @@
|
||||||
|
use std::time::Instant;
|
||||||
|
|
||||||
use super::state::State;
|
use super::state::State;
|
||||||
use winit::{
|
use winit::{
|
||||||
event::*,
|
event::*,
|
||||||
|
@ -9,16 +11,19 @@ pub async fn run() {
|
||||||
let event_loop = EventLoop::new();
|
let event_loop = EventLoop::new();
|
||||||
let window = WindowBuilder::new().build(&event_loop).unwrap();
|
let window = WindowBuilder::new().build(&event_loop).unwrap();
|
||||||
let mut state = State::new(&window).await;
|
let mut state = State::new(&window).await;
|
||||||
|
let mut last_render = Instant::now();
|
||||||
|
|
||||||
// Event loop
|
// Event loop
|
||||||
event_loop.run(move |event, _, control_flow| {
|
event_loop.run(move |event, _, control_flow| {
|
||||||
match event {
|
match event {
|
||||||
|
Event::DeviceEvent { ref event, .. } => {
|
||||||
|
state.input(None, Some(event));
|
||||||
|
}
|
||||||
Event::WindowEvent {
|
Event::WindowEvent {
|
||||||
ref event,
|
ref event,
|
||||||
window_id,
|
window_id,
|
||||||
} if window_id == window.id() => {
|
} if window_id == window.id() => {
|
||||||
if !state.input(event) {
|
if !state.input(Some(event), None) {
|
||||||
// UPDATED!
|
|
||||||
match event {
|
match event {
|
||||||
WindowEvent::CloseRequested
|
WindowEvent::CloseRequested
|
||||||
| WindowEvent::KeyboardInput {
|
| WindowEvent::KeyboardInput {
|
||||||
|
@ -36,13 +41,15 @@ pub async fn run() {
|
||||||
WindowEvent::ScaleFactorChanged { new_inner_size, .. } => {
|
WindowEvent::ScaleFactorChanged { new_inner_size, .. } => {
|
||||||
state.resize(**new_inner_size);
|
state.resize(**new_inner_size);
|
||||||
}
|
}
|
||||||
WindowEvent::CursorMoved { position, .. } => {}
|
|
||||||
_ => {}
|
_ => {}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Event::RedrawRequested(window_id) if window_id == window.id() => {
|
Event::RedrawRequested(window_id) if window_id == window.id() => {
|
||||||
state.update();
|
let now = Instant::now();
|
||||||
|
let dt = now - last_render;
|
||||||
|
last_render = now;
|
||||||
|
state.update(dt);
|
||||||
match state.render() {
|
match state.render() {
|
||||||
Ok(_) => {}
|
Ok(_) => {}
|
||||||
// Reconfigure the surface if lost
|
// Reconfigure the surface if lost
|
|
@ -1,7 +1,6 @@
|
||||||
mod surf;
|
mod core;
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
env_logger::init();
|
env_logger::init();
|
||||||
pollster::block_on(surf::updater::run());
|
pollster::block_on(core::updater::run());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,5 +1,18 @@
|
||||||
|
struct InstanceInput {
|
||||||
|
@location(5) model_matrix_0: vec4<f32>,
|
||||||
|
@location(6) model_matrix_1: vec4<f32>,
|
||||||
|
@location(7) model_matrix_2: vec4<f32>,
|
||||||
|
@location(8) model_matrix_3: vec4<f32>,
|
||||||
|
};
|
||||||
|
|
||||||
// Vertex shader
|
// Vertex shader
|
||||||
|
|
||||||
|
struct CameraUniform {
|
||||||
|
view_proj: mat4x4<f32>,
|
||||||
|
};
|
||||||
|
@group(1) @binding(0)
|
||||||
|
var<uniform> camera: CameraUniform;
|
||||||
|
|
||||||
struct VertexInput {
|
struct VertexInput {
|
||||||
@location(0) position: vec3<f32>,
|
@location(0) position: vec3<f32>,
|
||||||
@location(1) tex_coords: vec2<f32>,
|
@location(1) tex_coords: vec2<f32>,
|
||||||
|
@ -13,10 +26,17 @@ struct VertexOutput {
|
||||||
@vertex
|
@vertex
|
||||||
fn vs_main(
|
fn vs_main(
|
||||||
model: VertexInput,
|
model: VertexInput,
|
||||||
|
instance: InstanceInput,
|
||||||
) -> VertexOutput {
|
) -> VertexOutput {
|
||||||
|
let model_matrix = mat4x4<f32>(
|
||||||
|
instance.model_matrix_0,
|
||||||
|
instance.model_matrix_1,
|
||||||
|
instance.model_matrix_2,
|
||||||
|
instance.model_matrix_3,
|
||||||
|
);
|
||||||
var out: VertexOutput;
|
var out: VertexOutput;
|
||||||
out.tex_coords = model.tex_coords;
|
out.tex_coords = model.tex_coords;
|
||||||
out.clip_position = vec4<f32>(model.position, 1.0);
|
out.clip_position = camera.view_proj * model_matrix * vec4<f32>(model.position, 1.0);
|
||||||
return out;
|
return out;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,3 +0,0 @@
|
||||||
pub mod state;
|
|
||||||
pub mod updater;
|
|
||||||
pub mod types;
|
|
Loading…
Add table
Add a link
Reference in a new issue