-
Notifications
You must be signed in to change notification settings - Fork 3
/
lambient_occlusion.cpp
256 lines (195 loc) · 6.79 KB
/
lambient_occlusion.cpp
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
#include "lambient_occlusion.h"
#include "core/os/threaded_array_processor.h"
namespace LM {
void AmbientOcclusion::ProcessAO_Texel(int tx, int ty, int qmc_variation) {
// if ((tx == 77) && (ty == 221))
// print_line("test");
const MiniList &ml = m_Image_TriIDs.GetItem(tx, ty);
if (!ml.num)
return; // no triangles in this UV
// could be off the image
float *pfTexel = m_Image_AO.Get(tx, ty);
float power = 0.0f;
// simple or complex? (are there cuts on this texel)
if (1)
// if (!m_Image_Cuts.GetItem(tx, ty))
{
power = CalculateAO(tx, ty, qmc_variation, ml);
} else {
power = CalculateAO_Complex(tx, ty, qmc_variation, ml);
}
*pfTexel = power;
}
void AmbientOcclusion::ProcessAO() {
if (bake_begin_function) {
bake_begin_function(m_iHeight);
}
// find the max range in voxels. This can be used to speed up the ray trace
const float range = m_Settings_AO_Range;
m_Scene.m_Tracer.GetDistanceInVoxels(range, m_Scene.m_VoxelRange);
#define LAMBIENT_OCCLUSION_USE_THREADS
#ifdef LAMBIENT_OCCLUSION_USE_THREADS
// int nCores = OS::get_singleton()->get_processor_count();
int nSections = m_iHeight / 64;
if (!nSections) nSections = 1;
int y_section_size = m_iHeight / nSections;
int leftover_start = y_section_size * nSections;
for (int s = 0; s < nSections; s++) {
int y_section_start = s * y_section_size;
if (bake_step_function) {
m_bCancel = bake_step_function(y_section_start, String("Process Texels: ") + " (" + itos(y_section_start) + ")");
if (m_bCancel) {
if (bake_end_function) {
bake_end_function();
}
return;
}
}
thread_process_array(y_section_size, this, &AmbientOcclusion::ProcessAO_LineMT, y_section_start);
}
// leftovers
int nLeftover = m_iHeight - leftover_start;
if (nLeftover)
thread_process_array(nLeftover, this, &AmbientOcclusion::ProcessAO_LineMT, leftover_start);
#else
for (int y = 0; y < m_iHeight; y++) {
if ((y % 10) == 0) {
if (bake_step_function) {
m_bCancel = bake_step_function(y, String("Process Texels: ") + " (" + itos(y) + ")");
if (m_bCancel) {
if (bake_end_function) {
bake_end_function();
}
return;
}
}
}
ProcessAO_LineMT(0, y);
}
#endif
if (bake_end_function) {
bake_end_function();
}
}
void AmbientOcclusion::ProcessAO_LineMT(uint32_t y_offset, int y_section_start) {
int ty = y_section_start + y_offset;
// seed based on the line
int qmc_variation = m_QMC.RandomVariation();
for (int x = 0; x < m_iWidth; x++) {
qmc_variation = m_QMC.GetNextVariation(qmc_variation);
ProcessAO_Texel(x, ty, qmc_variation);
}
}
float AmbientOcclusion::CalculateAO(int tx, int ty, int qmc_variation, const MiniList &ml) {
Ray r;
int nSamples = m_AdjustedSettings.m_AO_Samples;
// find the max range in voxels. This can be used to speed up the ray trace
Vec3i voxel_range = m_Scene.m_VoxelRange;
int nHits = 0;
int nSamplesInside = 0;
for (int n = 0; n < nSamples; n++) {
// pick a float position within the texel
Vector2 st;
AO_RandomTexelSample(st, tx, ty, n);
// find which triangle on the minilist we are inside (if any)
uint32_t tri_inside_id;
Vector3 bary;
if (!AO_FindTexelTriangle(ml, st, tri_inside_id, bary))
continue;
nSamplesInside++;
// calculate world position ray origin from barycentric
m_Scene.m_Tris[tri_inside_id].InterpolateBarycentric(r.o, bary);
// calculate surface normal (should be use plane?)
Vector3 ptNormal;
m_Scene.m_TriNormals[tri_inside_id].InterpolateBarycentric(ptNormal, bary);
//const Vector3 &ptNormal = m_Scene.m_TriPlanes[tri_inside_id].normal;
// construct a random ray to test
AO_RandomQMCRay(r, ptNormal, n, qmc_variation);
// test ray
if (m_Scene.TestIntersect_Ray(r, m_Settings_AO_Range, voxel_range)) {
nHits++;
}
} // for samples
float fTotal = (float)nHits / nSamplesInside;
fTotal = 1.0f - (fTotal * 1.0f);
if (fTotal < 0.0f)
fTotal = 0.0f;
return fTotal;
}
float AmbientOcclusion::CalculateAO_Complex(int tx, int ty, int qmc_variation, const MiniList &ml) {
// first we need to identify some sample points
AOSample samples[MAX_COMPLEX_AO_TEXEL_SAMPLES];
int nSampleLocs = AO_FindSamplePoints(tx, ty, ml, samples);
// if no good samples locs found
if (!nSampleLocs) {
// set to dilate
m_Image_ID_p1.GetItem(tx, ty) = 0;
return 0.5f; // 0.5 could use an intermediate value for texture filtering to look better?
}
int sample_counter = 0;
int nHits = 0;
Ray r;
for (int n = 0; n < m_Settings_AO_Samples; n++) {
// get the sample to look from
const AOSample &sample = samples[sample_counter++];
// wraparound
if (sample_counter == nSampleLocs)
sample_counter = 0;
r.o = sample.pos;
// construct a random ray to test
AO_RandomQMCDirection(r.d, sample.normal, n, qmc_variation);
// test ray
if (m_Scene.TestIntersect_Ray(r, m_Settings_AO_Range, m_Scene.m_VoxelRange)) {
nHits++;
}
} // for n
float fTotal = (float)nHits / m_Settings_AO_Samples;
fTotal = 1.0f - (fTotal * 1.0f);
if (fTotal < 0.0f)
fTotal = 0.0f;
return fTotal;
}
int AmbientOcclusion::AO_FindSamplePoints(int tx, int ty, const MiniList &ml, AOSample samples[MAX_COMPLEX_AO_TEXEL_SAMPLES]) {
int samples_found = 0;
int attempts = m_Settings_AO_Samples + 64;
// scale number of sample points roughly to the user interface quality
int num_desired_samples = m_Settings_AO_Samples;
if (num_desired_samples > MAX_COMPLEX_AO_TEXEL_SAMPLES)
num_desired_samples = MAX_COMPLEX_AO_TEXEL_SAMPLES;
Ray r;
for (int n = 0; n < attempts; n++) {
Vector2 st;
AO_RandomTexelSample(st, tx, ty, 10);
// find which triangle on the minilist we are inside (if any)
uint32_t tri_inside_id;
Vector3 bary;
if (!AO_FindTexelTriangle(ml, st, tri_inside_id, bary))
continue; // not inside a triangle, failed this attempt
// calculate world position ray origin from barycentric
Vector3 pos;
m_Scene.m_Tris[tri_inside_id].InterpolateBarycentric(pos, bary);
r.o = pos;
// calculate surface normal (should be use plane?)
const Vector3 &ptNormal = m_Scene.m_TriPlanes[tri_inside_id].normal;
// construct a random ray to test
AO_FindSamplesRandomRay(r, ptNormal);
// test ray
if (m_Scene.TestIntersect_Ray(r, m_Settings_AO_Range, m_Scene.m_VoxelRange, true))
continue; // hit, not interested
// no hit .. we can use this sample!
AOSample &sample = samples[samples_found++];
// be super careful with this offset.
// the find test offsets BACKWARDS to find tris on the floor
// the actual ambient test offsets FORWARDS to avoid self intersection.
// the ambient test could also use the backface culling test, but this is slower.
sample.pos = pos + (ptNormal * m_Settings_SurfaceBias); //0.005f);
sample.uv = st;
sample.normal = ptNormal;
sample.tri_id = tri_inside_id;
// finished?
if (samples_found == num_desired_samples)
return samples_found;
}
return samples_found;
}
} // namespace LM