directional light
This commit is contained in:
parent
da773cad56
commit
b559bd5e08
8 changed files with 273 additions and 55 deletions
src/scene
|
@ -10,7 +10,12 @@ use crate::primitives::cube::Cube;
|
|||
use crate::primitives::quad::Quad;
|
||||
use crate::scene::oct_tree::OctTree;
|
||||
|
||||
use super::memorizable::Memorizable;
|
||||
use super::light::LightSource;
|
||||
use super::light::PointLight;
|
||||
use super::AppData;
|
||||
use super::LightsIter;
|
||||
use super::Scene;
|
||||
|
||||
pub struct EmptyVolume {
|
||||
pub memory_start: usize,
|
||||
|
@ -863,12 +868,37 @@ impl EmptyVolume {
|
|||
}
|
||||
quads
|
||||
}
|
||||
|
||||
pub fn select_lights(&self, lights: LightsIter, light_number: u32) -> Vec<u32> {
|
||||
let center = self.position + Vector3{x: self.size_x / 2, y: self.size_y / 2, z: self.size_z / 2};
|
||||
let mut weighted_indices = vec![];
|
||||
for light in lights {
|
||||
let weight = light.borrow().weighted_distance(center);
|
||||
weighted_indices.push((weight, light.borrow().get_memory_start()));
|
||||
}
|
||||
weighted_indices.sort_by(|a, b| a.0.partial_cmp(&b.0).unwrap());
|
||||
|
||||
let mut out_index = vec![];
|
||||
for index in 0..weighted_indices.len() {
|
||||
out_index.push(weighted_indices[weighted_indices.len() - (index + 1)].1 as u32);
|
||||
if out_index.len() == light_number as usize {
|
||||
break;
|
||||
}
|
||||
}
|
||||
while out_index.len() < light_number as usize {
|
||||
out_index.push(0);
|
||||
}
|
||||
out_index
|
||||
}
|
||||
}
|
||||
|
||||
impl Memorizable for EmptyVolume {
|
||||
// MARK: Get Buffer Mem Size
|
||||
pub fn get_buffer_mem_size(&self, light_number: u32) -> u32 {
|
||||
fn get_buffer_mem_size(&self, data: &AppData) -> u32 {
|
||||
let mut mem_size: u32 = 0;
|
||||
mem_size += 3; //pos
|
||||
mem_size += 3; //max sizes
|
||||
mem_size += light_number; // light references
|
||||
mem_size += data.num_lights_per_volume; // light references
|
||||
mem_size += 12; //color/roughness buffer sizes, 2 values each
|
||||
mem_size += 12; //neighbor buffer sizes, 2 values each
|
||||
|
||||
|
@ -890,7 +920,7 @@ impl EmptyVolume {
|
|||
mem_size
|
||||
}
|
||||
// MARK: insert into Memory
|
||||
pub fn insert_into_memory(&self, mut v: Vec<u32>, light_number: u32, lights: &Vec<PointLight>) -> Vec<u32> {
|
||||
fn insert_into_memory(&self, mut v: Vec<u32>, data: &AppData, scene: &Scene) -> Vec<u32> {
|
||||
let mut mem_index = self.memory_start;
|
||||
//pos
|
||||
v[mem_index] = self.position.x as u32;
|
||||
|
@ -907,7 +937,7 @@ impl EmptyVolume {
|
|||
v[mem_index] = self.size_z as u32;
|
||||
mem_index += 1;
|
||||
//Todo: insert lights
|
||||
let selected_lights = self.select_lights(lights, light_number);
|
||||
let selected_lights = self.select_lights(scene.get_light_iter(), data.num_lights_per_volume);
|
||||
for light in selected_lights {
|
||||
v[mem_index] = light;
|
||||
mem_index += 1;
|
||||
|
@ -1207,26 +1237,12 @@ impl EmptyVolume {
|
|||
v
|
||||
}
|
||||
|
||||
pub fn select_lights(&self, lights: &Vec<PointLight>, light_number: u32) -> Vec<u32> {
|
||||
let center = self.position + Vector3{x: self.size_x / 2, y: self.size_y / 2, z: self.size_z / 2};
|
||||
let mut weighted_indices = vec![];
|
||||
for light in lights {
|
||||
let weight = light.weighted_distance(center);
|
||||
weighted_indices.push((weight, light.memory_start));
|
||||
}
|
||||
weighted_indices.sort_by(|a, b| a.0.partial_cmp(&b.0).unwrap());
|
||||
fn get_memory_start(&self) -> usize {
|
||||
self.memory_start
|
||||
}
|
||||
|
||||
let mut out_index = vec![];
|
||||
for index in 0..weighted_indices.len() {
|
||||
out_index.push(weighted_indices[weighted_indices.len() - (index + 1)].1 as u32);
|
||||
if out_index.len() == light_number as usize {
|
||||
break;
|
||||
}
|
||||
}
|
||||
while out_index.len() < light_number as usize {
|
||||
out_index.push(0);
|
||||
}
|
||||
out_index
|
||||
fn set_memory_start(&mut self, memory_start: usize) {
|
||||
self.memory_start = memory_start;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue