From e93eee4bb96bd2d81e422dc9d453da6138fd6ba2 Mon Sep 17 00:00:00 2001 From: Martin Lehoux Date: Sun, 5 Apr 2020 22:46:01 -0700 Subject: misc - Doc --- color.go | 49 ++++++++++++++++++++++++++++--------------------- 1 file changed, 28 insertions(+), 21 deletions(-) (limited to 'color.go') diff --git a/color.go b/color.go index bb7318a..70c65f1 100644 --- a/color.go +++ b/color.go @@ -2,14 +2,17 @@ package main import "math" +// Color in RGB, each val being between 0 and 1 type Color struct { r, g, b float64 } +// ToPixel converts a 0-1 color to a 0-255 pixel func (color Color) ToPixel() Pixel { return Pixel{int(color.r * 255), int(color.g * 255), int(color.b * 255)} } +// LinearBlend mixes two colors with a float param func LinearBlend(color1 Color, color2 Color, param float64) Color { return Color{ param*color1.r + (1.0-param)*color2.r, @@ -18,6 +21,7 @@ func LinearBlend(color1 Color, color2 Color, param float64) Color { } } +// AttenuateColor multiplies two colors func AttenuateColor(attenuation Color, color Color) Color { return Color{ attenuation.r * color.r, @@ -26,37 +30,40 @@ func AttenuateColor(attenuation Color, color Color) Color { } } -func RayColor(ray Ray, hitable_list HitableList, depth int) Color { - if depth > MAX_DEPTH { +// RayColor finds a hit for a ray, scatters the ray and return the ray color +func RayColor(ray Ray, hitableList HitableList, depth int) Color { + if depth > MaxDepth { return Color{0.0, 0.0, 0.0} } - hit := hitable_list.Hit(ray, T_MIN, T_MAX) + hit := hitableList.Hit(ray, TMin, TMax) if hit.time > 0.0 { scattered, attenuation := hit.material.Scatter(ray, hit) - return AttenuateColor(attenuation, RayColor(scattered, hitable_list, depth+1)) + return AttenuateColor(attenuation, RayColor(scattered, hitableList, 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) } -func ColorMean(color_list [RAY_PER_PIXEL]Color) Color { - length := float64(len(color_list)) - mean_color := Color{} - for _, color := range color_list { - mean_color.r += color.r - mean_color.g += color.g - mean_color.b += color.b +// ColorMean computes the mean color of a color list +func ColorMean(colorList [RaysPerPixel]Color) Color { + length := float64(len(colorList)) + meanColor := Color{} + for _, color := range colorList { + meanColor.r += color.r + meanColor.g += color.g + meanColor.b += color.b } - mean_color.r /= length - mean_color.g /= length - mean_color.b /= length - return mean_color + meanColor.r /= length + meanColor.g /= length + meanColor.b /= length + return meanColor } -func ColorMeanSquare(color_list [RAY_PER_PIXEL]Color) Color { - mean_color := ColorMean(color_list) - mean_color.r = math.Sqrt(mean_color.r) - mean_color.g = math.Sqrt(mean_color.g) - mean_color.b = math.Sqrt(mean_color.b) - return mean_color +// ColorMeanSquare is a more realistic color mean +func ColorMeanSquare(colorList [RaysPerPixel]Color) Color { + meanColor := ColorMean(colorList) + meanColor.r = math.Sqrt(meanColor.r) + meanColor.g = math.Sqrt(meanColor.g) + meanColor.b = math.Sqrt(meanColor.b) + return meanColor } -- cgit v1.3