-
Notifications
You must be signed in to change notification settings - Fork 1
/
ambient.go
58 lines (53 loc) · 1.65 KB
/
ambient.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
package raytracer
// Calculate light reflecting from other objects.
func ambientLightCalc(scene *Scene, intersection *Intersection, samples []Intersection, totalDirs int) float64 {
totalHits := 0.0
rad := scene.ShortRadius
if GlobalConfig.AmbientRadius > 0 {
rad = GlobalConfig.AmbientRadius
}
for i := 0; i < len(samples); i++ {
if samples[i].Dist < rad {
totalHits++
}
}
return 1.0 - (totalHits / float64(totalDirs))
}
func ambientColor(scene *Scene, intersection *Intersection, samples []Intersection, totalDirs int) (result Vector) {
if !intersection.Hit {
return Vector{}
}
totalHits := 0.0
totalColor := Vector{}
sampleCount := len(samples)
for i := 0; i < sampleCount; i++ {
color := samples[i].getColor()
if vectorLength(color) < DIFF {
continue
}
totalHits++
totalColor = addVector(totalColor, color)
}
if totalHits == 0 {
return Vector{}
}
return scaleVector(totalColor, 1.0/float64(sampleCount))
}
func ambientSampling(scene *Scene, intersection *Intersection) []Intersection {
sampleDirs := createSamples(intersection.IntersectionNormal, GlobalConfig.SamplerLimit, 0)
hitChannel := make(chan Intersection, len(sampleDirs))
for i := range sampleDirs {
go func(scene *Scene, intersection *Intersection, dir Vector, channel chan Intersection) {
hit := raycastSceneIntersect(scene, intersection.Intersection, dir)
channel <- hit
}(scene, intersection, sampleDirs[i], hitChannel)
}
samples := make([]Intersection, 0, len(sampleDirs))
for i := 0; i < len(sampleDirs); i++ {
hit := <-hitChannel
if hit.Hit && hit.Triangle.id != intersection.Triangle.id {
samples = append(samples, hit)
}
}
return samples
}