rtrace 1.0.0

A pet raytracer to test overall pseudo-performance and multi-threading
Documentation
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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
// Adapted from http://www.ffconsultancy.com/languages/ray_tracer/comparison.html
// Original Author: Jack Palevich
// Performance Improvements: Sebastian Thiel

package main

import fmt "fmt"
import io "io"
import os "os"
import math "math"

var infinity float32 = float32(math.Inf(1))
var delta float32 = float32(math.Sqrt(1.19209E-07)) // sqrt(float_epsilon)

func sqrtf(a float32) float32 {
	return float32(math.Sqrt(float64(a)))
}

type Vec3 struct {
	x, y, z float32
}

func (v *Vec3) add(b *Vec3) *Vec3 {
	v.x += b.x
	v.y += b.y
	v.z += b.z
	return v
}

func (v *Vec3) sub(b *Vec3) *Vec3 {
	v.x -= b.x
	v.y -= b.y
	v.z -= b.z
	return v
}

func (v *Vec3) mulf(b float32) *Vec3 {
	v.x *= b
	v.y *= b
	v.z *= b
	return v
}

func (v *Vec3) normalize() *Vec3 {
	return v.mulf(1.0 / sqrtf(v.dot(v)))
}

func (v Vec3) normalized() Vec3 {
	return *v.mulf(1.0 / sqrtf(v.dot(&v)))
}

func (v *Vec3) dot(b *Vec3) float32 {
	return v.x*b.x + v.y*b.y + v.z*b.z
}

func vec3add(a Vec3, b Vec3) Vec3 {
	a.x += b.x
	a.y += b.y
	a.z += b.z
	return a
}

func vec3sub(a Vec3, b Vec3) Vec3 {
	a.x -= b.x
	a.y -= b.y
	a.z -= b.z
	return a
}

func vec3mulf(a Vec3, b float32) Vec3 {
	a.x *= b
	a.y *= b
	a.z *= b
	return a
}

func vec3dot(a Vec3, b Vec3) float32 {
	return a.x*b.x + a.y*b.y + a.z*b.z
}

func normalize(a Vec3) Vec3 {
	return vec3mulf(a, 1.0/sqrtf(vec3dot(a, a)))
}

var backgroundColor Vec3 = Vec3{0.1, 0.1, 0.1}
var diffuseSphereColor Vec3 = Vec3{0.0, 0.7, 0.0}
var ambientSphereColor Vec3 = Vec3{0.2, 0.3, 0.2}

type Sphere struct {
	center Vec3
	radius float32
}

type Hit struct {
	distance float32
	pos      Vec3
}

var hitinfinity Hit = Hit{infinity, Vec3{0, 0, 0}}

type Ray struct {
	orig, dir Vec3
}

type Geometry interface {
	Intersect(h *Hit, r *Ray)
	Print() // Temporary until fmt handles interfaces.
}

func (s *Sphere) RaySphere(r *Ray) float32 {
	v := vec3sub(s.center, r.orig)
	b := vec3dot(v, r.dir)
	disc := b*b - vec3dot(v, v) + s.radius*s.radius
	if disc < 0.0 {
		return infinity
	}
	d := sqrtf(disc)
	t2 := b + d
	if t2 < 0.0 {
		return infinity
	}
	t1 := b - d
	if t1 > 0.0 {
		return t1
	}
	return t2
}

func (s *Sphere) Intersect(h *Hit, r *Ray) {
	lambda := s.RaySphere(r)
	if lambda >= h.distance {
		return
	}
	h.distance = lambda
	h.pos = normalize(vec3add(r.orig, vec3sub(vec3mulf(r.dir, lambda), s.center)))
}

func (s *Sphere) Print() {
	fmt.Println("Sphere:", *s)
}

type Group struct {
	bound    Sphere
	children []Geometry
}

func (g *Group) Print() {
	fmt.Print("Group:")
	g.bound.Print()
	for i := 0; i < len(g.children); i++ {
		fmt.Print("  ")
		g.children[i].Print()
	}
}

