1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
|
package main
import "math"
type Color struct {
r, g, b float64
}
func (color Color) ToPixel() Pixel {
return Pixel{int(color.r * 255), int(color.g * 255), int(color.b * 255)}
}
func LinearBlend(color1 Color, color2 Color, param float64) Color {
return Color{
param*color1.r + (1.0-param)*color2.r,
param*color1.g + (1.0-param)*color2.g,
param*color1.b + (1.0-param)*color2.b,
}
}
func RayColor(ray Ray, hitable_list HitableList) Color {
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)
}
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
}
mean_color.r /= length
mean_color.g /= length
mean_color.b /= length
return mean_color
}
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
}
|