-
Notifications
You must be signed in to change notification settings - Fork 1
/
antialias.go
33 lines (31 loc) · 907 Bytes
/
antialias.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
package raytracer
import (
"math"
"math/rand"
)
func getPixel(scene *Scene, x, y int) Vector {
if GlobalConfig.AntialiasSamples > 64 {
GlobalConfig.AntialiasSamples = 64
}
if GlobalConfig.AntialiasSamples == 0 {
return scene.Pixels[x][y].Color
}
observer := scene.Cameras[0]
sw := scene.Width * 8
sh := scene.Height * 8
totalColor := Vector{}
totalHits := 0.0
p := rand.Perm(64)
for _, n := range p[:GlobalConfig.AntialiasSamples] {
yi := int(math.Floor(float64(n)/float64(8))) + (y * 8) - 4
xi := (n % 8) + (x * 8) - 4
rayDir := screenToWorld(xi, yi, sw, sh, observer.Position, *observer.Projection, observer.view)
hit := raycastSceneIntersect(scene, scene.Cameras[0].Position, rayDir)
render := hit.render(scene, 0)
totalColor = addVector(totalColor, render)
totalHits += 1.0
}
totalColor = scaleVector(totalColor, 1.0/totalHits)
totalColor[3] = 1
return totalColor
}