halfway to raytracing
This commit is contained in:
parent
15a94c2f93
commit
3f4656939c
7 changed files with 222 additions and 42 deletions
src/scene
|
@ -10,6 +10,8 @@ use crate::primitives::cube::Cube;
|
|||
use crate::primitives::quad::Quad;
|
||||
use crate::scene::oct_tree::OctTree;
|
||||
|
||||
use super::light::PointLight;
|
||||
|
||||
pub struct EmptyVolume {
|
||||
pub memory_start: usize,
|
||||
|
||||
|
@ -47,7 +49,7 @@ impl EmptyVolume {
|
|||
self.position[1] + self.size_y > pos[1] && pos[1] >= self.position[1] &&
|
||||
self.position[2] + self.size_z > pos[2] && pos[2] >= self.position[2]
|
||||
}
|
||||
|
||||
// MARK: From Oct Tree
|
||||
pub fn from_oct_tree(tree: &OctTree<Cube>) -> (Vec<Rc<RefCell<EmptyVolume>>>, OctTree<Rc<RefCell<EmptyVolume>>>) {
|
||||
// todo: ppotentially use a child exist check while going through the oct tree to find some obvios starting empty volumes. Will still need to check for possible expansions though
|
||||
let mut volumes: Vec<Rc<RefCell<EmptyVolume>>> = vec![];
|
||||
|
@ -80,6 +82,7 @@ impl EmptyVolume {
|
|||
}
|
||||
println!("new starting pos: {}, {}, {}", x_index, y_index, z_index);
|
||||
println!("start growing volume x");
|
||||
// MARK: Start new Volume
|
||||
let mut x_size = 0;
|
||||
let mut grow = true;
|
||||
while grow {
|
||||
|
@ -160,7 +163,7 @@ impl EmptyVolume {
|
|||
neighbor_front: vec![],
|
||||
};
|
||||
println!("adding neighbor references");
|
||||
//fill in info in the neighbor octtree
|
||||
// MARK: fill in info in the neighbor octtree
|
||||
let reference = Rc::new(RefCell::new(new_volume));
|
||||
for x in 0..x_size+1 {
|
||||
for y in 0..y_size+1 {
|
||||
|
@ -200,7 +203,7 @@ impl EmptyVolume {
|
|||
let x_max_pos = reference.borrow().position.x + reference.borrow().size_x;
|
||||
let y_max_pos = reference.borrow().position.y + reference.borrow().size_y;
|
||||
let z_max_pos = reference.borrow().position.z + reference.borrow().size_z;
|
||||
// bottom face of the volume
|
||||
// MARK: bottom face of the volume
|
||||
let mut bottom_colors = vec![];
|
||||
let mut bottom_roughness = vec![];
|
||||
let mut bottom_elements_num = 0;
|
||||
|
@ -226,7 +229,7 @@ impl EmptyVolume {
|
|||
reference.borrow_mut().color_bottom= vec![];
|
||||
reference.borrow_mut().roughness_bottom= vec![];
|
||||
}
|
||||
// top face of the volume
|
||||
// MARK: top face of the volume
|
||||
let mut top_colors = vec![];
|
||||
let mut top_roughness = vec![];
|
||||
let mut top_elements_num = 0;
|
||||
|
@ -253,7 +256,7 @@ impl EmptyVolume {
|
|||
reference.borrow_mut().roughness_top= vec![];
|
||||
}
|
||||
|
||||
// back face of the volume
|
||||
// MARK: back face of the volume
|
||||
let mut back_colors = vec![];
|
||||
let mut back_roughness = vec![];
|
||||
let mut back_elements_num = 0;
|
||||
|
@ -280,7 +283,7 @@ impl EmptyVolume {
|
|||
reference.borrow_mut().roughness_back= vec![];
|
||||
}
|
||||
|
||||
// front face of the volume
|
||||
// MARK: front face of the volume
|
||||
let mut front_colors = vec![];
|
||||
let mut front_roughness = vec![];
|
||||
let mut front_elements_num = 0;
|
||||
|
@ -307,7 +310,7 @@ impl EmptyVolume {
|
|||
reference.borrow_mut().roughness_front= vec![];
|
||||
}
|
||||
|
||||
// front face of the volume
|
||||
// MARK: left face of the volume
|
||||
let mut left_colors = vec![];
|
||||
let mut left_roughness = vec![];
|
||||
let mut left_elements_num = 0;
|
||||
|
@ -334,7 +337,7 @@ impl EmptyVolume {
|
|||
reference.borrow_mut().roughness_left= vec![];
|
||||
}
|
||||
|
||||
// back face of the volume
|
||||
// MARK: right face of the volume
|
||||
let mut right_colors = vec![];
|
||||
let mut right_roughness = vec![];
|
||||
let mut right_elements_num = 0;
|
||||
|
@ -370,6 +373,7 @@ impl EmptyVolume {
|
|||
}
|
||||
}
|
||||
println!("add the neighbor linkage for all the volumes of the oct tree");
|
||||
// MARK: Neighbor Linkage
|
||||
for reference in volumes.iter_mut() {
|
||||
let x_min_pos;
|
||||
if reference.borrow().position.x == 0 {
|
||||
|
@ -398,7 +402,7 @@ impl EmptyVolume {
|
|||
let x_max_pos = reference.borrow().position.x + reference.borrow().size_x;
|
||||
let y_max_pos = reference.borrow().position.y + reference.borrow().size_y;
|
||||
let z_max_pos = reference.borrow().position.z + reference.borrow().size_z;
|
||||
// bottom face of the volume
|
||||
// MARK: bottom face of the volume
|
||||
let mut bottom_neighbors = vec![];
|
||||
let mut bottom_elements_num = 0;
|
||||
if z_min_pos != 0 {
|
||||
|
@ -420,7 +424,7 @@ impl EmptyVolume {
|
|||
else {
|
||||
reference.borrow_mut().neighbor_bottom = vec![None];
|
||||
}
|
||||
// top face of the volume
|
||||
// MARK: top face of the volume
|
||||
let mut top_neighbors = vec![];
|
||||
let mut top_elements_num = 0;
|
||||
for x in 0..reference.borrow().size_x {
|
||||
|
@ -441,7 +445,7 @@ impl EmptyVolume {
|
|||
reference.borrow_mut().neighbor_top = vec![None];
|
||||
}
|
||||
|
||||
// back face of the volume
|
||||
// MARK: back face of the volume
|
||||
let mut back_neighbors = vec![];
|
||||
let mut back_elements_num = 0;
|
||||
for x in 0..reference.borrow().size_x {
|
||||
|
@ -462,7 +466,7 @@ impl EmptyVolume {
|
|||
reference.borrow_mut().neighbor_back = vec![None];
|
||||
}
|
||||
|
||||
// front face of the volume
|
||||
// MARK: front face of the volume
|
||||
let mut front_neighbors = vec![];
|
||||
let mut front_elements_num = 0;
|
||||
if y_min_pos != 0{
|
||||
|
@ -485,7 +489,7 @@ impl EmptyVolume {
|
|||
reference.borrow_mut().neighbor_front = vec![None];
|
||||
}
|
||||
|
||||
// left face of the volume
|
||||
// MARK: left face of the volume
|
||||
let mut left_neighbors = vec![];
|
||||
let mut left_elements_num = 0;
|
||||
if x_min_pos != 0 {
|
||||
|
@ -508,7 +512,7 @@ impl EmptyVolume {
|
|||
reference.borrow_mut().neighbor_left = vec![None];
|
||||
}
|
||||
|
||||
// right face of the volume
|
||||
// MARK: right face of the volume
|
||||
let mut right_neighbors = vec![];
|
||||
let mut right_elements_num = 0;
|
||||
for y in 0..reference.borrow().size_y {
|
||||
|
@ -532,7 +536,7 @@ impl EmptyVolume {
|
|||
println!("volume creation took {} s", start_time.elapsed().as_millis() as f32 / 1000.0);
|
||||
(volumes, neighbors)
|
||||
}
|
||||
|
||||
// MARK: To Quads
|
||||
pub fn to_quads(&self) -> Vec<Quad> {
|
||||
let mut quads = vec![];
|
||||
let float_pos = Vector3 {x: self.position.x as f32, y: self.position.y as f32, z: self.position.z as f32};
|
||||
|
@ -686,10 +690,12 @@ impl EmptyVolume {
|
|||
}
|
||||
quads
|
||||
}
|
||||
|
||||
pub fn get_buffer_mem_size(&self) -> u32 {
|
||||
// MARK: Get Buffer Mem Size
|
||||
pub fn get_buffer_mem_size(&self, light_number: u32) -> u32 {
|
||||
let mut mem_size: u32 = 0;
|
||||
mem_size += 3; //pos
|
||||
mem_size += 3; //max sizes
|
||||
mem_size += light_number; // light references
|
||||
mem_size += 12; //color/roughness buffer sizes, 2 values each
|
||||
mem_size += 12; //neighbor buffer sizes, 2 values each
|
||||
|
||||
|
@ -710,9 +716,16 @@ impl EmptyVolume {
|
|||
|
||||
mem_size
|
||||
}
|
||||
|
||||
pub fn insert_into_memory(&self, mut v: Vec<u32>) -> Vec<u32> {
|
||||
// MARK: insert into Memory
|
||||
pub fn insert_into_memory(&self, mut v: Vec<u32>, light_number: u32, lights: &Vec<PointLight>) -> Vec<u32> {
|
||||
let mut mem_index = self.memory_start;
|
||||
//pos
|
||||
v[mem_index] = self.position.x as u32;
|
||||
mem_index += 1;
|
||||
v[mem_index] = self.position.y as u32;
|
||||
mem_index += 1;
|
||||
v[mem_index] = self.position.y as u32;
|
||||
mem_index += 1;
|
||||
//max sizes
|
||||
v[mem_index] = self.size_x as u32;
|
||||
mem_index += 1;
|
||||
|
@ -720,6 +733,12 @@ impl EmptyVolume {
|
|||
mem_index += 1;
|
||||
v[mem_index] = self.size_z as u32;
|
||||
mem_index += 1;
|
||||
//Todo: insert lights
|
||||
let selected_lights = self.select_lights(lights, light_number);
|
||||
for light in selected_lights {
|
||||
v[mem_index] = light;
|
||||
mem_index += 1;
|
||||
}
|
||||
//color/roughness buffer sizes, 2 values each
|
||||
if self.color_top.len() > 0 {
|
||||
v[mem_index] = self.size_x as u32;
|
||||
|
@ -1014,4 +1033,26 @@ impl EmptyVolume {
|
|||
println!("last memory index of volume was {}, equivalent to {}kB", mem_index, mem_index * 32 / 8 / 1024);
|
||||
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());
|
||||
|
||||
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
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue