/
main.go
243 lines (188 loc) · 4.92 KB
/
main.go
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
package main
import (
"fmt"
"image"
"image/color"
"image/png"
"os"
"runtime/pprof"
"sync"
)
var (
red = color.RGBA64{R: 0xffff, A: 0xffff}
green = color.RGBA64{G: 0xffff, A: 0xffff}
black = color.RGBA64{A: 0xffff}
white = color.RGBA64{R: 0xffff, G: 0xffff, B: 0xffff, A: 0xffff}
blue = color.RGBA64{G: 0xff, B: 0xffff, A: 0xffff}
)
type Environment struct {
ambientCoefficient float64
diffuseCoefficient float64
shapes []Shape
width int
height int
eye Point
light Point
jitter int
}
type Point2D struct {
X int
Y int
}
type ColoredPoint2D struct {
X int
Y int
color color.RGBA64
}
func shadeColor(c color.RGBA64, percent float64) color.RGBA64 {
red := uint16(float64(c.R) * percent)
green := uint16(float64(c.G) * percent)
blue := uint16(float64(c.B) * percent)
return color.RGBA64{R: red, G: green, B: blue, A: 65535}
}
func averageColor(dots []color.RGBA64) color.RGBA64 {
r := 0
g := 0
b := 0
a := 0
for _, c := range dots {
r += int(c.R)
g += int(c.G)
b += int(c.B)
a += int(c.A)
}
l := len(dots)
return color.RGBA64{R: uint16(r / l), G: uint16(g / l), B: uint16(b / l), A: uint16(a / l)}
}
func main() {
f, err := os.Create("run.prof")
if err != nil {
panic("Could not create prof")
}
err = pprof.StartCPUProfile(f)
if err != nil {
panic("failed to start profile")
}
defer pprof.StopCPUProfile()
//ball1 := Sphere{Point{250.0, 100.0, 0.0}, 50.0, red}
//ball2 := Sphere{Point{150.0, 125.0, 0.0}, 80.0, green}
// cube := Cube{[8]Ray{top, bottom, left, right, front, back}, blue}
cube := NewCube(Point{0, 0, 0}, 50, blue)
// shapes := []Shape{ball1, ball2, cube}
//shapes := []Shape{ball1, ball2}
img := image.NewRGBA64(image.Rect(0, 0, 800, 800))
drawChan := make(chan ColoredPoint2D, 500)
var wg sync.WaitGroup
wg.Add(1)
go func() {
for point := range drawChan {
img.Set(point.X, point.Y, point.color)
}
wg.Done()
}()
environment := Environment{
ambientCoefficient: 0.2,
diffuseCoefficient: 0.8,
shapes: []Shape{cube},
width: img.Rect.Dx(),
height: img.Rect.Dy(),
eye: Point{150, -200, -400.0},
light: Point{100, -400, -400},
jitter: 3,
}
renderChan := make(chan Point2D, 5)
go renderPoint(renderChan, environment, drawChan)
// Walk the screen left to right, top to bottom
// (or bottom to top, I'm not sure)
for x := 0; x < img.Rect.Dx(); x++ {
for y := 0; y < img.Rect.Dy(); y++ {
renderChan <- Point2D{x, y}
}
}
close(renderChan)
wg.Wait()
name := "drawing.png"
fmt.Println("Writing file", name)
f, err = os.Create(name)
if err != nil {
panic(err)
}
err = png.Encode(f, img)
if err != nil {
panic("Failed to encode image")
}
err = f.Close()
if err != nil {
panic("Failed to close file")
}
}
func renderPoint(renderChan chan Point2D, environment Environment, drawChan chan ColoredPoint2D) {
halfx := environment.width / 2
halfy := environment.height / 2
var wg sync.WaitGroup
for point := range renderChan {
wg.Add(1)
go func(point Point2D) {
defer wg.Done()
// Translate our x and y so that 0, 0 is in the center
tx := float64(point.X - halfx)
ty := float64(point.Y - halfy)
// This will hold all of the dots from our jitter
dots := make([]color.RGBA64, environment.jitter*environment.jitter)
var increment, offset float64
if environment.jitter == 1 {
increment = 0.0
offset = 0.0
} else {
increment = 1.0 / (float64(environment.jitter) - 1.0)
offset = 0.5
}
// Our jitter
index := 0
for xj := 0; xj < environment.jitter; xj++ {
for yj := 0; yj < environment.jitter; yj++ {
xx := tx - offset + increment*float64(xj)
yy := ty - offset + increment*float64(yj)
hits := make(map[float64]color.RGBA64)
for _, shape := range environment.shapes {
// Create a unit vector from our eye
ray := unitRay(environment.eye, Point{xx, yy, 0.0})
// Calculate the point of intersect
hit, m := shape.intersect(ray)
if !hit {
continue
}
p := multiplyRay(ray, m)
// Normal unit vector out of the sphere
normal := shape.normal(p)
shade := -dotProduct(normal, unitRay(environment.light, p).Direction)
if shade < 0 {
shade = 0
}
pointColor := shadeColor(shape.getColor(),
environment.ambientCoefficient+environment.diffuseCoefficient*shade)
hits[m] = pointColor
}
pointColor := white
if len(hits) > 0 {
set := false
var last float64
// Find the closest one
for k, v := range hits {
if !set || k < last {
last = k
pointColor = v
set = true
}
}
}
dots[index] = pointColor
index++
}
}
drawChan <- ColoredPoint2D{X: point.X, Y: point.Y, color: averageColor(dots)}
}(point)
}
wg.Wait()
close(drawChan)
}