-
Notifications
You must be signed in to change notification settings - Fork 56
/
Copy pathraytracer.um
290 lines (237 loc) · 7.83 KB
/
raytracer.um
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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
// Raytracer demo in Umka
import (
"std.um"
"mat.um"
)
fn randn(): mat::Vec {
return {std::frand() - 0.5, std::frand() - 0.5, std::frand() - 0.5}
}
type Color = mat::Vec
type Ray = struct {
origin, dir: mat::Vec
}
type GenericBody = struct {
center: mat::Vec
color: Color
diffuseness: real
isLamp: bool
}
fn (b: ^GenericBody) lambertFactor(lambert: real): real {
return 1.0 - (1.0 - lambert) * b.diffuseness
}
type Body = interface {
get(): ^GenericBody
intersect(ray: ^Ray, point, normal: ^mat::Vec): bool
}
type Box = struct {
body: GenericBody
halfsize: mat::Vec
}
fn (b: ^Box) get(): ^GenericBody {return &b.body}
fn (b: ^Box) intersectFace(ray: ^Ray, point, normal: ^mat::Vec, i, j, k: int): bool {
if fabs(ray.dir[k]) > 1e-9 {
side := 1.0
if ray.dir[k] > 0.0 {
side = -1.0
}
if factor := (b.body.center[k] + side * b.halfsize[k] - ray.origin[k]) / ray.dir[k]; factor > 0.1 {
point^ = ray.origin.add(ray.dir.mul(factor))
const within = fn (x, y, xmin, ymin, xmax, ymax: real): bool {
return (x > xmin) && (x < xmax) && (y > ymin) && (y < ymax)
}
if within(
point[i], point[j],
b.body.center[i] - b.halfsize[i], b.body.center[j] - b.halfsize[j],
b.body.center[i] + b.halfsize[i], b.body.center[j] + b.halfsize[j]) {
normal[i] = 0
normal[j] = 0
normal[k] = side
return true
}
}
}
return false
}
fn (b: ^Box) intersect(ray: ^Ray, point, normal: ^mat::Vec): bool {
return b.intersectFace(ray, point, normal, 0, 1, 2) ||
b.intersectFace(ray, point, normal, 2, 0, 1) ||
b.intersectFace(ray, point, normal, 1, 2, 0)
}
type Sphere = struct {
body: GenericBody
radius: real
}
fn (s: ^Sphere) get(): ^GenericBody {return &s.body}
fn (s: ^Sphere) intersect(ray: ^Ray, point, normal: ^mat::Vec): bool {
displacement := s.body.center.sub(ray.origin)
proj := displacement.dot(ray.dir)
discr := s.radius * s.radius + proj * proj - displacement.dot(displacement)
if discr > 0 {
if factor := proj - sqrt(discr); factor > 0.1 {
point^ = ray.origin.add(ray.dir.mul(factor))
normal^ = point.sub(s.body.center).mul(1.0 / s.radius)
return true
}
}
return false
}
type Scene = struct {
ambientColor: Color
body: []Body
}
fn (sc: ^Scene) trace(ray: ^Ray, depth: int): Color {
if depth > 3 {
return sc.ambientColor
}
// Find nearest intersection
bestDist := 1e9
bestIndex := -1
var bestPoint, bestNormal: mat::Vec
for i, b in sc.body {
var point, normal: mat::Vec
if found := b.intersect(ray, &point, &normal); found {
if dist := point.sub(ray.origin).norm(); dist < bestDist {
bestDist = dist
bestIndex = i
bestPoint = point
bestNormal = normal
}
}
}
// Reflect rays
if bestIndex >= 0 {
bestBody := sc.body[bestIndex].get()
if bestBody.isLamp {
return bestBody.color
}
specularDir := ray.dir.sub(bestNormal.mul(2.0 * (ray.dir.dot(bestNormal))))
diffuseDir := specularDir.add(randn().mul(2.0 * bestBody.diffuseness)).normalize()
lambert := diffuseDir.dot(bestNormal)
if lambert < 0 {
diffuseDir = diffuseDir.sub(bestNormal.mul(2.0 * lambert))
lambert = -lambert
}
diffuseRay := Ray{bestPoint, diffuseDir}
return Color(mat::Vec(sc.trace(&diffuseRay, depth + 1)).mul(bestBody.lambertFactor(lambert)).elementwise(mat::Vec(bestBody.color)))
}
return sc.ambientColor
}
fn main() {
const (
width = 640
height = 480
)
// Define scene
body := []Body {
Box {
body: {
center: {500, -100, 1200},
color: {0.4, 0.7, 1.0},
diffuseness: 0.1,
isLamp: false
},
halfsize: {400 / 2.0, 600 / 2.0, 300 / 2.0}
},
Box {
body: {
center: {550, 210, 1100},
color: {0.9, 1.0, 0.6},
diffuseness: 0.3,
isLamp: false
},
halfsize: {1000 / 2.0, 20 / 2.0, 1000 / 2.0}
},
Sphere {
body: {
center: {600, 0, 700},
color: {1.0, 0.4, 0.6},
diffuseness: 0.2,
isLamp: false
},
radius: 200
},
Sphere {
body: {
center: {330, 150, 700},
color: {1.0, 1.0, 0.3},
diffuseness: 0.15,
isLamp: false
},
radius: 50
},
// Define light
Sphere {
body: {
center: {500, -1000, -700},
color: {1.0, 1.0, 1.0},
diffuseness: 1.0,
isLamp: true
},
radius: 800
}
}
scene := &Scene{{0.2, 0.2, 0.2}, body}
var numRays, numRenderers: int
printf("Raytracer demo\n")
printf("Rays per pixel (recommended 1 to 20): "); scanf("%lld", &numRays)
printf("Threads (recommended 1, 2, 4, 8, 16): "); scanf("%lld", &numRenderers)
// Define fibers
renderers := make([]fiber, numRenderers)
// Define eye
pos := mat::Vec{0, 0, 0}
azimuth := 30.0 * std::pi / 180.0
focal := 500.0
antialiasing := 1.0
pixels := new([height][width][3]uint8)
for i, renderer^ in renderers {
startHeight := height / numRenderers * i
stopHeight := height / numRenderers * (i + 1)
// Spawn fiber
renderer^ = make(fiber, |startHeight, stopHeight, numRays, pos, azimuth, focal, antialiasing, scene, pixels| {
sinAz, cosAz := sin(azimuth), cos(azimuth)
for i := startHeight; i < stopHeight; i++ {
for j := 0; j < width; j++ {
dir := mat::Vec{j - width / 2, i - height / 2, focal}
rotDir := mat::Vec{
dir[0]*cosAz + dir[2]*sinAz,
dir[1],
-dir[0]*sinAz + dir[2]*cosAz}
color := Color{0, 0, 0}
for r := 0; r < numRays; r++ {
randomDir := rotDir.add(randn().mul(
5491
antialiasing))
ray := Ray{pos, randomDir.normalize()}
color = Color(mat::Vec(color).add(mat::Vec(scene.trace(&ray, 0))))
}
color = Color(mat::Vec(color).mul(255.0 / numRays))
pixels[i][j] = {round(color[0]), round(color[1]), round(color[2])}
}
printf("%3lld/%3lld\n", i + 1, height)
resume()
}
})
}
// Render scene
startTime := std::clock()
for true {
working := false
for _, renderer in renderers {
if valid(renderer) {
resume(renderer)
working = true
}
}
if !working {break}
}
endTime := std::clock()
printf("Rendering time = %.3f s\n", endTime - startTime)
// Write output file
const fileName = "scene.ppm"
f, err := std::fopen(fileName, "wb")
std::exitif(err)
fprintf(f, "P6\n%lld %lld\n255\n", width, height)
std::fwrite(f, pixels)
std::fclose(f)
std::println("Done. See " + fileName)
std::getchar()
std::getchar()
}