diff --git a/shaders/compiled/frag_rt_quad.spv b/shaders/compiled/frag_rt_quad.spv index 18213c1..216c161 100644 Binary files a/shaders/compiled/frag_rt_quad.spv and b/shaders/compiled/frag_rt_quad.spv differ diff --git a/shaders/compiled/vert_rt_quad.spv b/shaders/compiled/vert_rt_quad.spv index 64d060c..2dbe257 100644 Binary files a/shaders/compiled/vert_rt_quad.spv and b/shaders/compiled/vert_rt_quad.spv differ diff --git a/shaders/rt_quad.frag b/shaders/rt_quad.frag index d5747b9..2e2a32a 100644 --- a/shaders/rt_quad.frag +++ b/shaders/rt_quad.frag @@ -4,6 +4,8 @@ layout(location = 0) in vec2 fragRasterPos; layout(location = 1) flat in uint fragVolumeStart; layout(location = 2) in vec3 origPosition; layout(location = 3) flat in uint facing; +layout(location = 4) flat in uvec2 minRasterPos; +layout(location = 5) flat in uvec2 maxRasterPos; layout(location = 0) out vec4 outColor; @@ -468,15 +470,21 @@ vec3 clamp_to_volume(uint volume_start, vec3 position) { return vec3(min(max(position.x, low_x_border), high_x_border), min(max(position.y, low_y_border), high_y_border), min(max(position.z, low_z_border), high_z_border)); } -vec3 add_reflection(vec3 view_vector, uint f, uint volume_start, vec3 pos, uvec4 color_sample, vec3 color_sum) { - vec3 orig_color_sample = vec3(float(color_sample.x) / 255.0, float(color_sample.y) / 255.0, float(color_sample.z) / 255.0); +vec2 clamp_to_quad(vec2 raster_pos, uvec2 min_raster_pos, uvec2 max_raster_pos) { + return vec2(max(min_raster_pos.x, min(max_raster_pos.x - 1, raster_pos.x)), max(min_raster_pos.y, min(max_raster_pos.y - 1, raster_pos.y))); +} - vec3 reflection_direction = reflect_vector(view_vector, f); - Tracing reflection_tracing = trace_ray(volume_start, pos, reflection_direction, pos_infinity, 0, max_iterations_per_light, true); +vec3 add_reflection(vec3 view_vector, uint f, uint volume_start, vec3 pos, uvec4 color_sample, vec3 color_sum) { float reflectivity = 1.0 - float(color_sample.w) / 255.0; - if (reflection_tracing.has_hit || reflection_tracing.has_transparent_hit) { - vec3 color_from_reflection = diffuse_tracing(reflection_tracing.end_volume, reflection_tracing.end_raster, reflection_tracing.end_pos, reflection_tracing.end_facing) * orig_color_sample; - color_sum = color_sum * (1.0 - reflectivity) + color_from_reflection * reflectivity; + + if (reflectivity > 0.01) { + vec3 orig_color_sample = vec3(float(color_sample.x) / 255.0, float(color_sample.y) / 255.0, float(color_sample.z) / 255.0); + vec3 reflection_direction = reflect_vector(view_vector, f); + Tracing reflection_tracing = trace_ray(volume_start, pos, reflection_direction, pos_infinity, 0, max_iterations_per_light, true); + if (reflection_tracing.has_hit || reflection_tracing.has_transparent_hit) { + vec3 color_from_reflection = diffuse_tracing(reflection_tracing.end_volume, reflection_tracing.end_raster, reflection_tracing.end_pos, reflection_tracing.end_facing) * orig_color_sample; + color_sum = color_sum * (1.0 - reflectivity) + color_from_reflection * reflectivity; + } } return color_sum; @@ -484,13 +492,14 @@ vec3 add_reflection(vec3 view_vector, uint f, uint volume_start, vec3 pos, uvec4 void main() { vec3 clamped_pos = clamp_to_volume(fragVolumeStart, origPosition); - uvec4 color_roughness = sample_color_from_scene_info(fragVolumeStart, fragRasterPos, facing); + vec2 clamped_raster_pos = clamp_to_quad(fragRasterPos, minRasterPos, maxRasterPos); + uvec4 color_roughness = sample_color_from_scene_info(fragVolumeStart, clamped_raster_pos, facing); vec3 orig_color_sample = vec3(float(color_roughness.x) / 255.0, float(color_roughness.y) / 255.0, float(color_roughness.z) / 255.0); vec3 color_sum; - uint orig_neighbor = sample_neighbor_from_scene_info(fragVolumeStart, fragRasterPos, facing); + uint orig_neighbor = sample_neighbor_from_scene_info(fragVolumeStart, clamped_raster_pos, facing); if (orig_neighbor != 0) { - vec3 color_direct = diffuse_tracing(fragVolumeStart, fragRasterPos, clamped_pos, facing); + vec3 color_direct = diffuse_tracing(fragVolumeStart, clamped_raster_pos, clamped_pos, facing); color_direct = add_reflection(normalize(clamped_pos - ubo.camera_pos), facing, fragVolumeStart, clamped_pos, color_roughness, color_direct); Tracing t = trace_ray(fragVolumeStart, ubo.camera_pos, clamped_pos - ubo.camera_pos, pos_infinity, 0, max_iterations_per_light, false); @@ -507,11 +516,10 @@ void main() { } } else { - color_sum = diffuse_tracing(fragVolumeStart, fragRasterPos, clamped_pos, facing); + color_sum = diffuse_tracing(fragVolumeStart, clamped_raster_pos, clamped_pos, facing); color_sum = add_reflection(normalize(clamped_pos - ubo.camera_pos), facing, fragVolumeStart, clamped_pos, color_roughness, color_sum); } outColor = vec4(color_sum, 1.0); - //outColor = vec4(orig_color_sample, 1.0); } \ No newline at end of file diff --git a/shaders/rt_quad.vert b/shaders/rt_quad.vert index d43d6e1..b5e9b2b 100644 --- a/shaders/rt_quad.vert +++ b/shaders/rt_quad.vert @@ -14,12 +14,17 @@ layout(location = 0) in vec3 inPosition; layout(location = 1) in uvec2 inRasterPos; layout(location = 2) in uint inVolumeStart; layout(location = 3) in uint inFacing; +layout(location = 4) in uvec2 inMinRasterPos; +layout(location = 5) in uvec2 inMaxRasterPos; layout(location = 0) out vec2 rasterPos; layout(location = 1) flat out uint volumeStart; layout(location = 2) out vec3 origPosition; layout(location = 3) flat out uint facing; +layout(location = 4) flat out uvec2 minRasterPos; +layout(location = 5) flat out uvec2 maxRasterPos; + void main() { if (ubo.use_geom_shader[0]) { gl_Position = ubo.geom_rot * ubo.model * vec4(inPosition, 1.0); @@ -30,4 +35,7 @@ void main() { volumeStart = inVolumeStart; origPosition = inPosition; facing = inFacing; + + minRasterPos = inMinRasterPos; + maxRasterPos = inMaxRasterPos; } \ No newline at end of file diff --git a/src/primitives/quad.rs b/src/primitives/quad.rs index b055edb..a6926d1 100644 --- a/src/primitives/quad.rs +++ b/src/primitives/quad.rs @@ -24,28 +24,36 @@ impl Drawable for Quad { vec3(self.pos1.x as f32, self.pos1.y as f32, self.pos1.z as f32), self.raster_pos + self.size.mul_element_wise(cgmath::Vector2 {x: 0, y: 0}), self.volume_index, - self.facing + self.facing, + self.raster_pos, + self.raster_pos + self.size.mul_element_wise(cgmath::Vector2 {x: 1, y: 1}), )); // 1 top right far scene.rt_vertices.push(vertex::RTVertex::new( vec3(self.pos2.x as f32, self.pos2.y as f32, self.pos2.z as f32), self.raster_pos + self.size.mul_element_wise(cgmath::Vector2 {x: 0, y: 1}), self.volume_index, - self.facing + self.facing, + self.raster_pos, + self.raster_pos + self.size.mul_element_wise(cgmath::Vector2 {x: 1, y: 1}), )); // 2 top left near scene.rt_vertices.push(vertex::RTVertex::new( vec3(self.pos3.x as f32, self.pos3.y as f32, self.pos3.z as f32), self.raster_pos + self.size.mul_element_wise(cgmath::Vector2 {x: 1, y: 1}), self.volume_index, - self.facing + self.facing, + self.raster_pos, + self.raster_pos + self.size.mul_element_wise(cgmath::Vector2 {x: 1, y: 1}), )); // 3 top right near scene.rt_vertices.push(vertex::RTVertex::new( vec3(self.pos4.x as f32, self.pos4.y as f32, self.pos4.z as f32), self.raster_pos + self.size.mul_element_wise(cgmath::Vector2 {x: 1, y: 0}), self.volume_index, - self.facing + self.facing, + self.raster_pos, + self.raster_pos + self.size.mul_element_wise(cgmath::Vector2 {x: 1, y: 1}), )); // change node order while preserving texture coordinates diff --git a/src/scene/generators.rs b/src/scene/generators.rs index 78c629e..3b3a6fa 100644 --- a/src/scene/generators.rs +++ b/src/scene/generators.rs @@ -215,5 +215,5 @@ pub fn generate_test_scene2(scene: &mut Scene, data: &mut AppData, chunk_num_x: scene.directional_lights.push(Rc::new(RefCell::new(DirectionalLight { direction: vec3(0.0, 0.0, -1.0), color: vec3(0.1, 0.1, 0.1), memory_start: 0 }))); scene.oct_trees = oct_trees; - Ok(cgmath::point3((max_x as f32 / 2.0) as f32, (max_y as f32 / 2.0) as f32, height_map[(max_x as f32 / 2.0).floor() as usize][(max_y as f32 / 2.0).floor() as usize] + 2.0)) + Ok(cgmath::point3((max_x as f32 / 2.0) as f32, (max_y as f32 / 2.0) as f32, height_map[(max_x as f32 / 2.0).floor() as usize][(max_y as f32 / 2.0).floor() as usize] + 2000.0)) } \ No newline at end of file diff --git a/src/vertex.rs b/src/vertex.rs index 3d5c6e7..90cead4 100644 --- a/src/vertex.rs +++ b/src/vertex.rs @@ -181,14 +181,16 @@ pub struct RTVertex { pub raster_pos: cgmath::Vector2<u32>, pub volume_start: u32, facing: Facing, + min_raster_pos: cgmath::Vector2<u32>, + max_raster_pos: cgmath::Vector2<u32>, } impl RTVertex { - pub const fn new(pos: Vec3, raster_pos: cgmath::Vector2<u32>, volume_start: u32, facing: Facing) -> Self { - Self { pos, raster_pos, volume_start, facing } + pub const fn new(pos: Vec3, raster_pos: cgmath::Vector2<u32>, volume_start: u32, facing: Facing, min_raster_pos: cgmath::Vector2<u32>, max_raster_pos: cgmath::Vector2<u32>) -> Self { + Self { pos, raster_pos, volume_start, facing, min_raster_pos, max_raster_pos } } } -impl VertexContainer<4> for RTVertex { +impl VertexContainer<6> for RTVertex { fn binding_description() -> vk::VertexInputBindingDescription { vk::VertexInputBindingDescription::builder() .binding(0) @@ -197,7 +199,7 @@ impl VertexContainer<4> for RTVertex { .build() } - fn attribute_descriptions() -> [vk::VertexInputAttributeDescription; 4] { + fn attribute_descriptions() -> [vk::VertexInputAttributeDescription; 6] { let pos = vk::VertexInputAttributeDescription::builder() .binding(0) .location(0) @@ -226,7 +228,21 @@ impl VertexContainer<4> for RTVertex { .offset((size_of::<Vec3>() + size_of::<cgmath::Vector2<u32>>() + size_of::<u32>()) as u32) .build(); - [pos, raster_pos, volume_start, facing] + let min_raster_pos = vk::VertexInputAttributeDescription::builder() + .binding(0) + .location(4) + .format(vk::Format::R32G32_UINT) + .offset((size_of::<Vec3>() + size_of::<cgmath::Vector2<u32>>() + size_of::<u32>() + size_of::<u32>()) as u32) + .build(); + + let max_raster_pos = vk::VertexInputAttributeDescription::builder() + .binding(0) + .location(5) + .format(vk::Format::R32G32_UINT) + .offset((size_of::<Vec3>() + size_of::<cgmath::Vector2<u32>>() + size_of::<u32>() + size_of::<u32>() + size_of::<cgmath::Vector2<u32>>()) as u32) + .build(); + + [pos, raster_pos, volume_start, facing, min_raster_pos, max_raster_pos] } }