-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
150 lines (137 loc) · 3.53 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
package main
import (
"bufio"
"fmt"
"math"
"math/rand"
"os"
)
// Eps : Minimal distance
const Eps = 1.0e-4
const maxSteps = 128
const numSamples = 16
// Light : Simple point light
type Light struct {
position, color Vector
}
func clamp(s float64) uint8 {
return uint8(math.Min(math.Max(255.0*s, 0.0), 255.0))
}
func save(filename string, w, h int, framebuffer []Vector) {
f, _ := os.Create(filename)
defer f.Close()
wr := bufio.NewWriter(f)
defer wr.Flush()
fmt.Fprintf(wr, "P3\n%d %d\n255\n", w, h)
for _, p := range framebuffer {
r := clamp(p.x)
g := clamp(p.y)
b := clamp(p.z)
fmt.Fprintf(wr, "%d %d %d\n", r, g, b)
}
}
func opUnion(d1, d2 float64) float64 {
return math.Min(d1, d2)
}
func opSub(d1, d2 float64) float64 {
return math.Max(d1, -d2)
}
func sdSphere(p Vector, r float64) float64 {
return p.Length() - r
}
func sdTorus(p Vector, r1, r2 float64) float64 {
qx, qy := math.Sqrt(p.x*p.x+p.z*p.z)-r1, p.y
return math.Sqrt(qx*qx+qy*qy) - r2
}
func signedDistance(p Vector) float64 {
/*return opSub(opSub(opUnion(opUnion(opUnion(
sdSphere(p, 1.5),
sdSphere(Sub(p, Vector{2, 0, 0}), 1.0)),
sdSphere(Sub(p, Vector{0, -100, 0}), 100.0)),
sdTorus(Sub(p, Vector{0, 2, 0}), 1.0, 0.2)),
sdTorus(Sub(p, Vector{0, 1, 0}), 1.0, 0.2)),
sdSphere(Sub(p, Vector{-2, 0, -2}), 1.0))*/
return opSub(sdSphere(p, 2.0),
sdTorus(p, 2.0, 0.5))
}
func normal(pos Vector) Vector {
delx := Vector{Eps, 0, 0}
dely := Vector{0, Eps, 0}
delz := Vector{0, 0, Eps}
n := Vector{signedDistance(Add(pos, delx)) - signedDistance(Sub(pos, delx)),
signedDistance(Add(pos, dely)) - signedDistance(Sub(pos, dely)),
signedDistance(Add(pos, delz)) - signedDistance(Sub(pos, delz))}
n.Normalize()
return n
}
func lightContribution(pos Vector, light Light) Vector {
n := normal(pos)
v := Sub(light.position, pos)
v.Normalize()
hit, _ := sphereTrace(pos, v)
if hit {
return Vector{0.0, 0.0, 0.0}
} else {
return Mul(light.color, Dot(n, v))
}
}
func shade(pos Vector) Vector {
/*lights := [3]Light{Light{Vector{2.0, 5.0, -1.0}, Vector{0.3, 0.3, 0.3}},
Light{Vector{-2.0, 5.0, -1.0}, Vector{0.3, 0.3, 0.3}},
Light{Vector{0.0, 10.0, 0.0}, Vector{0.1, 0.15, 0.2}}}*/
lights := [2]Light{Light{Vector{3.0, 2.0, -2.0}, Vector{1.0, 0.5, 0.4}},
Light{Vector{-4.0, -1.0, -2.0}, Vector{0.2, 1.0, 0.5}}}
col := Vector{0.0, 0.0, 0.0}
for _, light := range lights {
col = Add(col, lightContribution(pos, light))
}
return col
}
func sphereTrace(o, dir Vector) (bool, Vector) {
t := Eps
for i := 0; i < maxSteps; i++ {
pos := Add(o, Mul(dir, t))
d := signedDistance(pos)
if math.Abs(d) < Eps*t {
return true, pos
}
t = t + d
if t > 100 {
break
}
}
return false, Vector{0.0, 0.0, 0.0}
}
func tracePixel(w, h int, x, y float64, fov float64) Vector {
o := Vector{0, 0, -5.0}
d := Vector{x + 0.5 - float64(w)/2.0,
-(y + 0.5 - float64(h)/2.0),
float64(h) / (2.0 * math.Tan(fov/2.0))}
d.Normalize()
hit, pos := sphereTrace(o, d)
if hit {
return shade(pos)
} else {
return Vector{0.34, 0.6, 0.8}
}
}
func render(w, h int, framebuffer []Vector) {
for y := 0; y < h; y++ {
for x := 0; x < w; x++ {
col := Vector{0, 0, 0}
for i := 0; i < numSamples; i++ {
px := float64(x) - 0.5 + rand.Float64()
py := float64(y) - 0.5 + rand.Float64()
col = Add(col, tracePixel(w, h, px, py, math.Pi/3.0))
}
framebuffer[y*w+x] = Mul(col, 1.0/float64(numSamples))
}
}
}
func main() {
fmt.Println("Hello, world y'all!")
w, h := 640, 360
framebuffer := make([]Vector, w*h)
render(w, h, framebuffer)
save("out.ppm", w, h, framebuffer)
}