adds naive update for light and volumes

This commit is contained in:
zomseffen 2025-03-26 11:22:22 +01:00
parent 90b16a3d5c
commit 579820334d
6 changed files with 158 additions and 52 deletions
src/scene

View file

@ -22,9 +22,21 @@ pub trait LightSource: Light + Memorizable {}
#[derive(Clone, Debug, PartialEq)]
pub struct PointLight{
pub pos: vertex::Vec3,
pub color: vertex::Vec3,
pos: vertex::Vec3,
color: vertex::Vec3,
pub memory_start: usize,
dirty: bool,
}
impl PointLight {
pub fn init(pos: vertex::Vec3, color: vertex::Vec3) -> Self {
Self { pos: pos, color: color, memory_start: 0, dirty: true }
}
pub fn set_pos(&mut self, pos: vertex::Vec3) {
self.pos = pos;
self.dirty = true;
}
}
impl Memorizable for PointLight {
@ -32,7 +44,7 @@ impl Memorizable for PointLight {
1 + 3 + 3
}
fn insert_into_memory(&self, mut v: Vec<u32>, data: &AppData, scene: &Scene) -> Vec<u32> {
fn insert_into_memory(&mut self, mut v: Vec<u32>, data: &AppData, scene: &Scene) -> Vec<u32> {
v[self.memory_start] = LightType::POINT as u32;
v[self.memory_start + 1] = u32::from_ne_bytes(self.pos.x.to_ne_bytes());
@ -42,6 +54,9 @@ impl Memorizable for PointLight {
v[self.memory_start + 4] = (self.color.x * 255.0) as u32;
v[self.memory_start + 5] = (self.color.y * 255.0) as u32;
v[self.memory_start + 6] = (self.color.z * 255.0) as u32;
self.dirty = false;
v
}
@ -52,6 +67,13 @@ impl Memorizable for PointLight {
fn set_memory_start(&mut self, memory_start: usize) {
self.memory_start = memory_start;
}
fn get_prev_buffer_mem_size(&self) -> u32 {
1 + 3 + 3
}
fn is_dirty(&self) -> bool {
self.dirty
}
}
impl Light for PointLight {
@ -120,9 +142,16 @@ impl LightSource for PointLight {}
#[derive(Clone, Debug, PartialEq)]
pub struct DirectionalLight{
pub direction: vertex::Vec3,
pub color: vertex::Vec3,
direction: vertex::Vec3,
color: vertex::Vec3,
pub memory_start: usize,
dirty: bool,
}
impl DirectionalLight {
pub fn init(direction: vertex::Vec3, color: vertex::Vec3) -> Self {
Self { direction: direction, color: color, memory_start: 0, dirty: true }
}
}
impl Memorizable for DirectionalLight {
@ -130,7 +159,7 @@ impl Memorizable for DirectionalLight {
1 + 3 + 3
}
fn insert_into_memory(&self, mut v: Vec<u32>, data: &AppData, scene: &Scene) -> Vec<u32> {
fn insert_into_memory(&mut self, mut v: Vec<u32>, data: &AppData, scene: &Scene) -> Vec<u32> {
v[self.memory_start] = LightType::DIRECTION as u32;
v[self.memory_start + 1] = u32::from_ne_bytes(self.direction.x.to_ne_bytes());
@ -140,6 +169,10 @@ impl Memorizable for DirectionalLight {
v[self.memory_start + 4] = (self.color.x * 255.0) as u32;
v[self.memory_start + 5] = (self.color.y * 255.0) as u32;
v[self.memory_start + 6] = (self.color.z * 255.0) as u32;
self.dirty = false;
v
}
@ -150,6 +183,13 @@ impl Memorizable for DirectionalLight {
fn set_memory_start(&mut self, memory_start: usize) {
self.memory_start = memory_start;
}
fn get_prev_buffer_mem_size(&self) -> u32 {
1 + 3 + 3
}
fn is_dirty(&self) -> bool {
self.dirty
}
}
impl Light for DirectionalLight {
@ -176,7 +216,7 @@ mod test {
#[test]
fn test_memorizable() {
let p = PointLight {pos: vertex::Vec3 { x: 1.0, y: 2.0, z: 3.0}, color: vertex::Vec3 { x: 0.0, y: 1.0, z: 0.0}, memory_start: 0};
let mut p = PointLight::init(vertex::Vec3 { x: 1.0, y: 2.0, z: 3.0}, vertex::Vec3 { x: 0.0, y: 1.0, z: 0.0});
let data= AppData::default();
let scene = Scene::default();
@ -184,13 +224,15 @@ mod test {
assert!(mem_size == 7);
let mut memory = vec![0; 7];
assert!(!p.dirty);
p.insert_into_memory(memory, &data, &scene);
assert!(p.dirty);
}
#[test]
#[should_panic]
fn test_mem_size() {
let p = PointLight {pos: vertex::Vec3 { x: 1.0, y: 2.0, z: 3.0}, color: vertex::Vec3 { x: 0.0, y: 1.0, z: 0.0}, memory_start: 0};
let mut p = PointLight::init(vertex::Vec3 { x: 1.0, y: 2.0, z: 3.0}, vertex::Vec3 { x: 0.0, y: 1.0, z: 0.0});
let data= AppData::default();
let scene = Scene::default();
@ -198,12 +240,14 @@ mod test {
assert!(mem_size == 7);
let mut memory = vec![0; 6];
assert!(!p.dirty);
p.insert_into_memory(memory, &data, &scene);
assert!(p.dirty);
}
#[test]
fn test_memorizable_directional_light() {
let p = DirectionalLight {direction: vertex::Vec3 { x: 1.0, y: 2.0, z: 3.0}, color: vertex::Vec3 { x: 0.0, y: 1.0, z: 0.0}, memory_start: 0};
let mut p = DirectionalLight::init(vertex::Vec3 { x: 1.0, y: 2.0, z: 3.0}, vertex::Vec3 { x: 0.0, y: 1.0, z: 0.0});
let data= AppData::default();
let scene = Scene::default();
@ -211,13 +255,15 @@ mod test {
assert!(mem_size == 7);
let mut memory = vec![0; 7];
assert!(!p.dirty);
p.insert_into_memory(memory, &data, &scene);
assert!(p.dirty);
}
#[test]
#[should_panic]
fn test_mem_size_directional_light() {
let p = DirectionalLight {direction: vertex::Vec3 { x: 1.0, y: 2.0, z: 3.0}, color: vertex::Vec3 { x: 0.0, y: 1.0, z: 0.0}, memory_start: 0};
let mut p = DirectionalLight::init(vertex::Vec3 { x: 1.0, y: 2.0, z: 3.0}, vertex::Vec3 { x: 0.0, y: 1.0, z: 0.0});
let data= AppData::default();
let scene = Scene::default();
@ -225,6 +271,8 @@ mod test {
assert!(mem_size == 7);
let mut memory = vec![0; 6];
assert!(!p.dirty);
p.insert_into_memory(memory, &data, &scene);
assert!(p.dirty);
}
}