diff options
| author | Martin Lehoux <martin@lehoux.net> | 2020-04-05 22:46:01 -0700 |
|---|---|---|
| committer | Martin Lehoux <martin@lehoux.net> | 2020-04-05 22:46:01 -0700 |
| commit | e93eee4bb96bd2d81e422dc9d453da6138fd6ba2 (patch) | |
| tree | 0ef600be27a5bcde32d04283563c7b9fb8dd50d5 /color.go | |
| parent | 8730659596dbfdb9625c0fc413625b261f7d6d70 (diff) | |
misc - Doc
Diffstat (limited to 'color.go')
| -rw-r--r-- | color.go | 49 |
1 files changed, 28 insertions, 21 deletions
@@ -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 } |
