Renamed "light" to "light_uniform"

pull/224/head
Martino Fontana 3 years ago
parent b2b77294f6
commit 463b6117eb

@ -242,7 +242,7 @@ impl model::Vertex for InstanceRaw {
#[repr(C)]
#[derive(Debug, Copy, Clone, bytemuck::Pod, bytemuck::Zeroable)]
struct Light {
struct LightUniform {
position: [f32; 3],
// Due to uniforms requiring 16 byte (4 float) spacing, we need to use a padding field here
_padding: u32,
@ -267,7 +267,7 @@ struct State {
instance_buffer: wgpu::Buffer,
depth_texture: texture::Texture,
size: winit::dpi::PhysicalSize<u32>,
light: Light,
light_uniform: LightUniform,
light_buffer: wgpu::Buffer,
light_bind_group: wgpu::BindGroup,
light_render_pipeline: wgpu::RenderPipeline,
@ -477,7 +477,7 @@ impl State {
)
.unwrap();
let light = Light {
let light_uniform = LightUniform {
position: [2.0, 2.0, 2.0],
_padding: 0,
color: [1.0, 1.0, 1.0],
@ -485,7 +485,7 @@ impl State {
let light_buffer = device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
label: Some("Light VB"),
contents: bytemuck::cast_slice(&[light]),
contents: bytemuck::cast_slice(&[light_uniform]),
usage: wgpu::BufferUsage::UNIFORM | wgpu::BufferUsage::COPY_DST,
});
@ -581,7 +581,7 @@ impl State {
instance_buffer,
depth_texture,
size,
light,
light_uniform,
light_buffer,
light_bind_group,
light_render_pipeline,
@ -617,13 +617,13 @@ impl State {
);
// Update the light
let old_position: cgmath::Vector3<_> = self.light.position.into();
self.light.position =
let old_position: cgmath::Vector3<_> = self.light_uniform.position.into();
self.light_uniform.position =
(cgmath::Quaternion::from_axis_angle((0.0, 1.0, 0.0).into(), cgmath::Deg(1.0))
* old_position)
.into();
self.queue
.write_buffer(&self.light_buffer, 0, bytemuck::cast_slice(&[self.light]));
.write_buffer(&self.light_buffer, 0, bytemuck::cast_slice(&[self.light_uniform]));
}
fn render(&mut self) -> Result<(), wgpu::SwapChainError> {

@ -241,7 +241,7 @@ impl model::Vertex for InstanceRaw {
#[repr(C)]
#[derive(Debug, Copy, Clone, bytemuck::Pod, bytemuck::Zeroable)]
struct Light {
struct LightUniform {
position: [f32; 3],
// Due to uniforms requiring 16 byte (4 float) spacing, we need to use a padding field here
_padding: u32,
@ -266,7 +266,7 @@ struct State {
instance_buffer: wgpu::Buffer,
depth_texture: texture::Texture,
size: winit::dpi::PhysicalSize<u32>,
light: Light,
light_uniform: LightUniform,
light_buffer: wgpu::Buffer,
light_bind_group: wgpu::BindGroup,
light_render_pipeline: wgpu::RenderPipeline,
@ -498,7 +498,7 @@ impl State {
)
.unwrap();
let light = Light {
let light_uniform = LightUniform {
position: [2.0, 2.0, 2.0],
_padding: 0,
color: [1.0, 1.0, 1.0],
@ -506,7 +506,7 @@ impl State {
let light_buffer = device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
label: Some("Light VB"),
contents: bytemuck::cast_slice(&[light]),
contents: bytemuck::cast_slice(&[light_uniform]),
usage: wgpu::BufferUsage::UNIFORM | wgpu::BufferUsage::COPY_DST,
});
@ -632,7 +632,7 @@ impl State {
instance_buffer,
depth_texture,
size,
light,
light_uniform,
light_buffer,
light_bind_group,
light_render_pipeline,
@ -670,13 +670,16 @@ impl State {
);
// Update the light
let old_position: cgmath::Vector3<_> = self.light.position.into();
self.light.position =
let old_position: cgmath::Vector3<_> = self.light_uniform.position.into();
self.light_uniform.position =
(cgmath::Quaternion::from_axis_angle((0.0, 1.0, 0.0).into(), cgmath::Deg(1.0))
* old_position)
.into();
self.queue
.write_buffer(&self.light_buffer, 0, bytemuck::cast_slice(&[self.light]));
self.queue.write_buffer(
&self.light_buffer,
0,
bytemuck::cast_slice(&[self.light_uniform]),
);
}
fn render(&mut self) -> Result<(), wgpu::SwapChainError> {

@ -118,7 +118,7 @@ impl model::Vertex for InstanceRaw {
#[repr(C)]
#[derive(Debug, Copy, Clone, bytemuck::Pod, bytemuck::Zeroable)]
struct Light {
struct LightUniform {
position: [f32; 3],
// Due to uniforms requiring 16 byte (4 float) spacing, we need to use a padding field here
_padding: u32,
@ -144,7 +144,7 @@ struct State {
instance_buffer: wgpu::Buffer,
depth_texture: texture::Texture,
size: winit::dpi::PhysicalSize<u32>,
light: Light,
light_uniform: LightUniform,
light_buffer: wgpu::Buffer,
light_bind_group: wgpu::BindGroup,
light_render_pipeline: wgpu::RenderPipeline,
@ -374,7 +374,7 @@ impl State {
.unwrap();
println!("Elapsed (Original): {:?}", std::time::Instant::now() - now);
let light = Light {
let light_uniform = LightUniform {
position: [2.0, 2.0, 2.0],
_padding: 0,
color: [1.0, 1.0, 1.0],
@ -382,7 +382,7 @@ impl State {
let light_buffer = device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
label: Some("Light VB"),
contents: bytemuck::cast_slice(&[light]),
contents: bytemuck::cast_slice(&[light_uniform]),
usage: wgpu::BufferUsage::UNIFORM | wgpu::BufferUsage::COPY_DST,
});
@ -509,7 +509,7 @@ impl State {
instance_buffer,
depth_texture,
size,
light,
light_uniform,
light_buffer,
light_bind_group,
light_render_pipeline,
@ -577,13 +577,16 @@ impl State {
);
// Update the light
let old_position: cgmath::Vector3<_> = self.light.position.into();
self.light.position =
let old_position: cgmath::Vector3<_> = self.light_uniform.position.into();
self.light_uniform.position =
(cgmath::Quaternion::from_axis_angle((0.0, 1.0, 0.0).into(), cgmath::Deg(1.0))
* old_position)
.into();
self.queue
.write_buffer(&self.light_buffer, 0, bytemuck::cast_slice(&[self.light]));
self.queue.write_buffer(
&self.light_buffer,
0,
bytemuck::cast_slice(&[self.light_uniform]),
);
}
fn render(&mut self) -> Result<(), wgpu::SwapChainError> {

@ -118,7 +118,7 @@ impl model::Vertex for InstanceRaw {
#[repr(C)]
#[derive(Debug, Copy, Clone, bytemuck::Pod, bytemuck::Zeroable)]
struct Light {
struct LightUniform {
position: [f32; 3],
// Due to uniforms requiring 16 byte (4 float) spacing, we need to use a padding field here
_padding: u32,
@ -144,7 +144,7 @@ struct State {
instance_buffer: wgpu::Buffer,
depth_texture: texture::Texture,
size: winit::dpi::PhysicalSize<u32>,
light: Light,
light_uniform: LightUniform,
light_buffer: wgpu::Buffer,
light_bind_group: wgpu::BindGroup,
light_render_pipeline: wgpu::RenderPipeline,
@ -373,7 +373,7 @@ impl State {
)
.unwrap();
let light = Light {
let light_uniform = LightUniform {
position: [2.0, 2.0, 2.0],
_padding: 0,
color: [1.0, 1.0, 1.0],
@ -381,7 +381,7 @@ impl State {
let light_buffer = device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
label: Some("Light VB"),
contents: bytemuck::cast_slice(&[light]),
contents: bytemuck::cast_slice(&[light_uniform]),
usage: wgpu::BufferUsage::UNIFORM | wgpu::BufferUsage::COPY_DST,
});
@ -508,7 +508,7 @@ impl State {
instance_buffer,
depth_texture,
size,
light,
light_uniform,
light_buffer,
light_bind_group,
light_render_pipeline,
@ -572,13 +572,16 @@ impl State {
);
// Update the light
let old_position: cgmath::Vector3<_> = self.light.position.into();
self.light.position =
let old_position: cgmath::Vector3<_> = self.light_uniform.position.into();
self.light_uniform.position =
(cgmath::Quaternion::from_axis_angle((0.0, 1.0, 0.0).into(), cgmath::Deg(1.0))
* old_position)
.into();
self.queue
.write_buffer(&self.light_buffer, 0, bytemuck::cast_slice(&[self.light]));
self.queue.write_buffer(
&self.light_buffer,
0,
bytemuck::cast_slice(&[self.light_uniform]),
);
}
fn render(&mut self) -> Result<(), wgpu::SwapChainError> {

@ -104,7 +104,7 @@ impl model::Vertex for InstanceRaw {
#[repr(C)]
#[derive(Debug, Copy, Clone, bytemuck::Pod, bytemuck::Zeroable)]
struct Light {
struct LightUniform {
position: [f32; 3],
// Due to uniforms requiring 16 byte (4 float) spacing, we need to use a padding field here
_padding: u32,
@ -130,7 +130,7 @@ struct State {
instance_buffer: wgpu::Buffer,
depth_texture: texture::Texture,
size: winit::dpi::PhysicalSize<u32>,
light: Light,
light_uniform: LightUniform,
light_buffer: wgpu::Buffer,
light_bind_group: wgpu::BindGroup,
light_render_pipeline: wgpu::RenderPipeline,
@ -308,7 +308,7 @@ impl State {
)
.unwrap();
let light = Light {
let light_uniform = LightUniform {
position: [2.0, 2.0, 2.0],
_padding: 0,
color: [1.0, 1.0, 1.0],
@ -316,7 +316,7 @@ impl State {
let light_buffer = device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
label: Some("Light VB"),
contents: bytemuck::cast_slice(&[light]),
contents: bytemuck::cast_slice(&[light_uniform]),
usage: wgpu::BufferUsage::UNIFORM | wgpu::BufferUsage::COPY_DST,
});
@ -434,7 +434,7 @@ impl State {
instance_buffer,
depth_texture,
size,
light,
light_uniform,
light_buffer,
light_bind_group,
light_render_pipeline,
@ -507,13 +507,16 @@ impl State {
);
// Update the light
let old_position: cgmath::Vector3<_> = self.light.position.into();
self.light.position =
let old_position: cgmath::Vector3<_> = self.light_uniform.position.into();
self.light_uniform.position =
(cgmath::Quaternion::from_axis_angle((0.0, 1.0, 0.0).into(), cgmath::Deg(1.0))
* old_position)
.into();
self.queue
.write_buffer(&self.light_buffer, 0, bytemuck::cast_slice(&[self.light]));
self.queue.write_buffer(
&self.light_buffer,
0,
bytemuck::cast_slice(&[self.light_uniform]),
);
}
fn render(&mut self) {

@ -11,14 +11,14 @@ pub struct LightData {
unsafe impl bytemuck::Pod for LightData {}
unsafe impl bytemuck::Zeroable for LightData {}
pub struct Light {
pub struct LightUniform {
#[allow(dead_code)]
data: LightData,
#[allow(dead_code)]
buffer: wgpu::Buffer,
}
impl Light {
impl LightUniform {
pub fn new(device: &wgpu::Device, position: Vector3<f32>, color: Vector3<f32>) -> Self {
let data = LightData {
position: Vector4::new(position.x, position.y, position.z, 1.0),

@ -22,7 +22,7 @@ Before we can get into that though, we need to add a light to our scene.
// main.rs
#[repr(C)]
#[derive(Debug, Copy, Clone, bytemuck::Pod, bytemuck::Zeroable)]
struct Light {
struct LightUniform {
position: [f32; 3],
// Due to uniforms requiring 16 byte (4 float) spacing, we need to use a padding field here
_padding: u32,
@ -30,12 +30,12 @@ struct Light {
}
```
Our `Light` represents a colored point in space. We're just going to use pure white light, but it's good to allow different colors of light.
Our `LightUniform` represents a colored point in space. We're just going to use pure white light, but it's good to allow different colors of light.
We're going to create another buffer to store our light in.
```rust
let light = Light {
let light_uniform = LightUniform {
position: [2.0, 2.0, 2.0],
_padding: 0,
color: [1.0, 1.0, 1.0],
@ -45,13 +45,13 @@ let light = Light {
let light_buffer = device.create_buffer_init(
&wgpu::util::BufferInitDescriptor {
label: Some("Light VB"),
contents: bytemuck::cast_slice(&[light]),
contents: bytemuck::cast_slice(&[light_uniform]),
usage: wgpu::BufferUsage::UNIFORM | wgpu::BufferUsage::COPY_DST,
}
);
```
Don't forget to add the `light` and `light_buffer` to `State`. After that we need to create a bind group layout and bind group for our light.
Don't forget to add the `light_uniform` and `light_buffer` to `State`. After that we need to create a bind group layout and bind group for our light.
```rust
let light_bind_group_layout =
@ -95,11 +95,11 @@ Let's also update the lights position in the `update()` method, so we can see wh
```rust
// Update the light
let old_position: cgmath::Vector3<_> = self.light.position.into();
self.light.position =
let old_position: cgmath::Vector3<_> = self.light_uniform.position.into();
self.light_uniform.position =
cgmath::Quaternion::from_axis_angle((0.0, 1.0, 0.0).into(), cgmath::Deg(1.0))
* old_position;
self.queue.write_buffer(&self.light_buffer, 0, bytemuck::cast_slice(&[self.light]));
self.queue.write_buffer(&self.light_buffer, 0, bytemuck::cast_slice(&[self.light_uniform]));
```
This will have the light rotate around the origin one degree every frame.

@ -412,7 +412,7 @@ fn update(&mut self, dt: std::time::Duration) {
While we're at it, let's use `dt` for the light's rotation as well.
```rust
self.light.position =
self.light_uniform.position =
cgmath::Quaternion::from_axis_angle((0.0, 1.0, 0.0).into(), cgmath::Deg(60.0 * dt.as_secs_f32()))
* old_position; // UPDATED!
```

Loading…
Cancel
Save