-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
99 lines (87 loc) · 2.47 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
package main
import (
"fmt"
"math/rand"
"os"
"runtime"
"github.com/borisbsv/golearnyouatracer/draw"
"github.com/borisbsv/golearnyouatracer/geom"
"github.com/borisbsv/golearnyouatracer/material"
)
func main() {
f, _ := os.Create("test.ppm")
defer f.Close()
const x, y float64 = 200, 100
scene := draw.NewScene(x, y)
lFrom := geom.NewVec(3, 3, 2)
lAt := geom.NewVec(0, 0, -1)
vUp := geom.NewVec(0, 1, 0)
cam := draw.NewCamera(
lFrom,
lAt,
vUp,
90,
x/y,
lFrom.Sub(lAt).Len(),
0.01,
)
l := randomScene()
if err := scene.Generate(f, l, 100, cam, runtime.GOMAXPROCS(0)); err != nil {
fmt.Println(err)
return
}
if err := scene.WritePPM(f); err != nil {
fmt.Println(err)
}
}
func randomScene() draw.Hittable {
k := draw.NewList(
geom.NewSphere(geom.NewVec(0, -1000, 0), 1000, material.Lambertian{Albedo: geom.NewVec(0.5, 0.5, 0.5)}),
)
for a := -11.0; a < 11; a++ {
for b := -11.0; b < 11; b++ {
mat := rand.Float64()
center := geom.NewVec(a+0.9*rand.Float64(), 0.2, b+0.9*rand.Float64())
if center.Sub(geom.NewVec(4, 0.2, 0)).Len() > 0.9 {
switch {
case mat < 0.8:
k.Append(
geom.NewSphere(
center,
0.2,
material.Lambertian{
Albedo: geom.NewVec(rand.Float64()*rand.Float64(), rand.Float64()*rand.Float64(), rand.Float64()*rand.Float64()),
},
),
)
case mat < 0.95:
k.Append(
geom.NewSphere(
center,
0.2,
material.NewMetal(
geom.NewVec(0.5*(1+rand.Float64()), 0.5*(1+rand.Float64()), 0.5*rand.Float64()),
0.3,
),
),
)
default:
k.Append(geom.NewSphere(center, 0.2, material.NewGlass(1.5)))
}
}
}
}
k.Append(geom.NewSphere(geom.NewVec(0, 1, 0), 1, material.NewGlass(1.5)))
k.Append(geom.NewSphere(geom.NewVec(-4, 0, 1), 1, material.Lambertian{Albedo: geom.NewVec(0.4, 0.2, 0.1)}))
k.Append(geom.NewSphere(geom.NewVec(4, 1, 0), 1, material.NewMetal(geom.NewVec(0.7, 0.6, 0.5), 0)))
return k
}
func practiceScene() draw.Hittable {
return draw.NewList(
geom.NewSphere(geom.NewVec(0, 0, -1), 0.5, &material.Lambertian{Albedo: geom.NewVec(0.1, 0.2, 0.5)}),
geom.NewSphere(geom.NewVec(0, -100.5, -1), 100, &material.Lambertian{Albedo: geom.NewVec(0.8, 0.8, 0)}),
geom.NewSphere(geom.NewVec(1, 0, -1), 0.5, material.NewMetal(geom.NewVec(0.8, 0.6, 0.2), 0.3)),
geom.NewSphere(geom.NewVec(-1, 0, -1), 0.5, material.NewGlass(1.5)),
geom.NewSphere(geom.NewVec(-1, 0, -1), -0.45, material.NewGlass(1.5)),
)
}