From 5c66c6673590323b2f875b77a353cd852565b4d1 Mon Sep 17 00:00:00 2001 From: ladevieq Date: Mon, 4 Jan 2021 22:54:07 +0100 Subject: Metallic materials --- include/vk-renderer.hpp | 10 +++++++++- shaders/compute.comp | 22 ++++++++++++++++++---- src/main.cpp | 6 ++++-- 3 files changed, 31 insertions(+), 7 deletions(-) diff --git a/include/vk-renderer.hpp b/include/vk-renderer.hpp index 06427f0..afd5019 100644 --- a/include/vk-renderer.hpp +++ b/include/vk-renderer.hpp @@ -6,6 +6,11 @@ #include "vec3.hpp" +enum MATERIAL_TYPE: uint32_t { + LAMBERTIAN, + METAL, +}; + struct input_data { color sky_color; color ground_color; @@ -23,10 +28,13 @@ struct input_data { vec3 position; struct material { color albedo; + float fuzz; + uint32_t type; + uint32_t padding[2]; } material; float radius; float padding[3]; - } spheres[2]; + } spheres[4]; uint32_t max_bounce; float padding[3]; diff --git a/shaders/compute.comp b/shaders/compute.comp index 6bf4205..7763538 100644 --- a/shaders/compute.comp +++ b/shaders/compute.comp @@ -5,6 +5,9 @@ struct ray { struct material { vec4 albedo; + float fuzz; + uint type; + uint[2] padding; }; struct hit_info { @@ -36,7 +39,7 @@ layout(set = 0, binding = 0) buffer Inputs { vec4[1000] random_in_sphere; - sphere[2] spheres; + sphere[4] spheres; uint max_bounce; float [3] padding; @@ -97,7 +100,7 @@ bool hit_spheres(ray r, float min_t, float max_t, out hit_info info) { float closest_t = max_t; hit_info temp_info; bool hit = false; - for (uint sphere_index = 0; sphere_index < 2; sphere_index++) { + for (uint sphere_index = 0; sphere_index < 4; sphere_index++) { if (hit_sphere(spheres[sphere_index], r, min_t, closest_t, temp_info)) { info = temp_info; closest_t = temp_info.t; @@ -108,7 +111,7 @@ bool hit_spheres(ray r, float min_t, float max_t, out hit_info info) { return hit; } -bool lambertian_scatter(material mat, ray incident, vec2 seed, hit_info info, out vec4 attenuation, out ray scattered) { +bool lambertian_scattering(material mat, ray incident, vec2 seed, hit_info info, out vec4 attenuation, out ray scattered) { vec3 unit_hemisphere_vec = to_hemisphere_vec(random_in_sphere[uint(rand(seed) * 999.0)].xyz, info.normal); attenuation = mat.albedo; @@ -117,6 +120,16 @@ bool lambertian_scatter(material mat, ray incident, vec2 seed, hit_info info, ou return true; } +bool metal_scattering(material mat, ray incident, vec2 seed, hit_info info, out vec4 attenuation, out ray scattered) { + vec3 reflected = reflect(normalize(incident.direction), info.normal); + vec3 unit_sphere_vec = random_in_sphere[uint(rand(seed) * 999.0)].xyz; + + attenuation = mat.albedo; + scattered = ray(info.point, reflected + mat.fuzz * unit_sphere_vec); + + return (dot(scattered.direction, info.normal) > 0.0); +} + vec4 ray_color(ray r, uint sample_index) { hit_info info; uint bounces = 0; @@ -127,7 +140,8 @@ vec4 ray_color(ray r, uint sample_index) { vec2 seed = vec2(info.point.x * sample_index, info.point.y * bounces) * gl_GlobalInvocationID.xy; ray scattered; vec4 attenuation; - if (lambertian_scatter(info.mat, current_ray, seed, info, attenuation, scattered)) { + if (info.mat.type == 0 && lambertian_scattering(info.mat, current_ray, seed, info, attenuation, scattered) || + info.mat.type == 1 && metal_scattering(info.mat, current_ray, seed, info, attenuation, scattered)) { final_color *= attenuation; current_ray = scattered; } else { diff --git a/src/main.cpp b/src/main.cpp index 5469e13..7ba2b47 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -103,8 +103,10 @@ int main() { .viewport_height = 2.f, .proj_plane_distance = 1.f, .spheres = { - { { 0.f, 0.f, -1.f }, { { 0.7f, 0.7f, 0.3f }, }, 0.5f }, - { { 0.f, -100.5f, -1.f }, { { 0.8f, 0.8f, 0.f }, }, 100.f } + { { -1.f, 0.f, -1.f }, { { 0.8f, 0.8f, 0.8f }, 0.3, MATERIAL_TYPE::METAL }, 0.5f }, + { { 0.f, 0.f, -1.f }, { { 0.7f, 0.3f, 0.3f }, MATERIAL_TYPE::LAMBERTIAN }, 0.5f }, + { { 1.f, 0.f, -1.f }, { { 0.8f, 0.6f, 0.2f }, 1.0, MATERIAL_TYPE::METAL }, 0.5f }, + { { 0.f, -100.5f, -1.f }, { { 0.8f, 0.8f, 0.f }, MATERIAL_TYPE::LAMBERTIAN }, 100.f } }, .max_bounce = max_depth, }; -- cgit v1.3