func (g *Group) Intersect(h *Hit, r *Ray) {
	l := g.bound.RaySphere(r)
	if l >= h.distance {
		return
	}
	for _, c := range g.children {
		c.Intersect(h, r)
	}
}

func NewGroup(bound Sphere, children []Geometry) *Group {
	g := new(Group)
	g.bound = bound
	g.children = children
	return g
}

type Scene struct {
	light Vec3
	g     Geometry
}

func createScene(light Vec3, g Geometry) *Scene {
	scene := new(Scene)
	scene.light = light
	scene.g = g
	return scene
}

func (s *Scene) rayTrace(r *Ray) Vec3 {
	var hit Hit = hitinfinity
	s.g.Intersect(&hit, r)
	if hit.distance == infinity {
		return backgroundColor
	}
	g := vec3dot(hit.pos, s.light)
	if g >= 0.0 {
		// The hit intersection is in shadow
		return ambientSphereColor
	}
	p := vec3add(r.orig, vec3add(vec3mulf(r.dir, hit.distance), vec3mulf(hit.pos, delta)))
	hit.distance = infinity
	s.g.Intersect(&hit, &Ray{p, vec3mulf(s.light, -1.0)})
	if hit.distance < infinity {
		// There`s an object between us and the light.
		return ambientSphereColor
	}
	litColor := vec3mulf(diffuseSphereColor, -g)
	totalColor := vec3add(ambientSphereColor, litColor)
	return totalColor
}

func createSpherePyramid(level int, c Vec3, r float32) Geometry {
	s := new(Sphere)
	s.center = c
	s.radius = r
	if level == 1 {
		return s
	}
	children := make([]Geometry, 5)
	i := 0
	children[i] = s
	i++
	rn := 3.0 * r / sqrtf(12.0)
	for dz := -1; dz <= 1; dz += 2 {
		for dx := -1; dx <= 1; dx += 2 {
			newc := vec3add(c, vec3mulf(Vec3{float32(dx), 1.0, float32(dz)}, rn))
			children[i] = createSpherePyramid(level-1, newc, r*0.5)
			i++
		}
	}
	return NewGroup(Sphere{c, 3 * r}, children)
}

type Texture struct {
	w, h int
	buf  []byte
}

func NewTexture(w int, h int) *Texture {
	t := new(Texture)
	t.w = w
	t.h = h
	t.buf = make([]byte, w*h*4)
	return t
}

func formatTGAShort(buf []byte, offset int, value int) {
	buf[offset] = byte(value & 0xff)
	buf[offset+1] = byte((value >> 8) & 0xff)
}

func (t *Texture) WriteTGA(w io.Writer) {
	header := make([]byte, 18)
	header[0] = 0 // ID length
	header[1] = 0 // Color map type
	header[2] = 2 // Image type (2 == uncompressed true-color image)
	header[3] = 0
	header[4] = 0
	header[5] = 0
	header[6] = 0
	header[7] = 0
	formatTGAShort(header, 8, 0)
	formatTGAShort(header, 10, 0)
	formatTGAShort(header, 12, t.w)
	formatTGAShort(header, 14, t.h)
	header[16] = 24 // pixel depth
	header[17] = 0

	w.Write(header)
	buf := make([]byte, t.w*3)
	i := 4 * t.w * (t.h - 1)
	for y := 0; y < t.h; y++ {
		o := 0
		for x := 0; x < t.w; x++ {
			buf[o] = t.buf[i+2]
			buf[o+1] = t.buf[i+1]
			buf[o+2] = t.buf[i+0]
			o += 3
			i += 4
		}
		i -= 2 * 4 * t.w
		w.Write(buf)
	}
}

func (t *Texture) SetRgba(x int, y int, r byte, g byte, b byte, a byte) {
	o := 4 * (t.w*y + x)
	t.buf[o] = r
	t.buf[o+1] = g
	t.buf[o+2] = b
	t.buf[o+3] = a
}

