1 use bytemuck::__core::ops::Range; 2 use bytemuck::{Zeroable, Pod}; 3 use cgmath::Point3; 4 use std::sync::Arc; 5 use wgpu::TextureView; 6 use crate::components::{RangeCopy, Position}; 7 use crate::render::OPENGL_TO_WGPU_MATRIX; 8 9 10 #[repr(C)] 11 #[derive(Clone, Copy)] 12 pub struct LightRaw { 13 proj: [[f32; 4]; 4], 14 pos: [f32; 4], 15 color: [f32; 4], 16 } 17 18 unsafe impl Pod for LightRaw {} 19 20 unsafe impl Zeroable for LightRaw {} 21 22 #[derive(Clone, Debug)] 23 pub struct DirectionalLight { 24 pub color: wgpu::Color, 25 pub fov: f32, 26 pub depth: RangeCopy<f32>, 27 pub target_view: Arc<TextureView>, 28 } 29 30 impl DirectionalLight { 31 pub fn to_raw(&self, pos: &Position) -> LightRaw { 32 use cgmath::{Deg, EuclideanSpace, Matrix4, PerspectiveFov, Point3, Vector3}; 33 34 let point3d = Point3::new(pos.x, pos.y, pos.z); 35 let point3d_2 = Point3::new(pos.x, pos.y - 1.0, pos.z); 36 let mx_view = Matrix4::look_at(point3d, point3d_2, Vector3::unit_z()); 37 38 let projection = PerspectiveFov { 39 fovy: Deg(self.fov).into(), 40 aspect: 1.0, 41 near: self.depth.start, 42 far: self.depth.end, 43 }; 44 let mx_correction = OPENGL_TO_WGPU_MATRIX; 45 let mx_view_proj = 46 mx_correction * cgmath::Matrix4::from(projection.to_perspective()) * mx_view; 47 LightRaw { 48 proj: *mx_view_proj.as_ref(), 49 pos: [pos.x, pos.y, pos.z, 1.0], 50 color: [ 51 self.color.r as f32, 52 self.color.g as f32, 53 self.color.b as f32, 54 1.0, 55 ], 56 } 57 } 58 }