-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathmain.go
More file actions
180 lines (142 loc) · 4.57 KB
/
main.go
File metadata and controls
180 lines (142 loc) · 4.57 KB
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
package main
import (
"flag"
"fmt"
"math"
"math/rand"
"os"
"time"
p "github.com/markphelps/go-trace/primitives"
)
var config struct {
nx, ny, ns int
aperture, fov float64
filename string
}
func check(err error, msg string) {
if err != nil {
fmt.Fprintf(os.Stderr, msg, err)
os.Exit(1)
}
}
func color(r p.Ray, world p.Hitable, depth int) p.Color {
hit, record := world.Hit(r, 0.001, math.MaxFloat64)
if hit {
if depth < 50 {
bounced, bouncedRay := record.Bounce(r, record)
if bounced {
newColor := color(bouncedRay, world, depth+1)
return record.Material.Color().Multiply(newColor)
}
}
return p.Black
}
return p.Gradient(p.White, p.Blue, r.Direction.Normalize().Y)
}
// samples rays for anti-aliasing
func sample(world *p.World, camera *p.Camera, i, j int) p.Color {
rgb := p.Color{}
for s := 0; s < config.ns; s++ {
u := (float64(i) + rand.Float64()) / float64(config.nx)
v := (float64(j) + rand.Float64()) / float64(config.ny)
ray := camera.RayAt(u, v)
rgb = rgb.Add(color(ray, world, 0))
}
// average
return rgb.DivideScalar(float64(config.ns))
}
func render(world *p.World, camera *p.Camera) {
file, err := os.Create(config.filename)
check(err, "Error opening file: %v\n")
defer file.Close()
// http://netpbm.sourceforge.net/doc/ppm.html
_, err = fmt.Fprintf(file, "P3\n%d %d\n255\n", config.nx, config.ny)
check(err, "Error writting to file: %v\n")
ch := make(chan int)
defer close(ch)
go func() {
for {
if i, rendering := <-ch; rendering {
pct := 100 * float64(i) / float64(config.ny)
fmt.Printf("\r%.2f %% complete", pct)
}
}
}()
row := 1
for j := config.ny - 1; j >= 0; j-- {
ch <- row
row++
for i := 0; i < config.nx; i++ {
rgb := sample(world, camera, i, j)
// get intensity of colors with gamma-2 correction
ir := int(255.99 * math.Sqrt(rgb.R))
ig := int(255.99 * math.Sqrt(rgb.G))
ib := int(255.99 * math.Sqrt(rgb.B))
_, err := fmt.Fprintf(file, "%d %d %d\n", ir, ig, ib)
check(err, "Error writing to file: %v\n")
}
}
}
func scene() *p.World {
world := &p.World{}
floor := p.NewSphere(0, -1000, 0, 1000, p.Lambertian{Attenuation: p.Color{R: 0.5, G: 0.5, B: 0.5}})
world.Add(floor)
for a := -11; a < 11; a++ {
for b := -11; b < 11; b++ {
material := rand.Float64()
center := p.Vector{
X: float64(a) + 0.9*rand.Float64(),
Y: 0.2,
Z: float64(b) + 0.9*rand.Float64()}
if center.Subtract(p.Vector{X: 4, Y: 0.2, Z: 0}).Length() > 0.9 {
if material < 0.8 {
lambertian := p.NewSphere(center.X, center.Y, center.Z, 0.2,
p.Lambertian{Attenuation: p.Color{
R: rand.Float64() * rand.Float64(),
G: rand.Float64() * rand.Float64(),
B: rand.Float64() * rand.Float64()}})
world.Add(lambertian)
} else if material < 0.95 {
metal := p.NewSphere(center.X, center.Y, center.Z, 0.2,
p.Metal{Attenuation: p.Color{
R: 0.5 * (1.0 + rand.Float64()),
G: 0.5 * (1.0 + rand.Float64()),
B: 0.5 * (1.0 + rand.Float64())},
Fuzz: 0.5 + rand.Float64()})
world.Add(metal)
} else {
glass := p.NewSphere(center.X, center.Y, center.Z, 0.2, p.Dielectric{Index: 1.5})
world.Add(glass)
}
}
}
}
glass := p.NewSphere(0, 1, 0, 1.0, p.Dielectric{Index: 1.5})
lambertian := p.NewSphere(-4, 1, 0, 1.0, p.Lambertian{Attenuation: p.Color{R: 0.4, G: 0.0, B: 0.1}})
metal := p.NewSphere(4, 1, 0, 1.0, p.Metal{Attenuation: p.Color{R: 0.7, G: 0.6, B: 0.5}, Fuzz: 0.0})
world.AddAll(glass, lambertian, metal)
return world
}
func init() {
rand.Seed(time.Now().UnixNano())
}
func main() {
lookFrom := p.Vector{}
flag.Float64Var(&lookFrom.X, "x", 10, "look from X")
flag.Float64Var(&lookFrom.Y, "y", 4, "look from Y")
flag.Float64Var(&lookFrom.Z, "z", 6, "look from Z")
flag.Float64Var(&config.fov, "fov", 75.0, "vertical field of view (degrees)")
flag.IntVar(&config.nx, "width", 600, "width of image")
flag.IntVar(&config.ny, "height", 500, "height of image")
flag.IntVar(&config.ns, "samples", 100, "number of samples for anti-aliasing")
flag.Float64Var(&config.aperture, "aperture", 0.01, "camera aperture")
flag.StringVar(&config.filename, "out", "out.ppm", "output filename")
flag.Parse()
lookAt := p.Vector{X: 0, Y: 0, Z: -1}
camera := p.NewCamera(lookFrom, lookAt, config.fov, float64(config.nx)/float64(config.ny), config.aperture)
start := time.Now()
scene := scene()
fmt.Printf("\nRendering %d x %d pixel scene with %d objects...\n", config.nx, config.ny, scene.Count())
render(scene, camera)
fmt.Printf("\n\nDone. Elapsed: %v\nOutput to: %s\n", time.Since(start), config.filename)
}