diff --git a/projects/saf_r/shaders/raytracing.comp b/projects/saf_r/shaders/raytracing.comp index 042b532b5a7c4c923d99c3b477c8e7f9dcb86d4a..d8ff6e684025420581ada2ce8db78fbdceb11e53 100644 --- a/projects/saf_r/shaders/raytracing.comp +++ b/projects/saf_r/shaders/raytracing.comp @@ -24,6 +24,76 @@ layout(std430, binding = 2) coherent buffer Spheres{ Material material; }; +layout(local_size_x = 16, local_size_y = 16, local_size_z = 1) in; + +/* +vec3 safr_reflect(const vec3 dir, const vec3 hit_center) { + return dir - hit_center * 2.f * (dot(dir, hit_center)); +} + +bool ray_intersect(const vec3 origin, const vec3 dir, float t0){ + vec3 L = center - origin; + float tca = dot(L, dir); + float d2 = dot(L, L) - tca * tca; + if (d2 > radius * radius) return false; + float thc = float(sqrt(radius * radius - d2)); + t0 = tca - thc; + float t1 = tca + thc; + if (t0 < 0) t0 = t1; + if (t0 < 0) return false; + return true; +} + +bool sceneIntersect(const vec3 orig, const vec3 dir, const Spheres spheres[], vec3 hit, vec3 hit_center, Material material) { + float spheres_dist = std::numeric_limits<float>::max(); + for (int i = 0; i < spheres.size(); i++) { + float dist_i; + if (spheres[i].ray_intersect(orig, dir, dist_i) && dist_i < spheres_dist) { + spheres_dist = dist_i; + hit = orig + dir * dist_i; + hit_center = normalize(hit - spheres[i].center); + material = spheres[i].material; + } + } + return spheres_dist < 1000; +} + +vec3 castRay(const vec orig, const vec3 dir, const Spheres spheres[4], const Lights lights[3], int depth) { + depth = 0; + vec3 point, hit_center; + Material material; + +//return background color if a max recursive depth is reached + if (depth > 4 || !sceneIntersect(orig, dir, spheres, point, hit_center, material)) { + return vec3(0.2, 0.7, 0.8); + } + +//compute recursive directions and origins of rays and then call the function + vec3 reflect_dir = normalize(safr_reflect(dir, hit_center)); + vec3 reflect_orig = (dot(reflect_dir, hit_center) < 0) ? point - hit_center * float(1e-3) : + point + hit_center * float(1e-3); // offset the original point to avoid occlusion by the object itself + vec3 reflect_color = castRay(reflect_orig, reflect_dir, spheres, lights, depth + 1); + +//compute shadows and other light properties for the returned ray color + float diffuse_light_intensity = 0, specular_light_intensity = 0; + for (int i = 0; i < lights.size(); i++) { + vec3 light_dir = normalize(lights[i].position - point); + float light_distance = distance(lights[i].position, point); + + vec3 shadow_orig = (dot(light_dir, hit_center) < 0) ? point - hit_center * float(1e-3) : + point + hit_center * float(1e-3); // checking if the point lies in the shadow of the lights[i] + vec3 shadow_pt, shadow_hit_center; + Material tmpmaterial; + if (sceneIntersect(shadow_orig, light_dir, spheres, shadow_pt, shadow_hit_center, tmpmaterial) + && distance(shadow_pt, shadow_orig) < light_distance) + continue; + diffuse_light_intensity += lights[i].intensity * max(0.f, dot(light_dir, hit_center)); + specular_light_intensity += powf(max(0.f, dot(safr_reflect(light_dir, hit_center), dir)), material.specular_exponent) * lights[i].intensity; + } + return material.diffuse_color * diffuse_light_intensity * material.albedo[0] + + vec3(1., 1., 1.) * specular_light_intensity * material.albedo[1] + reflect_color * material.albedo[2]; +} */ + void main(){ int i = 42; } \ No newline at end of file diff --git a/projects/saf_r/src/main.cpp b/projects/saf_r/src/main.cpp index 9f6a1f1deed1c2ccc6456e9b6b2534ab61df8428..316d0a7dcb7f0a5f21d2f0d772094f9d31e7a313 100644 --- a/projects/saf_r/src/main.cpp +++ b/projects/saf_r/src/main.cpp @@ -78,7 +78,6 @@ int main(int argc, const char** argv) { const vkcv::DescriptorBindings& descriptorBindings = safrShaderProgram.getReflectedDescriptors().at(0); vkcv::DescriptorSetLayoutHandle descriptorSetLayout = core.createDescriptorSetLayout(descriptorBindings); - vkcv::DescriptorSetHandle descriptorSet = core.createDescriptorSet(descriptorSetLayout); //materials for the spheres @@ -236,7 +235,7 @@ int main(int argc, const char** argv) { vkcv::PushConstants pushConstantsCompute(0); //pushConstantsCompute.appendDrawcall(pushData); - uint32_t computeDispatchCount[3] = { 2,1,1 }; + uint32_t computeDispatchCount[3] = {static_cast<uint32_t> (std::ceil( windowWidth/16.f)), static_cast<uint32_t> (std::ceil(windowHeight/16.f)), 1 }; // Anzahl workgroups core.recordComputeDispatchToCmdStream(cmdStream, computePipeline, computeDispatchCount,