directional light
This commit is contained in:
parent
da773cad56
commit
b559bd5e08
8 changed files with 273 additions and 55 deletions
src/scene
|
@ -1,8 +1,10 @@
|
|||
mod oct_tree;
|
||||
mod empty_volume;
|
||||
mod light;
|
||||
mod memorizable;
|
||||
|
||||
use anyhow::Ok;
|
||||
use light::{DirectionalLight, LightSource};
|
||||
use vulkanalia::prelude::v1_0::*;
|
||||
use anyhow::Result;
|
||||
|
||||
|
@ -11,6 +13,7 @@ use cgmath::{vec2, vec3, Vector3};
|
|||
use std::cell::RefCell;
|
||||
use std::rc::Rc;
|
||||
|
||||
use crate::scene::memorizable::Memorizable;
|
||||
use crate::app_data::AppData;
|
||||
use crate::buffer;
|
||||
use crate::primitives::rec_cuboid::Cuboid;
|
||||
|
@ -54,7 +57,8 @@ pub struct Scene {
|
|||
|
||||
pub rt_memory: Vec<u32>,
|
||||
|
||||
pub point_lights: Vec<PointLight>,
|
||||
point_lights: Vec<Rc<RefCell<PointLight>>>,
|
||||
directional_lights: Vec<Rc<RefCell<DirectionalLight>>>,
|
||||
}
|
||||
|
||||
impl Scene {
|
||||
|
@ -99,8 +103,9 @@ 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 });
|
||||
self.point_lights.push(PointLight { pos: vec3(9.0, 9.0, 11.0), color: vec3(0.5, 0.5, 0.5), memory_start: 0 });
|
||||
self.point_lights.push(Rc::new(RefCell::new(PointLight { pos: vec3(11.0, 11.0, 11.0), color: vec3(1.0, 1.0, 1.0), memory_start: 0 })));
|
||||
self.point_lights.push(Rc::new(RefCell::new(PointLight { pos: vec3(9.0, 9.0, 11.0), color: vec3(0.5, 0.5, 0.5), memory_start: 0 })));
|
||||
self.directional_lights.push(Rc::new(RefCell::new(DirectionalLight { direction: vec3(1.0, 1.0, -1.0), color: vec3(0.1, 0.1, 0.1), memory_start: 0 })));
|
||||
|
||||
let empty_volumes: Vec<Rc<RefCell<EmptyVolume>>>;
|
||||
(empty_volumes, _) = EmptyVolume::from_oct_tree(&oct_tree);
|
||||
|
@ -156,14 +161,14 @@ impl Scene {
|
|||
// 3 - diffuse raster size
|
||||
// 4 - max recursive rays
|
||||
// 5 - diffuse rays per hit
|
||||
for light in &mut self.point_lights {
|
||||
light.memory_start = memory_index;
|
||||
memory_index += light.get_buffer_mem_size() as usize;
|
||||
for light in LightsIter::new(self) {
|
||||
light.borrow_mut().set_memory_start(memory_index);
|
||||
memory_index += light.borrow_mut().get_buffer_mem_size(data) as usize;
|
||||
}
|
||||
|
||||
for volume in &empty_volumes {
|
||||
volume.borrow_mut().memory_start = memory_index;
|
||||
memory_index += volume.borrow().get_buffer_mem_size(data.num_lights_per_volume) as usize;
|
||||
volume.borrow_mut().set_memory_start(memory_index);
|
||||
memory_index += volume.borrow().get_buffer_mem_size(data) as usize;
|
||||
|
||||
}
|
||||
for volume in &empty_volumes {
|
||||
|
@ -181,10 +186,10 @@ impl Scene {
|
|||
volume_vec[5] = data.diffuse_rays_per_hit;
|
||||
|
||||
for volume in &empty_volumes {
|
||||
volume_vec = volume.borrow().insert_into_memory(volume_vec, data.num_lights_per_volume, &self.point_lights);
|
||||
volume_vec = volume.borrow().insert_into_memory(volume_vec, data, &self);
|
||||
}
|
||||
for light in &self.point_lights {
|
||||
volume_vec = light.insert_into_memory(volume_vec);
|
||||
for light in LightsIter::new(self) {
|
||||
volume_vec = light.borrow().insert_into_memory(volume_vec, data, &self);
|
||||
}
|
||||
//println!("volume_vec print {:?}", volume_vec);
|
||||
|
||||
|
@ -229,4 +234,40 @@ impl Scene {
|
|||
device.destroy_buffer(self.vertex_buffer_quad, None);
|
||||
device.free_memory(self.vertex_buffer_memory_quad, None);
|
||||
}
|
||||
|
||||
fn get_light_iter(&self) -> LightsIter {
|
||||
LightsIter::new(self)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
pub struct LightsIter<'a> {
|
||||
light_index: usize,
|
||||
scene: &'a Scene,
|
||||
}
|
||||
|
||||
impl<'a> LightsIter<'a> {
|
||||
fn new(scene: &'a Scene) -> Self {
|
||||
LightsIter {light_index: 0, scene: scene}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> Iterator for LightsIter<'a> {
|
||||
type Item = Rc<RefCell<dyn LightSource>>;
|
||||
|
||||
fn next(&mut self) -> Option<Self::Item> {
|
||||
if self.light_index >= self.scene.point_lights.len() {
|
||||
if self.light_index - self.scene.point_lights.len() >= self.scene.directional_lights.len() {
|
||||
None
|
||||
} else {
|
||||
let result = self.scene.directional_lights[self.light_index - self.scene.point_lights.len()].clone();
|
||||
self.light_index += 1;
|
||||
Some(result)
|
||||
}
|
||||
} else {
|
||||
let result = self.scene.point_lights[self.light_index].clone();
|
||||
self.light_index += 1;
|
||||
Some(result)
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue