From 91b9af0c86db9eb2859ac85609c3a6711f6be12a Mon Sep 17 00:00:00 2001 From: Andy LeClair Date: Fri, 29 May 2026 15:58:21 -0400 Subject: [PATCH] metal fuzziness --- src/color.ct | 4 ++++ src/main.ct | 8 ++++---- src/material.ct | 16 ++++++++++------ 3 files changed, 18 insertions(+), 10 deletions(-) diff --git a/src/color.ct b/src/color.ct index 491845f..9dbf145 100644 --- a/src/color.ct +++ b/src/color.ct @@ -1,6 +1,7 @@ (cl:defpackage #:ray/color (:use #:coalton #:coalton-prelude) (:export #:Color + #:black #:mul #:mul-scalar #:add)) @@ -13,6 +14,9 @@ (g F32) (b F32)) + (declare black (Void -> Color)) + (define (black) (Color 0.0 0.0 0.0)) + (declare mul (Color * Color -> Color)) (define (mul (Color r g b) (Color r2 g2 b2)) "Multiply a color by a given scalar value" diff --git a/src/main.ct b/src/main.ct index d13fbff..2c5f5d8 100644 --- a/src/main.ct +++ b/src/main.ct @@ -80,9 +80,9 @@ ; Center (s:Sphere (v:Vec3 0.0 0.0 -1.2) 0.5 (m:Lambertian (c:Color 0.1 0.2 0.5))) ; Left - (s:Sphere (v:Vec3 -1.0 0.0 -1.0) 0.5 (m:Metal (c:Color 0.8 0.8 0.8))) + (s:Sphere (v:Vec3 -1.0 0.0 -1.0) 0.5 (m:Metal (c:Color 0.8 0.8 0.8) 0.3)) ; Right - (s:Sphere (v:Vec3 1.0 0.0 -1.0) 0.5 (m:Metal (c:Color 0.8 0.6 0.2))) + (s:Sphere (v:Vec3 1.0 0.0 -1.0) 0.5 (m:Metal (c:Color 0.8 0.6 0.2) 1.0)) ]) (define INFINITY (the F32 math:infinity)) @@ -125,7 +125,7 @@ (declare ray-color (r:Ray * UFix -> c:Color)) (define (ray-color r depth) (cond - ((<= depth 0) (c:Color 0.0 0.0 0.0)) + ((<= depth 0) (c:black)) (True (match (hit-any? r 0.001 INFINITY) ((Some (h:HitRecord point normal _ _ material)) @@ -171,7 +171,7 @@ :repeat (unwrap-as UFix IMAGE-WIDTH) (let color = (for ((sample 0 (1+ sample)) - (pixel-color (c:Color 0.0 0.0 0.0) + (pixel-color (c:black) (c:add pixel-color (ray-color (get-ray x y) MAX_DEPTH)))) :returns (c:mul-scalar pixel-color PIXEL-SAMPLES-SCALE) :repeat (unwrap-as UFix SAMPLES-PER-PIXEL) diff --git a/src/material.ct b/src/material.ct index 91249c2..f44ba6f 100644 --- a/src/material.ct +++ b/src/material.ct @@ -13,7 +13,7 @@ (coalton-toplevel (define-type Material - (Metal c:Color) + (Metal c:Color F32) (Lambertian c:Color) (Dielectric F32)) @@ -25,15 +25,19 @@ (values (Some (r:Ray point normal)) albedo) (values (Some (r:Ray point scatter-direction)) albedo))) - (declare scatter-metal (v:Vec3 * v:Vec3 * v:Vec3 * c:Color -> Optional r:Ray * c:Color)) - (define (scatter-metal direction point normal albedo) + (declare scatter-metal (v:Vec3 * v:Vec3 * v:Vec3 * c:Color * F32 -> Optional r:Ray * c:Color)) + (define (scatter-metal direction point normal albedo fuzz) (let reflected = (v:reflect direction normal)) - (values (Some (r:Ray point reflected)) albedo)) + (let re-reflected = (v:vec-add (v:unit-vector reflected) + (v:vec-mul-scalar (v:random-unit-vector) fuzz))) + (if (> (v:vec-dot re-reflected normal) 0) + (values (Some (r:Ray point re-reflected)) albedo) + (values None (c:black)))) (declare scatter (r:Ray * v:Vec3 * v:Vec3 * Material -> Optional r:Ray * c:Color)) (define (scatter (r:Ray _ direction) point normal material) (match material ((Lambertian albedo) (scatter-lambertian point normal albedo)) - ((Metal albedo) (scatter-metal direction point normal albedo)) - (_ (values None (c:Color 0.0 0.0 0.0))))) + ((Metal albedo fuzz) (scatter-metal direction point normal albedo fuzz)) + (_ (values None (c:black))))) ) \ No newline at end of file -- 2.51.2