You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
learn-wgpu/code/showcase/framework/src/light.rs

41 lines
1.1 KiB
Rust

use cgmath::*;
use wgpu::util::{BufferInitDescriptor, DeviceExt};
#[repr(C)]
#[derive(Debug, Copy, Clone)]
pub struct LightData {
pub position: Vector4<f32>,
pub color: Vector4<f32>,
}
unsafe impl bytemuck::Pod for LightData {}
unsafe impl bytemuck::Zeroable for LightData {}
pub struct Light {
#[allow(dead_code)]
data: LightData,
#[allow(dead_code)]
buffer: wgpu::Buffer,
}
impl Light {
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),
color: Vector4::new(color.x, color.y, color.z, 1.0),
};
let buffer = device.create_buffer_init(&BufferInitDescriptor {
contents: bytemuck::cast_slice(&[data]),
usage: wgpu::BufferUsage::COPY_DST | wgpu::BufferUsage::UNIFORM,
label: Some("Light Buffer"),
});
Self { data, buffer }
}
}
pub struct LightBinding {
pub layout: wgpu::BindGroupLayout,
pub bind_group: wgpu::BindGroup,
}