From 23d71549ebf82d9065de978f50d911bc51931635 Mon Sep 17 00:00:00 2001 From: Martin Lehoux Date: Sat, 4 Apr 2020 19:10:08 -0700 Subject: feat - Add ray bouncing --- color.go | 18 +++++++++++++++--- main.go | 3 ++- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/color.go b/color.go index 2dcf369..7c7bc58 100644 --- a/color.go +++ b/color.go @@ -18,11 +18,23 @@ func LinearBlend(color1 Color, color2 Color, param float64) Color { } } -func RayColor(ray Ray, hitable_list HitableList) Color { +func AttenuateColor(attenuation Color, color Color) Color { + return Color{ + attenuation.r * color.r, + attenuation.g * color.g, + attenuation.b * color.b, + } +} + +func RayColor(ray Ray, hitable_list HitableList, depth int) Color { + if depth > MAX_DEPTH { + return Color{0.0, 0.0, 0.0} + } hit := hitable_list.Hit(ray, T_MIN, T_MAX) if hit.time > 0.0 { - vec := AddVec3D(hit.normal, Vec3D{1.0, 1.0, 1.0}) - return LinearBlend(Color{vec.x, vec.y, vec.z}, Color{0.0, 0.0, 0.0}, 0.5) + direction := AddVec3D(hit.normal, RandomInUnitSphere()) + scattered := Ray{hit.point, direction} + return AttenuateColor(Color{0.5, 0.5, 0.5}, RayColor(scattered, hitable_list, depth+1)) } param := 0.5 * (ray.direction.Unit().y + 1) return LinearBlend(Color{0.5, 0.7, 1.0}, Color{1.0, 1.0, 1.0}, param) diff --git a/main.go b/main.go index 61ab73c..291e60f 100644 --- a/main.go +++ b/main.go @@ -11,6 +11,7 @@ const WIDTH = 200 const T_MIN = 0.001 const T_MAX = 1000 const RAY_PER_PIXEL = 100 +const MAX_DEPTH = 50 func main() { LOWER_LEFT_CORNER := Vec3D{-2.0, -1.0, -1.0} @@ -32,7 +33,7 @@ func main() { u := (float64(y) + rand.Float64()) / float64(WIDTH) v := (float64(x) + rand.Float64()) / float64(HEIGHT) ray := CAMERA.GetRay(u, v) - color_list[it] = RayColor(ray, WORLD) + color_list[it] = RayColor(ray, WORLD, 0) } color := ColorMeanSquare(color_list) frame[HEIGHT-1-x][y] = color.ToPixel() -- cgit v1.3