halfway to raytracing
This commit is contained in:
parent
15a94c2f93
commit
3f4656939c
7 changed files with 222 additions and 42 deletions
|
@ -60,4 +60,5 @@ pub struct AppData {
|
|||
pub topology: vk::PrimitiveTopology,
|
||||
|
||||
pub scene_rt_memory_size: u64,
|
||||
pub num_lights_per_volume: u32,
|
||||
}
|
|
@ -178,6 +178,7 @@ impl App {
|
|||
let entry = Entry::new(loader).map_err(|b| anyhow!("{}", b))?;
|
||||
let mut data = app_data::AppData::default();
|
||||
data.use_geometry_shader = false;
|
||||
data.num_lights_per_volume = 1;
|
||||
let mut scene_handler = scene::Scene::default();
|
||||
|
||||
//load_model::load_model(&mut data)?;
|
||||
|
|
|
@ -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
|
||||
}
|
||||
}
|
34
src/scene/light.rs
Normal file
34
src/scene/light.rs
Normal file
|
@ -0,0 +1,34 @@
|
|||
use cgmath::{InnerSpace, MetricSpace, Vector3};
|
||||
|
||||
use crate::vertex;
|
||||
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
pub struct PointLight{
|
||||
pub pos: vertex::Vec3,
|
||||
pub color: vertex::Vec3,
|
||||
pub memory_start: usize,
|
||||
}
|
||||
|
||||
impl PointLight {
|
||||
pub fn get_buffer_mem_size(&self) -> u32 {
|
||||
4 * 3 + 4 * 3
|
||||
}
|
||||
|
||||
pub fn insert_into_memory(&self, mut v: Vec<u32>) -> Vec<u32> {
|
||||
v[self.memory_start] = self.pos.x as u32;
|
||||
v[self.memory_start + 1] = self.pos.y as u32;
|
||||
v[self.memory_start + 2] = self.pos.z as u32;
|
||||
|
||||
v[self.memory_start + 3] = self.color.x as u32;
|
||||
v[self.memory_start + 4] = self.color.y as u32;
|
||||
v[self.memory_start + 5] = self.color.z as u32;
|
||||
v
|
||||
}
|
||||
|
||||
pub fn weighted_distance(&self, center: Vector3<usize>) -> f32 {
|
||||
let distance = self.pos.distance(vertex::Vec3{x: center.x as f32, y: center.y as f32, z: center.z as f32});
|
||||
let light_intensity = self.color.magnitude();
|
||||
|
||||
light_intensity / distance.powf(2.0)
|
||||
}
|
||||
}
|
|
@ -1,5 +1,6 @@
|
|||
mod oct_tree;
|
||||
mod empty_volume;
|
||||
mod light;
|
||||
|
||||
use anyhow::Ok;
|
||||
use vulkanalia::prelude::v1_0::*;
|
||||
|
@ -10,6 +11,7 @@ use cgmath::{vec2, vec3};
|
|||
use std::cell::RefCell;
|
||||
use std::rc::Rc;
|
||||
|
||||
use crate::app_data;
|
||||
use crate::app_data::AppData;
|
||||
use crate::buffer;
|
||||
use crate::vertex;
|
||||
|
@ -17,6 +19,7 @@ use crate::primitives::cube::Cube;
|
|||
use crate::primitives::drawable::Drawable;
|
||||
use crate::scene::oct_tree::{OctTree, OctTreeIter, CHUNK_SIZE};
|
||||
use crate::scene::empty_volume::EmptyVolume;
|
||||
use crate::scene::light::PointLight;
|
||||
|
||||
extern crate rand;
|
||||
use rand::Rng;
|
||||
|
@ -50,6 +53,8 @@ pub struct Scene {
|
|||
pub index_buffer_memory_quad: vk::DeviceMemory,
|
||||
|
||||
pub rt_memory: Vec<u32>,
|
||||
|
||||
pub point_lights: Vec<PointLight>,
|
||||
}
|
||||
|
||||
impl Scene {
|
||||
|
@ -82,6 +87,8 @@ impl Scene {
|
|||
|
||||
oct_tree.set_cube(cube.clone());
|
||||
|
||||
self.point_lights.push(PointLight { pos: vec3(11.0, 11.0, 11.0), color: vec3(1.0, 1.0, 1.0), memory_start: 0 });
|
||||
|
||||
let mut empty_volumes: Vec<Rc<RefCell<EmptyVolume>>>;
|
||||
(empty_volumes, _) = EmptyVolume::from_oct_tree(&oct_tree);
|
||||
println!("number of empty volumes is {}", empty_volumes.len());
|
||||
|
@ -112,9 +119,14 @@ impl Scene {
|
|||
}
|
||||
|
||||
let mut memory_index = 1; // zero should be the location for the overall length (also will be the invalid memory allocation for pointing to a nonexistant neighbor)
|
||||
for light in &mut self.point_lights {
|
||||
light.memory_start = memory_index;
|
||||
memory_index += light.get_buffer_mem_size() as usize;
|
||||
}
|
||||
|
||||
for volume in &empty_volumes {
|
||||
volume.borrow_mut().memory_start = memory_index;
|
||||
memory_index += volume.borrow().get_buffer_mem_size() as usize;
|
||||
memory_index += volume.borrow().get_buffer_mem_size(data.num_lights_per_volume) as usize;
|
||||
|
||||
}
|
||||
for volume in &empty_volumes {
|
||||
|
@ -124,18 +136,15 @@ impl Scene {
|
|||
}
|
||||
}
|
||||
println!("Memory size is {} kB, max indes is {}", memory_index * 32 / 8 /1024 + 1, memory_index);
|
||||
let mut volume_vec = vec![memory_index as u32; memory_index];
|
||||
let mut volume_vec = vec![data.num_lights_per_volume; memory_index];
|
||||
for volume in &empty_volumes {
|
||||
volume_vec = volume.borrow().insert_into_memory(volume_vec);
|
||||
volume_vec = volume.borrow().insert_into_memory(volume_vec, data.num_lights_per_volume, &self.point_lights);
|
||||
}
|
||||
for light in &self.point_lights {
|
||||
volume_vec = light.insert_into_memory(volume_vec);
|
||||
}
|
||||
println!("volume_vec print {:?}", volume_vec);
|
||||
let mut empty_count = 0;
|
||||
for element in &volume_vec {
|
||||
if *element == memory_index as u32 {
|
||||
empty_count += 1;
|
||||
}
|
||||
}
|
||||
println!("empty elements count is {}", empty_count);
|
||||
|
||||
self.rt_memory = volume_vec;
|
||||
data.scene_rt_memory_size = (self.rt_memory.len() * 4) as u64; // size of the needed buffer size in bytes
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue