You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

58 lines
1.5 KiB

use bytemuck::__core::ops::Range;
use bytemuck::{Zeroable, Pod};
use cgmath::Point3;
use std::sync::Arc;
use wgpu::TextureView;
use crate::components::{RangeCopy, Position};
use crate::render::OPENGL_TO_WGPU_MATRIX;
#[repr(C)]
#[derive(Clone, Copy)]
4 years ago
pub struct LightRaw {
proj: [[f32; 4]; 4],
pos: [f32; 4],
color: [f32; 4],
}
unsafe impl Pod for LightRaw {}
unsafe impl Zeroable for LightRaw {}
#[derive(Clone, Debug)]
pub struct DirectionalLight {
pub color: wgpu::Color,
pub fov: f32,
pub depth: RangeCopy<f32>,
pub target_view: Arc<TextureView>,
pub pos: Position,
}
impl DirectionalLight {
pub fn to_raw(&self) -> LightRaw {
use cgmath::{Deg, EuclideanSpace, Matrix4, PerspectiveFov, Point3, Vector3};
let point3d = Point3::new(self.pos.x, self.pos.y, self.pos.z);
let mx_view = Matrix4::look_at(point3d, Point3::origin(), Vector3::unit_y());
let projection = PerspectiveFov {
fovy: Deg(self.fov).into(),
aspect: 1.0,
near: self.depth.start,
far: self.depth.end,
};
4 years ago
let mx_correction = OPENGL_TO_WGPU_MATRIX;
let mx_view_proj =
mx_correction * cgmath::Matrix4::from(projection.to_perspective()) * mx_view;
LightRaw {
proj: *mx_view_proj.as_ref(),
pos: [self.pos.x, self.pos.y, self.pos.z, 1.0],
color: [
self.color.r as f32,
self.color.g as f32,
self.color.b as f32,
1.0,
],
}
}
}