diff --git a/shaders/compiled/frag_rt_quad.spv b/shaders/compiled/frag_rt_quad.spv index 15b4f81..16608e1 100644 Binary files a/shaders/compiled/frag_rt_quad.spv and b/shaders/compiled/frag_rt_quad.spv differ diff --git a/shaders/rt_quad.frag b/shaders/rt_quad.frag index 495d707..307b9aa 100644 --- a/shaders/rt_quad.frag +++ b/shaders/rt_quad.frag @@ -133,8 +133,8 @@ vec3 get_light_position(uint light_index) { return vec3(float(scene_info.infos[light_index]), float(scene_info.infos[light_index + 1]), float(scene_info.infos[light_index + 2])); } -vec4 get_light_color(uint light_index) { - return vec4(float(scene_info.infos[light_index + 3]) / 255.0, float(scene_info.infos[light_index + 4]) / 255.0, float(scene_info.infos[light_index + 5]) / 255.0, 1.0); +vec3 get_light_color(uint light_index) { + return vec3(float(scene_info.infos[light_index + 3]) / 255.0, float(scene_info.infos[light_index + 4]) / 255.0, float(scene_info.infos[light_index + 5]) / 255.0); } void main() { @@ -147,22 +147,29 @@ void main() { uint max_light_num = scene_info.infos[0]; uint light_num = 0; uint volume_index = fragVolumeStart; - uint light_index = scene_info.infos[fragVolumeStart + 3]; + uint volume_pos_x = scene_info.infos[volume_index + 0]; + uint volume_pos_y = scene_info.infos[volume_index + 1]; + uint volume_pos_z = scene_info.infos[volume_index + 2]; + uint light_index = scene_info.infos[volume_index + 6]; vec3 light_direction = get_light_position(light_index) - origPosition; - vec4 light_color = get_light_color(light_index); + vec3 light_color = get_light_color(light_index); bool x_pos = light_direction.x > 0.0; - bool x_null = light_direction.x == 0.0; + bool x_null = (light_direction.x == 0.0); bool y_pos = light_direction.y > 0.0; - bool y_null = light_direction.y == 0.0; + bool y_null = (light_direction.y == 0.0); bool z_pos = light_direction.z > 0.0; - bool z_null = light_direction.z == 0.0; + bool z_null = (light_direction.z == 0.0); - uint max_iterations = max_light_num * 20; + vec3 color_sum = vec3(0.0, 0.0, 0.0) + (color_sample.xyz * 0.01); + + uint max_iterations = 2; //max_light_num * 20; for (int i = 0; i < max_iterations; i++) { - float x_border = float(scene_info.infos[volume_index + 0] + scene_info.infos[volume_index + 3] * uint(x_pos)); - float y_border = float(scene_info.infos[volume_index + 1] + scene_info.infos[volume_index + 4] * uint(y_pos)); - float z_border = float(scene_info.infos[volume_index + 2] + scene_info.infos[volume_index + 5] * uint(z_pos)); + float x_border = float(volume_pos_x + (scene_info.infos[volume_index + 3]) * uint(x_pos)) - 0.5; + float y_border = float(volume_pos_y + (scene_info.infos[volume_index + 4]) * uint(y_pos)) - 0.5; + float z_border = float(volume_pos_z + (scene_info.infos[volume_index + 5]) * uint(z_pos)) - 0.5; + + bool needs_next_light = false; // 2 is way behind the light position and should result in no collision being detected float x_factor = 2.0; @@ -172,17 +179,84 @@ void main() { x_factor = (x_border - origPosition.x) / light_direction.x; } if (!y_null) { - float y_factor = (y_border - origPosition.y) / light_direction.y; + y_factor = (y_border - origPosition.y) / light_direction.y; } if (!z_null) { - float z_factor = (z_border - origPosition.z) / light_direction.z; + z_factor = (z_border - origPosition.z) / light_direction.z; } - + if ((x_factor >= 0.999 && y_factor >= 0.999 && z_factor >= 0.999)) { + // no hit, add light color result + color_sum += (color_sample.xyz * light_color) / (0.01 * light_direction.length() * light_direction.length() + 1.0); + needs_next_light = true; + } else { + // if there is a border hit before reaching the light + // change to the relevant next volume + // Todo: look into removing ifs from this + uint hit_facing = 0; + uint u = 0; + uint v = 0; + if (x_factor <= y_factor && x_factor <= z_factor) { + if (x_pos) { + hit_facing = 3; + } else { + hit_facing = 2; + } + vec3 intersection_pos = origPosition + x_factor * light_direction; + u = uint(ceil(intersection_pos.y)) - volume_pos_y; + v = uint(ceil(intersection_pos.z)) - volume_pos_z; + } + + if (y_factor <= x_factor && y_factor <= z_factor) { + if (y_pos) { + hit_facing = 5; + } else { + hit_facing = 4; + } + vec3 intersection_pos = origPosition + y_factor * light_direction; + u = uint(ceil(intersection_pos.x)) - volume_pos_x; + v = uint(ceil(intersection_pos.z)) - volume_pos_z; + } + + if (z_factor <= x_factor && z_factor <= y_factor) { + if (y_pos) { + hit_facing = 0; + } else { + hit_facing = 1; + } + vec3 intersection_pos = origPosition + z_factor * light_direction; + u = uint(ceil(intersection_pos.x)) - volume_pos_x; + v = uint(ceil(intersection_pos.y)) - volume_pos_y; + } + uint next_neighbor = sample_neighbor_from_scene_info(volume_index, uvec2(u, v), hit_facing); + uvec4 color_sample = sample_color_from_scene_info(volume_index, uvec2(u, v), hit_facing); + + if (color_sample == uvec4(0, 0, 0, 0)) { + // not a color hit, so check neighbor + if (next_neighbor != 0) { + color_sum = vec3(1.0, 0.0, 0.0); + volume_index = next_neighbor; + uint volume_pos_x = scene_info.infos[volume_index + 0]; + uint volume_pos_y = scene_info.infos[volume_index + 1]; + uint volume_pos_z = scene_info.infos[volume_index + 2]; + } else { + color_sum = vec3(0.0, 0.0, 1.0); + // neightbor miss, shouldn't happen with a light inside of a volume. Might happen with ambient light. For now move on to next light. + needs_next_light = true; + } + } else { + color_sum = vec3(1.0, 0.0, 1.0); + // color hit, move on to next light (may change once transparents are implemnted) + needs_next_light = true; + } + } + if (needs_next_light) { + break; + } } - - outColor = color_sample; + // todo light color memory index does not contain the expected values -> check + outColor = vec4(color_sum, 1.0); /*if (scene_info.infos[1] == 16) { outColor = vec4(0, 1, 0, 1); diff --git a/src/scene/empty_volume.rs b/src/scene/empty_volume.rs index cbe73bb..d6407f6 100644 --- a/src/scene/empty_volume.rs +++ b/src/scene/empty_volume.rs @@ -216,8 +216,8 @@ impl EmptyVolume { bottom_roughness.push(128); } else { - bottom_colors.push(Vector3 { x: 255, y: 255, z: 255 }); - bottom_roughness.push(255); + bottom_colors.push(Vector3 { x: 0, y: 0, z: 0 }); + bottom_roughness.push(0); } } } @@ -242,8 +242,8 @@ impl EmptyVolume { top_roughness.push(128); } else { - top_colors.push(Vector3 { x: 255, y: 255, z: 255 }); - top_roughness.push(255); + top_colors.push(Vector3 { x: 0, y: 0, z: 0 }); + top_roughness.push(0); } } } @@ -269,8 +269,8 @@ impl EmptyVolume { back_roughness.push(128); } else { - back_colors.push(Vector3 { x: 255, y: 255, z: 255 }); - back_roughness.push(255); + back_colors.push(Vector3 { x: 0, y: 0, z: 0 }); + back_roughness.push(0); } } } @@ -296,8 +296,8 @@ impl EmptyVolume { front_roughness.push(128); } else { - front_colors.push(Vector3 { x: 255, y: 255, z: 255 }); - front_roughness.push(255); + front_colors.push(Vector3 { x: 0, y: 0, z: 0 }); + front_roughness.push(0); } } } @@ -323,8 +323,8 @@ impl EmptyVolume { left_roughness.push(128); } else { - left_colors.push(Vector3 { x: 255, y: 255, z: 255 }); - left_roughness.push(255); + left_colors.push(Vector3 { x: 0, y: 0, z: 0 }); + left_roughness.push(0); } } } @@ -350,8 +350,8 @@ impl EmptyVolume { right_roughness.push(128); } else { - right_colors.push(Vector3 { x: 255, y: 255, z: 255 }); - right_roughness.push(255); + right_colors.push(Vector3 { x: 0, y: 0, z: 0 }); + right_roughness.push(0); } } } diff --git a/src/scene/light.rs b/src/scene/light.rs index a83b754..4893dd3 100644 --- a/src/scene/light.rs +++ b/src/scene/light.rs @@ -11,7 +11,7 @@ pub struct PointLight{ impl PointLight { pub fn get_buffer_mem_size(&self) -> u32 { - 4 * 3 + 4 * 3 + 3 + 3 } pub fn insert_into_memory(&self, mut v: Vec) -> Vec { @@ -19,9 +19,9 @@ impl PointLight { 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[self.memory_start + 3] = (self.color.x * 255.0) as u32; + v[self.memory_start + 4] = (self.color.y * 255.0) as u32; + v[self.memory_start + 5] = (self.color.z * 255.0) as u32; v } diff --git a/src/scene/mod.rs b/src/scene/mod.rs index 7d710ff..e241b51 100644 --- a/src/scene/mod.rs +++ b/src/scene/mod.rs @@ -6,7 +6,7 @@ use anyhow::Ok; use vulkanalia::prelude::v1_0::*; use anyhow::Result; -use cgmath::{vec2, vec3}; +use cgmath::{vec2, vec3, Vector3}; use std::cell::RefCell; use std::rc::Rc; @@ -14,6 +14,7 @@ use std::rc::Rc; use crate::app_data; use crate::app_data::AppData; use crate::buffer; +use crate::primitives::rec_cuboid::Cuboid; use crate::vertex; use crate::primitives::cube::Cube; use crate::primitives::drawable::Drawable; @@ -117,6 +118,15 @@ impl Scene { None => {} } } + + let cube = Cuboid { + pos: vec3(11.0, 11.0, 11.0), + color: vec3(shade, 1.0, shade), + tex_coord: vec2(0.0, 0.0), + size: Vector3 {x: 0.5, y: 0.5, z: 0.5} + }; + let index = self.sized_vertices.len(); + cube.draw(&data.topology, index, self); 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 {