Skip to content

Commit

Permalink
Pass View and Proj Matrices Separetely
Browse files Browse the repository at this point in the history
  • Loading branch information
qlurkin committed Feb 22, 2024
1 parent 7f0ee9a commit 0b32af6
Show file tree
Hide file tree
Showing 8 changed files with 31 additions and 19 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "wgpu-bootstrap"
version = "0.2.1"
version = "0.2.2"
edition = "2021"

[dependencies]
Expand Down
5 changes: 3 additions & 2 deletions examples/cube/shader.wgsl
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
struct CameraUniform {
view_proj: mat4x4<f32>,
view: mat4x4<f32>,
proj: mat4x4<f32>,
};
@group(0) @binding(0) var<uniform> camera: CameraUniform;

Expand All @@ -19,7 +20,7 @@ fn vs_main(
) -> VertexOutput {
var out: VertexOutput;
out.color = model.color;
out.clip_position = camera.view_proj * vec4<f32>(model.position, 1.0);
out.clip_position = camera.proj * camera.view * vec4<f32>(model.position, 1.0);
return out;
}

Expand Down
5 changes: 3 additions & 2 deletions examples/gui/shader.wgsl
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
struct CameraUniform {
view_proj: mat4x4<f32>,
view: mat4x4<f32>,
proj: mat4x4<f32>,
};
@group(0) @binding(0) var<uniform> camera: CameraUniform;

Expand All @@ -19,7 +20,7 @@ fn vs_main(
) -> VertexOutput {
var out: VertexOutput;
out.color = model.color;
out.clip_position = camera.view_proj * vec4<f32>(model.position, 1.0);
out.clip_position = camera.proj * camera.view * vec4<f32>(model.position, 1.0);
return out;
}

Expand Down
5 changes: 3 additions & 2 deletions examples/instances/shader.wgsl
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
struct CameraUniform {
view_proj: mat4x4<f32>,
view: mat4x4<f32>,
proj: mat4x4<f32>,
};
@group(0) @binding(0) var<uniform> camera: CameraUniform;

Expand All @@ -24,7 +25,7 @@ fn vs_main(
) -> VertexOutput {
var out: VertexOutput;
out.color = model.color;
out.clip_position = camera.view_proj * vec4<f32>(model.position + instance.pos, 1.0);
out.clip_position = camera.proj * camera.view * vec4<f32>(model.position + instance.pos, 1.0);
return out;
}

Expand Down
5 changes: 3 additions & 2 deletions examples/shading/shader.wgsl
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
struct CameraUniform {
view_proj: mat4x4<f32>,
view: mat4x4<f32>,
proj: mat4x4<f32>,
};
@group(0) @binding(0) var<uniform> camera: CameraUniform;

Expand All @@ -24,7 +25,7 @@ fn vs_main(
out.position = model.position;
out.normal = model.normal;
out.color = model.color;
out.clip_position = camera.view_proj * vec4<f32>(model.position, 1.0);
out.clip_position = camera.proj * camera.view * vec4<f32>(model.position, 1.0);
return out;
}

Expand Down
5 changes: 3 additions & 2 deletions examples/wireframe/shader.wgsl
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
struct CameraUniform {
view_proj: mat4x4<f32>,
view: mat4x4<f32>,
proj: mat4x4<f32>,
};
@group(0) @binding(0) var<uniform> camera: CameraUniform;

Expand All @@ -19,7 +20,7 @@ fn vs_main(
) -> VertexOutput {
var out: VertexOutput;
out.color = model.color;
out.clip_position = camera.view_proj * vec4<f32>(model.position, 1.0);
out.clip_position = camera.proj * camera.view * vec4<f32>(model.position, 1.0);
return out;
}

Expand Down
21 changes: 14 additions & 7 deletions src/util/orbit_camera.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,25 +19,31 @@ pub const OPENGL_TO_WGPU_MATRIX: cgmath::Matrix4<f32> = cgmath::Matrix4::new(
pub struct CameraUniform {
// We can't use cgmath with bytemuck directly so we'll have
// to convert the Matrix4 into a 4x4 f32 array
view_proj: [[f32; 4]; 4],
view: [[f32; 4]; 4],
proj: [[f32; 4]; 4],
}

impl CameraUniform {
pub fn new() -> Self {
Self {
view_proj: cgmath::Matrix4::identity().into(),
view: cgmath::Matrix4::identity().into(),
proj: cgmath::Matrix4::identity().into(),
}
}

pub fn update_view_proj(&mut self, matrix: cgmath::Matrix4<f32>) {
self.view_proj = matrix.into();
pub fn update_proj(&mut self, matrix: cgmath::Matrix4<f32>) {
self.proj = matrix.into();
}

pub fn update_view(&mut self, matrix: cgmath::Matrix4<f32>) {
self.view = matrix.into();
}

pub fn desc() -> wgpu::BindGroupLayoutDescriptor<'static> {
wgpu::BindGroupLayoutDescriptor {
entries: &[wgpu::BindGroupLayoutEntry {
binding: 0,
visibility: wgpu::ShaderStages::VERTEX,
visibility: wgpu::ShaderStages::VERTEX_FRAGMENT,
ty: wgpu::BindingType::Buffer {
ty: wgpu::BufferBindingType::Uniform,
has_dynamic_offset: false,
Expand Down Expand Up @@ -123,8 +129,9 @@ impl OrbitCamera {
let pos = cgmath::Point3::from_vec(pos.to_vec() + self.target.to_vec());

let view = cgmath::Matrix4::look_at_rh(pos, self.target, self.up);
let projection_matrix = OPENGL_TO_WGPU_MATRIX * proj * view;
self.uniform.update_view_proj(projection_matrix);
let projection_matrix = OPENGL_TO_WGPU_MATRIX * proj;
self.uniform.update_proj(projection_matrix);
self.uniform.update_view(view);
context
.queue()
.write_buffer(&self.buffer, 0, bytemuck::cast_slice(&[self.uniform]));
Expand Down

0 comments on commit 0b32af6

Please sign in to comment.