func f2b(f float32) byte {
	scaled := 0.5 + f*255.0
	switch {
	case scaled < 0:
		scaled = 0
	case scaled > 255:
		scaled = 255
	}
	return byte(scaled)
}

func (t *Texture) SetV(x int, y int, v Vec3) {
	t.SetRgba(x, y, f2b(v.x), f2b(v.y), f2b(v.z), 255)
}

type Rect struct {
	l int
	t int
	r int
	b int
}

func newRect(l, t, r, b int) *Rect {
	rect := new(Rect)
	rect.l = l
	rect.t = t
	rect.r = r
	rect.b = b
	return rect
}

func (r *Rect) isEmpty() bool {
	return r.l == r.r || r.t == r.b
}

type Camera struct {
	eye Vec3
	w   int
	h   int
}

func (c *Camera) setRayDirForPixel(r *Ray, x, y float32) {
	r.dir.x = x - float32(c.w)*0.5
	r.dir.y = y - float32(c.h)*0.5
	r.dir.z = float32(c.w)
	r.dir.normalize()
}

type Renderer struct {
	scene      *Scene
	t          *Texture
	cam        *Camera
	ss         int // oversampling
	xres, yres int // image resolution
	jobChan    chan Rect
	quitChan   chan bool
	joinChan   chan bool
}

func (ren *Renderer) renderRect(tint Vec3, r *Rect) {
	ray := Ray{orig: ren.cam.eye}

	for y := r.t; y < r.b; y++ {
		for x := r.l; x < r.r; x++ {
			var g Vec3
			for ssx := 0; ssx < ren.ss; ssx++ {
				for ssy := 0; ssy < ren.ss; ssy++ {
					var xres float32 = float32(x) + float32(ssx)/float32(ren.ss)
					var yres float32 = float32(y) + float32(ssy)/float32(ren.ss)

					ren.cam.setRayDirForPixel(&ray, xres, yres)
					g = vec3add(g, ren.scene.rayTrace(&ray))
				} // END for each y subsample
			} // END for each x subsample

			ren.t.SetV(x, ren.cam.h-(y+1), vec3mulf(g, 1.0/float32(ren.ss*ren.ss)))

		} // END for each x pixel
	} // END for each y pixel
}

func (renderer *Renderer) worker(tint Vec3) {
	jobChan := renderer.jobChan
	for {
		select {
		case r := <-jobChan:
			renderer.renderRect(tint, &r)
		case <-renderer.quitChan:
			renderer.joinChan <- true
			return
		}
	}
}

func main() {
	level := 8
	chunkw := 16
	chunkh := 16
	w := 1024
	h := 768
	workers := 8
	ss := 4 // oversampling - use 4 to get 16 samples
	t := NewTexture(w, h)
	light := normalize(Vec3{-1.0, -3.0, 2.0})
	sp := createSpherePyramid(level, Vec3{0.0, -1.0, 0.0}, 1.0)
	scene := createScene(light, sp)
	eye := Vec3{0, 0, -4.0}
	camera := Camera{eye, w, h}
	quitChan := make(chan bool)
	joinChan := make(chan bool)
	jobChan := make(chan Rect)
	renderer := Renderer{scene, t, &camera, ss, w, h, jobChan, quitChan, joinChan}
	for w := 0; w < workers; w++ {
		tint := Vec3{0.5, float32(w) / float32(workers), 0.5}
		go renderer.worker(tint)
	}
	for y := 0; y < h; y += chunkh {
		for x := 0; x < w; x += chunkw {
			renderer.jobChan <- Rect{x, y, x + chunkw, y + chunkh}
		}
	}
	for w := 0; w < workers; w++ {
		renderer.quitChan <- true
	}
	for w := 0; w < workers; w++ {
		<-renderer.joinChan
	}
	od, err := os.OpenFile("out.tga", os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0666)
	if err == nil {
		t.WriteTGA(od)
		od.Close()
	}
}