-
Notifications
You must be signed in to change notification settings - Fork 2
/
BRDF.h
426 lines (382 loc) · 15.3 KB
/
BRDF.h
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
423
424
425
426
#pragma once
#include "Vector.h"
#include <omp.h>
#include "MERLBRDFRead.h"
class MaterialValues {
public:
MaterialValues() {
shadingN[0] = 0; shadingN[1] = 1; shadingN[2] = 0;
Kd[0] = 0.5; Kd[1] = 0.5; Kd[2] = 0.5;
Ne[0] = 100; Ne[1] = 100; Ne[2] = 100;
Ks[0] = 0.; Ks[1] = 0.; Ks[2] = 0.;
Ke[0] = 0.; Ke[1] = 0.; Ke[2] = 0.;
Ksub[0] = 0.; Ksub[1] = 0.; Ksub[2] = 0.;
}
Vector shadingN, Kd, Ks, Ne, Ke, Ksub;
bool transp;
float refr_index;
};
class BRDF {
public:
BRDF() {};
virtual Vector sample(const MaterialValues& mat, const Vector& wi, const Vector& N, float &pdf, float r1, float r2, bool &has_sampled_diffuse) const = 0;
virtual Vector eval(const MaterialValues& mat, const Vector& wi, const Vector& wo, const Vector& N) const = 0;
Vector sample(const MaterialValues& mat, const Vector& wo, const Vector& N, float &pdf, bool &has_sampled_diffuse) const { // performs MIS Kd-Ks
int threadid = omp_get_thread_num();
float invmax = 1.f / engine[threadid].max();
return sample(mat, wo, N, pdf, engine[threadid]()*invmax, engine[threadid]()*invmax, has_sampled_diffuse);
}
};
class PhongBRDF : public BRDF {
public:
PhongBRDF() {};
static Vector random_Phong(const Vector &R, float phong_exponent, float r1, float r2) {
float facteur = sqrt(1 - std::pow(r2, 2.f / (phong_exponent + 1.f)));
//double facteur = sqrt(1 - fastPrecisePow(r2, 2. / (phong_exponent + 1)));
Vector direction_aleatoire_repere_local(cos(2 * M_PI*r1)*facteur, sin(2 * M_PI*r1)*facteur, std::pow(r2, 1. / (phong_exponent + 1)));
//Vector aleatoire(uniform(engine) - 0.5, uniform(engine) - 0.5, uniform(engine) - 0.5);
//Vector tangent1 = cross(R, aleatoire); tangent1.normalize();
Vector tangent1;
Vector absR(abs(R[0]), abs(R[1]), abs(R[2]));
if (absR[0] <= absR[1] && absR[0] <= absR[2]) {
tangent1 = Vector(0, -R[2], R[1]);
} else
if (absR[1] <= absR[0] && absR[1] <= absR[2]) {
tangent1 = Vector(-R[2], 0, R[0]);
} else
tangent1 = Vector(-R[1], R[0], 0);
tangent1.normalize();
Vector tangent2 = cross(tangent1, R);
return direction_aleatoire_repere_local[2] * R + direction_aleatoire_repere_local[0] * tangent1 + direction_aleatoire_repere_local[1] * tangent2;
}
Vector sample(const MaterialValues& mat, const Vector& wo, const Vector& N, float &pdf, float r1, float r2, bool &has_sampled_diffuse) const { // performs MIS Kd-Ks
int threadid = omp_get_thread_num();
float avgNe = (mat.Ne[0] + mat.Ne[1] + mat.Ne[2]) / 3.f;
Vector direction_aleatoire;
float p = 1 - (mat.Ks[0] + mat.Ks[1] + mat.Ks[2]) / 3.f;
/*if (s.objects[sphere_id]->ghost) {
p = std::max(0.2, p);
}*/
Vector R = (-wo).reflect(N); // reflect takes a ray going towards a surface and reflects it outwards of it
if (engine[threadid]() / (float)engine[threadid].max() < p) {
has_sampled_diffuse = true;
direction_aleatoire = random_cos(N, r1, r2);
} else {
has_sampled_diffuse = false;
direction_aleatoire = random_Phong(R, avgNe, r1, r2);
}
float proba_phong = (avgNe + 1) / (2.f*M_PI) * pow(dot(R, direction_aleatoire), avgNe);
float proba_globale = p * dot(N, direction_aleatoire) / (M_PI)+(1.f - p)*proba_phong;
pdf = proba_globale;
return direction_aleatoire;
}
Vector eval(const MaterialValues& mat, const Vector& wi, const Vector& wo, const Vector& N) const {
Vector reflechi = (-wo).reflect(N);
float d = dot(reflechi, wi);
if (d < 0) return mat.Kd / float(M_PI);
//Vector lobe = pow(Vector(d, d, d), mat.Ne) * ((mat.Ne + Vector(2., 2., 2.)) / (2.*M_PI));
Vector lobe = Vector(pow(d, mat.Ne[0])*(mat.Ne[0]+2.f)/M_TWO_PI, pow(d, mat.Ne[1])*(mat.Ne[1] + 2.f) / M_TWO_PI, pow(d, mat.Ne[2])*(mat.Ne[2] + 2.f) / M_TWO_PI);
//Vector lobe = fastPrecisePow(Vector(d, d, d), mat.Ne) * ((mat.Ne + Vector(2., 2., 2.)) / (2.*M_PI));
return mat.Kd / float(M_PI) + lobe * mat.Ks;
}
};
class LambertBRDF : public BRDF {
public:
LambertBRDF() {};
Vector sample(const MaterialValues& mat, const Vector& wo, const Vector& N, float &pdf, float r1, float r2, bool &has_sampled_diffuse) const { // performs MIS Kd-Ks
Vector direction_aleatoire = random_cos(N, r1, r2);
pdf = dot(N, direction_aleatoire) / (M_PI);
has_sampled_diffuse = true;
return direction_aleatoire;
}
Vector eval(const MaterialValues& mat, const Vector& wi, const Vector& wo, const Vector& N) const {
return mat.Kd / float(M_PI);
}
Vector Kd;
};
class TitopoBRDF : public BRDF { //our internal thetai,thetao, phio format
public:
TitopoBRDF(std::string filename, int Nthetai, int Nthetao, int Nphid) :Nthetai(Nthetai), Nthetao(Nthetao), Nphid(Nphid) {
data.resize(Nthetai * Nthetao * Nphid * 3);
FILE* f = fopen(filename.c_str(), "rb");
fread(&data[0], sizeof(float), Nthetai * Nthetao * Nphid * 3, f); // half
fclose(f);
}
Vector sample(const MaterialValues& mat, const Vector& wi, const Vector& N, float &pdf, float r1, float r2, bool &has_sampled_diffuse) const {
Vector d = random_cos(N, r1, r2);
pdf = dot(N, d) / (M_PI);
has_sampled_diffuse = false; // technically yes though
return d;
}
Vector eval(const MaterialValues& mat, const Vector& wi, const Vector& wo, const Vector& N) const {
/*Vector sumVal(0., 0., 0.);
for (int i = 0; i < Nthetai*Nthetao*Nphio; i++) {
sumVal += Vector(&data[i * 3]);
}
std::cout << sumVal[0] << " " << sumVal[1] << " " << sumVal[2] << std::endl;*/
Vector tangent1;
Vector absN(abs(N[0]), abs(N[1]), abs(N[2]));
if (absN[0] <= absN[1] && absN[0] <= absN[2]) {
tangent1 = Vector(0, -N[2], N[1]);
} else {
if (absN[1] <= absN[0] && absN[1] <= absN[2]) {
tangent1 = Vector(-N[2], 0, N[0]);
} else
tangent1 = Vector(-N[1], N[0], 0);
}
tangent1.normalize();
Vector tangent2 = cross(tangent1, N);
Vector wilocal = Vector(dot(wi, tangent1), dot(wi, tangent2), dot(wi, N));
Vector wolocal = Vector(dot(wo, tangent1), dot(wo, tangent2), dot(wo, N));
float thetai = acos(wilocal[2]);
if (thetai >= M_PI / 2) return Vector(0., 0., 0.);
float thetao = acos(wolocal[2]);
if (thetao >= M_PI / 2) return Vector(0., 0., 0.);
float phid = atan2(wolocal[1], wolocal[0]) - atan2(wilocal[1], wilocal[0]); // in -pi..pi
if (phid < 0) phid += 2 * M_PI; // => 0..2pi
if (phid < 0) phid += 2 * M_PI; // => 0..2pi
if (phid >= 2 * M_PI) phid -= 2 * M_PI; // => 0..2pi
if (phid >= 2 * M_PI) phid -= 2 * M_PI; // => 0..2pi
int idthetai = (int)(thetai / (M_PI / 2.)*Nthetai);
int idthetao = (int)(thetao / (M_PI / 2.)*Nthetao);
int idphid = (int)(phid / (M_PI * 2.)*Nphid);
int idthetai2 = ((idthetai < Nthetai - 1) ? (idthetai + 1) : idthetai);
int idthetao2 = ((idthetao < Nthetao - 1) ? (idthetao + 1) : idthetao);
int idphid2 = ((idphid < Nphid - 1) ? (idphid + 1) : idphid);
float fthetai = thetai / (M_PI / 2.)*Nthetai - idthetai;
float fthetao = thetao / (M_PI / 2.)*Nthetao - idthetao;
float fphid = phid / (M_PI * 2.)*Nphid - idphid;
Vector val000 = Vector(&data[(idthetai*Nthetao*Nphid + idthetao * Nphid + idphid) * 3]);
Vector val001 = Vector(&data[(idthetai*Nthetao*Nphid + idthetao * Nphid + idphid2) * 3]);
Vector val010 = Vector(&data[(idthetai*Nthetao*Nphid + idthetao2 * Nphid + idphid) * 3]);
Vector val100 = Vector(&data[(idthetai2*Nthetao*Nphid + idthetao * Nphid + idphid) * 3]);
Vector val101 = Vector(&data[(idthetai2*Nthetao*Nphid + idthetao * Nphid + idphid2) * 3]);
Vector val110 = Vector(&data[(idthetai2*Nthetao*Nphid + idthetao2 * Nphid + idphid) * 3]);
Vector val011 = Vector(&data[(idthetai*Nthetao*Nphid + idthetao2 * Nphid + idphid2) * 3]);
Vector val111 = Vector(&data[(idthetai2*Nthetao*Nphid + idthetao2 * Nphid + idphid2) * 3]);
Vector lerpVal = ((val000*(1.f - fphid) + val001 * fphid)*(1.f - fthetao) + (val010*(1.f - fphid) + val011 * fphid)*fthetao) * (1.f - fthetai)
+ ((val100*(1.f - fphid) + val101 * fphid)*(1.f - fthetao) + (val110*(1.f - fphid) + val111 * fphid)*fthetao) * fthetai;
return lerpVal /*/ Vector(1.0, 1.15, 1.66)*/;
}
std::vector<float> data;
int Nthetai;
int Nthetao;
int Nphid;
};
class IsoMERLBRDF : public BRDF { // todo
public:
IsoMERLBRDF(std::string filename) {
read_brdf(filename.c_str(), data);
}
void setParameters(const MaterialValues &m) {};
Vector sample(const MaterialValues& mat, const Vector& wi, const Vector& N, float &pdf, float r1, float r2, bool &has_sampled_diffuse) const {
Vector d = random_cos(N, r1, r2);
pdf = dot(N, d) / (M_PI);
has_sampled_diffuse = false; // technically yes though
return d;
}
Vector eval(const MaterialValues& mat, const Vector& wi, const Vector& wo, const Vector& N) const {
/*Vector sumVal(0., 0., 0.);
for (int i = 0; i < 90*90*360/2; i++) {
sumVal += Vector(data[i] * RED_SCALE, data[i + BRDF_SAMPLING_RES_THETA_H * BRDF_SAMPLING_RES_THETA_D*BRDF_SAMPLING_RES_PHI_D / 2] * GREEN_SCALE, data[i + BRDF_SAMPLING_RES_THETA_H * BRDF_SAMPLING_RES_THETA_D*BRDF_SAMPLING_RES_PHI_D] * BLUE_SCALE);
}
std::cout << sumVal[0] << " " << sumVal[1] << " " << sumVal[2] << std::endl;*/
Vector tangent1;
Vector absN(abs(N[0]), abs(N[1]), abs(N[2]));
if (absN[0] <= absN[1] && absN[0] <= absN[2]) {
tangent1 = Vector(0, -N[2], N[1]);
} else {
if (absN[1] <= absN[0] && absN[1] <= absN[2]) {
tangent1 = Vector(-N[2], 0, N[0]);
} else
tangent1 = Vector(-N[1], N[0], 0);
}
tangent1.normalize();
Vector tangent2 = cross(tangent1, N);
Vector wilocal = Vector(dot(wi, tangent1), dot(wi, tangent2), dot(wi, N));
Vector wolocal = Vector(dot(wo, tangent1), dot(wo, tangent2), dot(wo, N));
float thetai = acos(wilocal[2]);
if (thetai >= M_PI / 2) return Vector(0., 0., 0.);
float thetao = acos(wolocal[2]);
if (thetao >= M_PI / 2) return Vector(0., 0., 0.);
float phio = atan2(wolocal[1], wolocal[0]); // in -pi..pi
if (phio < 0) phio += 2 * M_PI; // => 0..2pi
float phii = atan2(wilocal[1], wilocal[0]); // in -pi..pi
if (phii < 0) phii += 2 * M_PI; // => 0..2pi
Vector result;
double r, g, b;
lookup_brdf_val(data, thetai, phii, thetao, phio, r, g, b);
result[0] = r;
result[1] = g;
result[2] = b;
return result;
}
double* data;
};
class Texture {
public:
Texture() { multiplier = Vector(1., 1., 1.); W = 0; H = 0; type = 0; };
Texture(const char* filename, int texType, const Vector &multiplier) : type(texType) {
W = 0;
H = 0;
switch (texType) {
case 0: loadColors(filename); break; // diffuse defaults to white
case 1: loadColors(filename); break; // specular defaults to black
case 2: loadNormals(filename); break; // normals defaults to 0,0,1
case 3: loadColors(filename); break; // alpha defaults to white
case 4: loadColors(filename); break; // roughness defaults to 1
case 5: loadColors(filename); break; // transparency defaults to 0
case 6: loadColors(filename); break; // refr index defaults to 1.3
}
this->multiplier = multiplier;
}
static float wrap(float u) {
//u = fmod(u, 1);
u -= (int)u;
if (u < 0) u += 1;
return u;
}
static Texture defaultNormal() {
return Texture("Null", true, Vector(0., 0., 1.));
}
static Texture defaultSpecular() {
return Texture("Null", false, Vector(0., 0., 0.));
}
static Texture defaultDiffuse() {
return Texture("Null", false, Vector(1., 1., 1.));
}
void clear_texture() {
W = 0;
H = 0;
filename = "Null";
values.clear();
}
__forceinline Vector getVec(float u, float v) const {
if (W > 0) {
// no assert on u and v ; assume they are btw 0 and 1
int x = u * (W - 1);
int y = v * (H - 1);
int idx = (y*W + x) * 3;
return Vector(values[idx] * multiplier[0], values[idx + 1] * multiplier[1], values[idx + 2] * multiplier[2]);
/*__m256 regV = _mm256_loadu_ps(&values[idx]);
__m256 regM = _mm256_loadu_ps(&multiplier[0]);
__m256 mul = _mm256_mul_ps(regV, regM);
float* mulf = (float*)&mul;
return Vector(mulf[0], mulf[1], mulf[2]);*/
} else {
return multiplier;
}
/*if (W > 0) {
// no assert on u and v ; assume they are btw 0 and 1
float x = u * (W - 1);
float y = v * (H - 1);
int ix = x;
int iy = y;
if (x > W - 2 || y > H - 2) {
int idx = (iy*W + ix) * 3;
return Vector(values[idx]* multiplier[0], values[idx + 1]* multiplier[1], values[idx + 2]* multiplier[2]);
} else {
float fracx = x - ix;
float fracy = y - iy;
int idx = (iy*W + ix) * 3;
int idxX = idx + 3;
int idxY = idx + 3 * W;
int idxXY = idxY + 3;
return multiplier*Vector((values[idx] * (1 - fracx) + values[idxX] * fracx)*(1 - fracy) + (values[idxY] * (1 - fracx) + values[idxXY] * fracx)*fracy,
(values[idx + 1] * (1 - fracx) + values[idxX + 1] * fracx)*(1 - fracy) + (values[idxY + 1] * (1 - fracx) + values[idxXY + 1] * fracx)*fracy,
(values[idx + 2] * (1 - fracx) + values[idxX + 2] * fracx)*(1 - fracy) + (values[idxY + 2] * (1 - fracx) + values[idxXY + 2] * fracx)*fracy);
}
} else {
return multiplier;
}*/
}
bool getBool(float u, float v) const {
if (W > 0) {
// no assert on u and v ; assume they are btw 0 and 1
int x = u * (W - 1);
int y = v * (H - 1);
int idx = (y*W + x) * 3;
float cr = values[idx] * multiplier[0];
return cr < 0.5f;
} else {
return (multiplier[0] < 0.5f);
}
}
Vector getNormal(float u, float v) const {
if (W > 0) {
// no assert on u and v ; assume they are btw 0 and 1
int x = u * (W - 1);
int y = v * (H - 1);
int idx = (y*W + x) * 3;
return Vector(values[idx], values[idx + 1], values[idx + 2]);
} else {
return Vector(0., 0., 1.f);
}
/*if (W > 0) {
// no assert on u and v ; assume they are btw 0 and 1
float x = u * (W - 1);
float y = v * (H - 1);
int ix = x;
int iy = y;
if (x > W - 2 || y > H - 2) {
int idx = (iy*W + ix) * 3;
return Vector(values[idx], values[idx + 1], values[idx + 2]);
} else {
float fracx = x - ix;
float fracy = y - iy;
int idx = (iy*W + ix) * 3;
int idxX = idx + 3;
int idxY = idx + 3 * W;
int idxXY = idxY + 3;
return Vector((values[idx] * (1 - fracx) + values[idxX] * fracx)*(1 - fracy) + (values[idxY] * (1 - fracx) + values[idxXY] * fracx)*fracy,
(values[idx+1] * (1 - fracx) + values[idxX+1] * fracx)*(1 - fracy) + (values[idxY+1] * (1 - fracx) + values[idxXY+1] * fracx)*fracy,
(values[idx+2] * (1 - fracx) + values[idxX+2] * fracx)*(1 - fracy) + (values[idxY+2] * (1 - fracx) + values[idxXY+2] * fracx)*fracy);
}
} else {
return Vector(0., 0., 1.);
}*/
}
float getValRed(float u, float v) const {
if (W > 0) {
// no assert on u and v ; assume they are btw 0 and 1
int x = u * (W - 1);
int y = v * (H - 1);
int idx = (y*W + x) * 3;
float cr = values[idx] * multiplier[0];
return cr;
} else {
return multiplier[0];
}
}
void loadColors(const char* file) {
if (file) {
filename = std::string(file);
if (load_image(file, values, W, H)) {
#pragma omp parallel for
for (int i = 0; i < values.size(); i++) {
values[i] /= 255.f;
values[i] = std::pow(values[i], 2.2f); // values are then gamma corrected.
}
}
}
}
void loadNormals(const char* file) {
if (file) {
filename = std::string(file);
if (load_image(file, values, W, H)) {
for (int i = 0; i < values.size() / 3; i++) {
Vector v(values[i * 3] - 128, values[i * 3 + 1] - 128, values[i * 3 + 2] - 128);
v.normalize();
values[i * 3] = v[0];
values[i * 3 + 1] = v[1];
values[i * 3 + 2] = v[2];
}
}
}
}
size_t W, H;
std::vector<float> values;
Vector multiplier;
std::string filename;
unsigned char type; // 0: diffuse, 1: specular, 2: normal, 3:alpha, 4: roughness
};