-
Notifications
You must be signed in to change notification settings - Fork 1
/
direct_lighting.go
217 lines (181 loc) · 6.52 KB
/
direct_lighting.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
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
package raytracer
/*
Light related methods
*/
import (
"math"
)
const sunDist = 99999999999.00
const sunRadius = 4999999999.95
func isShortestIntersection(inter *Intersection, sInter *Intersection) bool {
return (sInter.Triangle != nil && sInter.Triangle.id == inter.Triangle.id) || sInter.Dist < DIFF
}
func isFlatGlass(inter *Intersection, sInter *Intersection) bool {
return (sInter.Hit && sInter.Triangle != nil) &&
(sInter.Triangle.id != inter.Triangle.id) && (sInter.Triangle.Material.Transmission > 0) &&
(GlobalConfig.RenderRefractions)
// && (!sInter.Triangle.Smooth)
}
func calculateDirectionalLight(scene *Scene, intersection *Intersection, light *Light, depth int) (result Vector) {
var shortestIntersection Intersection
if !intersection.Hit {
return
}
lightD := scaleVector(light.Direction, -1)
dotP := dot(intersection.IntersectionNormal, lightD)
if dotP < 0 {
return
}
if light.Samples == nil {
light.Samples = sampleSphere(sunRadius, GlobalConfig.LightSampleCount)
}
totalHits := 0.0
totalLight := Vector{}
for i := range light.Samples {
rayStart := addVectors(scaleVector(lightD, sunDist), intersection.Intersection, light.Samples[i])
dir := normalizeVector(subVector(rayStart, intersection.Intersection))
shortestIntersection = raycastSceneIntersect(scene, intersection.Intersection, dir)
if isShortestIntersection(intersection, &shortestIntersection) {
if !sameSideTest(intersection.IntersectionNormal, shortestIntersection.IntersectionNormal, 0) {
return
}
intensity := dotP * light.LightStrength
intensity *= GlobalConfig.Exposure
totalLight = addVector(totalLight, Vector{
light.Color[0] * intensity,
light.Color[1] * intensity,
light.Color[2] * intensity,
intensity,
})
totalHits += 1.0
}
// Let things pass if this is a regular glass
if isFlatGlass(intersection, &shortestIntersection) {
col := shortestIntersection.getColor()
lColor := Vector{
light.Color[0] * col[0],
light.Color[1] * col[1],
light.Color[2] * col[2],
1,
}
intensity := (1 / (shortestIntersection.Dist * shortestIntersection.Dist)) * GlobalConfig.Exposure
intensity *= dotP * light.LightStrength * shortestIntersection.Triangle.Material.Transmission
if intensity > DIFF && intensity < light.LightStrength {
subLight := Light{
Position: shortestIntersection.Intersection,
Color: lColor,
Active: true,
LightStrength: intensity,
}
return calculateDirectionalLight(scene, intersection, &subLight, depth)
}
}
}
if totalHits > 0 {
return scaleVector(totalLight, totalHits/float64(GlobalConfig.LightSampleCount))
}
return
}
// Calculate light for given light source.
// Result will be used to calculate "avarage" of the pixel color.
func calculateLight(scene *Scene, intersection *Intersection, light *Light, depth int) (result Vector) {
if !intersection.Hit {
return
}
l1 := normalizeVector(subVector(light.Position, intersection.Intersection))
l2 := intersection.IntersectionNormal
dotP := dot(l2, l1)
if dotP < 0 {
return
}
if intersection.Triangle.Material.Light {
if intersection.Triangle.Material.LightStrength == 0 {
intersection.Triangle.Material.LightStrength = light.LightStrength
}
return Vector{
GlobalConfig.Exposure * light.Color[0] * intersection.Triangle.Material.LightStrength,
GlobalConfig.Exposure * light.Color[1] * intersection.Triangle.Material.LightStrength,
GlobalConfig.Exposure * light.Color[2] * intersection.Triangle.Material.LightStrength,
1,
}
}
rayDir := normalizeVector(subVector(intersection.Intersection, light.Position))
rayLength := vectorDistance(intersection.Intersection, light.Position)
shortestIntersection := raycastSceneIntersect(scene, light.Position, rayDir)
s := math.Abs(rayLength - shortestIntersection.Dist)
if (shortestIntersection.Triangle != nil && shortestIntersection.Triangle.id == intersection.Triangle.id) || s < DIFF {
if !sameSideTest(intersection.IntersectionNormal, shortestIntersection.IntersectionNormal, 0) {
return
}
intensity := (1 / (rayLength * rayLength)) * GlobalConfig.Exposure
intensity *= dotP * light.LightStrength
if intersection.Triangle.Material.LightStrength > 0 {
intensity = intersection.Triangle.Material.LightStrength * GlobalConfig.Exposure
}
return Vector{
light.Color[0] * intensity,
light.Color[1] * intensity,
light.Color[2] * intensity,
intensity,
}
}
// Let things pass if this is a regular glass
if isFlatGlass(intersection, &shortestIntersection) {
col := shortestIntersection.getColor()
lColor := Vector{
light.Color[0] * col[0],
light.Color[1] * col[1],
light.Color[2] * col[2],
1,
}
intensity := (1 / (shortestIntersection.Dist * shortestIntersection.Dist)) * GlobalConfig.Exposure
intensity *= dotP * light.LightStrength * shortestIntersection.Triangle.Material.Transmission
if intensity > DIFF && intensity < light.LightStrength {
subLight := Light{
Position: shortestIntersection.Intersection,
Color: lColor,
Active: true,
LightStrength: intensity,
}
return calculateLight(scene, intersection, &subLight, depth)
}
}
return
}
func calculateTotalLight(scene *Scene, intersection *Intersection, depth int) (result Vector) {
if (!intersection.Hit) || (depth >= GlobalConfig.MaxReflectionDepth) {
return
}
if intersection.Triangle.Material.Light {
c := scaleVector(intersection.Triangle.Material.Color, intersection.Triangle.Material.LightStrength)
return c
}
lightChan := make(chan Vector, len(scene.Lights))
for i := range scene.Lights {
go func(scene *Scene, intersection *Intersection, light *Light, depth int, lightChan chan Vector) {
if light.Directional {
lightChan <- calculateDirectionalLight(scene, intersection, light, depth)
} else {
lightChan <- calculateLight(scene, intersection, light, depth)
}
}(scene, intersection, &scene.Lights[i], depth, lightChan)
}
result = Vector{}
for i := 0; i < len(scene.Lights); i++ {
light := <-lightChan
if light[3] > 0 {
result = addVector(result, light)
}
}
if GlobalConfig.PhotonSpacing > 0 && GlobalConfig.RenderCaustics {
if intersection.Triangle.Photons != nil && len(intersection.Triangle.Photons) > 0 {
for i := range intersection.Triangle.Photons {
if vectorDistance(intersection.Triangle.Photons[i].Location, intersection.Intersection) < GlobalConfig.PhotonSpacing {
c := scaleVector(intersection.Triangle.Photons[i].Color, GlobalConfig.Exposure)
result = addVector(result, c)
}
}
}
}
return result
}