From 3e41274f45735fa6e93ae346eafc8738a056d846 Mon Sep 17 00:00:00 2001 From: Anton Ljungdahl Date: Fri, 25 Apr 2025 23:47:57 +0200 Subject: [PATCH] diffuse skylight --- src/main.cu | 21 +++++++++++++++------ timeBuild.ctm | Bin 2468 -> 2500 bytes 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/src/main.cu b/src/main.cu index 0d6780b..cb56bfe 100644 --- a/src/main.cu +++ b/src/main.cu @@ -488,10 +488,10 @@ get_sample_color(RayF32 ray, Entity *entities, curandState *local_rand_state) Vec3F32 out = {0}; F32 current_attenuation = 1.0f; + F32 attenuation_factor = 0.5f; Vec3F32 sample_pixel_color = vec3F32(0.0f, 0.0f, 0.0f); for(U32 bounce_idx = 0; - //bounce_idx < MAX_DIFFUSE_DEPTH; - bounce_idx < 1; + bounce_idx < MAX_DIFFUSE_DEPTH; bounce_idx += 1) { @@ -524,14 +524,18 @@ get_sample_color(RayF32 ray, Entity *entities, curandState *local_rand_state) if(hit_rec.hit) { - // Paint entity + // "Paint entity" + // For a diffuse color we actually just update the attenuation here and + // bounce rays around... Then when we are not hitting anything anymore we will sample + // the background gradient and use the computed attenuation. Since the rays are + // bouncing diffusely this will shade nicely. Vec3F32 rand_dir = rand_unit_vector_on_hemisphere_F32(local_rand_state, hit_rec.normal); - current_attenuation = current_attenuation * 0.5f; + current_attenuation = current_attenuation * attenuation_factor; current_ray.origin = hit_rec.point; current_ray.direction = rand_dir; - sample_pixel_color = add_V3F32(hit_rec.normal, vec3F32(1.0f, 1.0f, 1.0f)); - sample_pixel_color = scale_V3F32(0.5f, sample_pixel_color); + //sample_pixel_color = add_V3F32(hit_rec.normal, vec3F32(1.0f, 1.0f, 1.0f)); + //sample_pixel_color = scale_V3F32(0.5f, sample_pixel_color); // debug //sample_pixel_color = vec3F32(1.0f, 0.0f, 0.0f); } @@ -547,6 +551,7 @@ get_sample_color(RayF32 ray, Entity *entities, curandState *local_rand_state) F32 blend = 0.5f*(unit_dir.y + 1.0f); sample_pixel_color = lerp_V3F32(blend, white, light_blue); + // Scale by the current attenuation for diffuse shading using background color sample_pixel_color = scale_V3F32(current_attenuation, sample_pixel_color); break; } @@ -568,7 +573,11 @@ cuda_main(Entity *entities, Vec3F32 *pixelbuffer, curandState *rand_state) if(x < image.width && y < image.height) { + // NOTE! We need to pass this as a pointer to subsequent usage functions, in order + // to update the random state on this thread, after each call to a distribution function. curandState local_rand_state = rand_state[idx]; + + // We are adding all samples and then dividing by num samples to get the mean, so // we initialise the color for this pixel to black. // Loop over all pixel samples diff --git a/timeBuild.ctm b/timeBuild.ctm index 24f69157b8a6549c5d3d2869e18f7c7e0d622d16..619dfc043a807c80a5751abb7baa5ad078dcc212 100644 GIT binary patch delta 40 ocmZ1?d_;J|5>5ppW7m0_+io*5GcYhTa4|64E)87);~N3_00of>;{X5v delta 7 OcmX>iyhM1z5>5aNwgT(`