-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmaterial.h
504 lines (476 loc) · 17.3 KB
/
material.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
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
#ifndef MATERIAL_H
#define MATERIAL_H
#include <algorithm>
#include "utils.h"
#include "runtime.h"
#include "ray.h"
#include "hit.h"
#include "light.h"
#include "texture.h"
class Material {
protected:
Material() {}
public:
virtual Vector3f shade(const Ray& ray, const Hit& hit, const PointLight& l) const=0;
virtual bool sample(Vector3f& col, const Hit& hit, Ray& ray, Vector3f& coe, Runtime& rt, bool emit) const=0;
virtual bool need_coords() const{
return 0;
}
virtual bool fix_norm() const {
return 1;
}
virtual bool is_brdf() const {
return 0;
}
};
//0: ??, 1: COS, 2: RAW
#define SAMPLER 1
class BRDFMaterial: public Material {
public:
const Texture diffuseColor;
const Texture specularColor;
float roughness;
const Texture emissionColor;
BRDFMaterial(const Texture& d_color = Vector3f::ZERO,
const Texture& s_color = Vector3f::ZERO,
float roughness = 0,
const Texture& e_color = Vector3f::ZERO) :
diffuseColor(d_color), specularColor(s_color),
roughness(roughness), emissionColor(e_color) {
}
//cos already applied
Vector3f BRDF(const Vector3f& in, const Vector3f& out, const Hit& hit) const {
Vector3f L = out.normalized();
Vector3f N = hit.norm;
float LN = Vector3f::dot(L, N);
if (LN <= 0) return Vector3f::ZERO;
Vector3f R = 2 * LN * N - L, V = -in; V.normalize();
Vector3f H = L + V; H.normalize();
float HN = Vector3f::dot(H, N), VH = Vector3f::dot(V, H),
VN = Vector3f::dot(V, N), LH = Vector3f::dot(L, H);
if (VN < 0) return Vector3f::ZERO; //?
float nhh = HN * HN;
float D = expf((nhh - 1) / (roughness * roughness * nhh)) / (roughness * roughness * nhh * nhh);
// float D = roughness * roughness / (PI * td * td);
float u = 1 - VN; //u^5
u *= u; u *= u; u *= 1 - VN;
Vector3f F = specularColor.get(hit.pos) + (1 - specularColor.get(hit.pos)) * u;
float G = std::min(std::min(HN * VN / VH, HN * LN / VH) * 2, float(1));
return LN * diffuseColor.get(hit.pos)
+ D * F * G / (4 * VN);
}
Vector3f BRDF0(const Vector3f& in, const Vector3f& out, const Hit& hit) const {
Vector3f L = out.normalized();
Vector3f N = hit.norm;
float LN = Vector3f::dot(L, N);
if (LN <= 0) return Vector3f::ZERO;
Vector3f R = 2 * LN * N - L, V = -in; V.normalize();
Vector3f H = L + V; H.normalize();
float HN = Vector3f::dot(H, N), VH = Vector3f::dot(V, H),
VN = Vector3f::dot(V, N), LH = Vector3f::dot(L, H);
if (VN < 0) return Vector3f::ZERO; //?
float nhh = HN * HN;
float D = expf((nhh - 1) / (roughness * roughness * nhh)) / (roughness * roughness * nhh * nhh);
// float D = roughness * roughness / (PI * td * td);
float u = 1 - VN; //u^5
u *= u; u *= u; u *= 1 - VN;
Vector3f F = specularColor.get(hit.pos) + (1 - specularColor.get(hit.pos)) * u;
float G = std::min(std::min(HN * VN / VH, HN * LN / VH) * 2, float(1));
return LN * diffuseColor.get(hit.pos);
}
Vector3f BRDF1(const Vector3f& in, const Vector3f& out, const Hit& hit) const {
Vector3f L = out.normalized();
Vector3f N = hit.norm;
float LN = Vector3f::dot(L, N);
if (LN <= 0) return Vector3f::ZERO;
Vector3f R = 2 * LN * N - L, V = -in; V.normalize();
Vector3f H = L + V; H.normalize();
float HN = Vector3f::dot(H, N), VH = Vector3f::dot(V, H),
VN = Vector3f::dot(V, N), LH = Vector3f::dot(L, H);
if (VN < 0) return Vector3f::ZERO; //?
float nhh = HN * HN;
float D = expf((nhh - 1) / (roughness * roughness * nhh)) / (roughness * roughness * nhh * nhh);
// float D = roughness * roughness / (PI * td * td);
float u = 1 - VN; //u^5
u *= u; u *= u; u *= 1 - VN;
Vector3f F = specularColor.get(hit.pos) + (1 - specularColor.get(hit.pos)) * u;
float G = std::min(std::min(HN * VN / VH, HN * LN / VH) * 2, float(1));
return D * F * G / (4 * VN);
}
Vector3f shade(const Ray& ray, const Hit& hit, const PointLight& l) const override{
return BRDF(ray.d, l.o - ray.pos(hit.t), hit) * l.c;
}
bool need_coords() const override{
return diffuseColor.need_coord() || specularColor.need_coord() || emissionColor.need_coord();
}
bool is_brdf() const override{
return 1;
}
bool sample(Vector3f& col, const Hit& hit, Ray& ray, Vector3f& coe, Runtime& rt, bool emit) const override{
RNG_TYPE& g = *rt.g;
// float& davg = rt.davg[this->id];
// float& favg = rt.favg[this->id];
// if (!this->emissionColor.empty) {
// std::cerr << emit << "?\n";
// }
if(emit) col += this->emissionColor.get(hit.pos) * coe;
if (Vector3f::dot(hit.norm, -ray.d) < 0) return 0;
Vector3f hitp = ray.pos(hit.t);
Vector3f p0 = hit.norm, p1, p2;
createCoordinateSystem(p0, p1, p2);
float r = this->roughness;
#if SAMPLER == 0
if (0)//g() % 2)
{
float cog = sqrtf(randf(g)), phi = randf(g) * PI * 2;
float sinTheta = sqrtf(1 - cog * cog);
float C = cog * 2;
float y = sinTheta * cosf(phi), z = sinTheta * sinf(phi);
Vector3f np = cog * p0 + y * p1 + z * p2;
Vector3f H = np - ray.d; H.normalize();
float co = Vector3f::dot(H, p0);
float D = expf((1 - 1.0 / (co * co)) / (r * r)) / (r * r * co * co * co);
if (D < 0) D = 0;
coe = coe * this->BRDF0(ray.d, np, hit) / cog;
// coe = coe * this->BRDF0(ray.d, np, hit) / cog / 2 * 2;// *((C * C) / (C * C + D * D)) * 2;
ray = Ray(hitp, np);
}
else
{
int fail = 0;
while (1)
{
float u = randf(g);
float co = sqrtf(1 / (1 - r * r * logf(1 - u)));
float d = expf((1 - 1.0 / (co * co)) / (r * r)) / (r * r * co * co * co);
// float D = d;// / co;
float phi = randf(g) * 2 * PI, g = sqrtf(1 - co * co);
Vector3f H = p0 * co + (p1 * cosf(phi) + p2 * sinf(phi)) * g;
Vector3f np = H * 2 * Vector3f::dot(H, -ray.d) + ray.d; np.normalize();
if (Vector3f::dot(np, p0) < 0)
{
fail++;
if (fail >= 20)
{
return 0;
}
continue;
}
// float C = Vector3f::dot(p0, np) * 2;
d /= Vector3f::dot(np, H) * 2;
davg = davg * 0.95 + 0.05 * d;
favg = favg * 0.95 + 0.05 * (1 + fail);
coe = coe * this->BRDF(ray.d, np, hit) / d * (davg/favg) / 2;
// coe = coe * this->BRDF1(ray.d, np, hit) / d * davg * 2;
// coe = coe * this->BRDF1(ray.d, np, hit) * davg / d;// *2;// *((D * D) / (C * C + D * D)) * 2;
ray = Ray(hitp, np);
break;
}
}
#elif SAMPLER == 1
float C = sqrtf(randf(g)), phi = randf(g) * PI * 2;
float sinTheta = sqrtf(1 - C * C);
float y = sinTheta * cosf(phi), z = sinTheta * sinf(phi);
Vector3f np = C * p0 + y * p1 + z * p2;
coe = coe * this->BRDF(ray.d, np, hit) / C / 2;
ray = Ray(hitp, np);
#else
float C = randf(g), phi = randf(g) * PI * 2;
float sinTheta = sqrtf(1 - C * C);
float y = sinTheta * cosf(phi), z = sinTheta * sinf(phi);
Vector3f np = C * p0 + y * p1 + z * p2;
coe = coe * this->BRDF(ray.d, np, hit);
ray = Ray(hitp, np);
#endif
#undef SAMPLER
return 1;
}
protected:
float clamp(float x)
{
if (x < 0) return 0;
if (x > 1) return 1;
return x;
}
};
class MirrorMaterial : public Material {
public:
const Texture reflectColor;
MirrorMaterial(const Texture & reflect_color = Vector3f::ZERO) :
reflectColor(reflect_color) {
}
Vector3f shade(const Ray& ray, const Hit& hit, const PointLight& l) const override {
return Vector3f(0, 0, 0);
}
bool need_coords() const override {
return reflectColor.need_coord();
}
bool sample(Vector3f& col, const Hit& hit, Ray& ray, Vector3f& coe, Runtime& rt, bool emit) const override {
RNG_TYPE& g = *rt.g;
coe = coe * reflectColor.get(hit.pos);
Vector3f np = hit.norm * 2 * Vector3f::dot(hit.norm, -ray.d) + ray.d; np.normalize();
ray.o = ray.pos(hit.t); ray.d = np; return 1;
}
};
class GlassMaterial : public Material {
public:
const Texture reflectColor;
const Texture refractColor;
float refractCoef, r0;
BRDFMaterial *m;
float thickness;
GlassMaterial(float coef = 1,
const Texture & reflect_color = Vector3f::ZERO,
const Texture & refract_color = Vector3f::ZERO,
float thickness = 0) :
refractCoef(coef),
reflectColor(reflect_color), refractColor(refract_color),
thickness(thickness) {
refractCoef = 1 / refractCoef;
r0 = (1 - refractCoef) / (1 + refractCoef); r0 *= r0;
m = new BRDFMaterial(Vector3f(0.5, 0.5, 0.5), Vector3f(0.5, 0.5, 0.5), 0.3);
}
Vector3f shade(const Ray& ray, const Hit& hit, const PointLight& l) const override {
return Vector3f(0, 0, 0);
}
bool need_coords() const override {
return reflectColor.need_coord() || refractColor.need_coord();
}
bool sample(Vector3f& col, const Hit& hit, Ray& ray, Vector3f& coe, Runtime& rt, bool emit) const override {
using std::cerr;
RNG_TYPE& g = *rt.g;
float co = Vector3f::dot(-ray.d, hit.norm);
float t = thickness;
if (co < 0) co = -co, t=0;
float si = sqrtf(std::max(1 - co * co, float(0)));
float si2 = si * refractCoef;
float co2 = sqrt(1 - si2 * si2);
float rs = (refractCoef * co2 - co) / (refractCoef * co2 + co);
float rp = (co2 - refractCoef * co) / (co2 + refractCoef * co);
float r = (rs * rs + rp * rp) / 2;
//cerr << co << " " << r << "\n";
// r = 0.25 + 0.5 * r;
//Vector3f i1=ray.d, i2;
if (randf(g) > r) {
coe = coe * refractColor.get(hit.pos);
ray.o = ray.pos(hit.t+ t);
}
else {
coe = coe * reflectColor.get(hit.pos);
Vector3f np = hit.norm * 2 * Vector3f::dot(hit.norm, -ray.d) + ray.d; np.normalize();
ray.o = ray.pos(hit.t); ray.d = np;
}
/*
i2 = ray.d;
if (Vector3f::dot(i1, -hit.norm) < 0) i1 = -i1;
if (Vector3f::dot(i2, hit.norm) < 0) i2 = -i2;
Vector3f b=m->BRDF(i1, i2, hit);*/
/*
if (b[0] > 2) b[0] = 2;
if (b[1] > 2) b[1] = 2;
if (b[2] > 2) b[2] = 2;*/
//coe = coe * b; //not actually physical but could work
return 1;
}
};
//maybe wrong, not sure
class SolidGlassMaterial : public Material {
public:
const Texture reflectColor;
const Texture refractColor;
float refractCoef, r0;
SolidGlassMaterial(float coef = 1,
const Texture& reflect_color = Vector3f::ZERO,
const Texture& refract_color = Vector3f::ZERO) :
refractCoef(coef),
reflectColor(reflect_color),refractColor(refract_color) {
refractCoef = 1 / refractCoef;
// r0 = (1 - refractCoef) / (1 + refractCoef); r0 *= r0;
}
Vector3f shade(const Ray& ray, const Hit& hit, const PointLight& l) const override {
return Vector3f(0, 0, 0);
}
bool need_coords() const override{
return reflectColor.need_coord() || refractColor.need_coord();
}
bool sample(Vector3f& col, const Hit& hit, Ray& ray, Vector3f& coe, Runtime& rt, bool emit) const override {
using std::cerr;
RNG_TYPE& g = *rt.g;
float co = Vector3f::dot(-ray.d, hit.norm);
// std::cerr << co << "sample!"<<" "<<hit.norm[0]<<"w"<<hit.norm[1]<<"w"<<hit.norm[2]<<" "<<ray.d[0]<<"w"<<ray.d[1]<<"w"<<ray.d[2]<<"\n";
if (co > 0) { //air->glass
float si = sqrtf(std::max(1 - co * co, float(0)));
float si2 = si * refractCoef;
// cerr << "check A " << si2 << "\n";
if (si2 <= 1) { //total reflection otherwise
float u = 1 - co;
float r = r0 + (1 - r0) * u * u * u * u * u;
r = 0.25 + 0.5 * r;
// cerr << "prob=" << r << " " << r0 << "\n";
if (randf(g) > r) //refract
{
//get refraction ray as a linear combination
float beta = asinf(si), alpha = asinf(si2);
// std::cerr << "refract A " << si << "->" << si2 << " "<<beta<<"->"<<alpha<<"\n";
float s = beta - alpha;
ray.o = ray.pos(hit.t);
// ray.d = -((-ray.d) * s + hit.norm).normalized();
if (si != 0) //unchanged otherwise
ray.d = (ray.d - hit.norm.normalized() * (sinf(s) / si)).normalized();
coe = coe * refractColor.get(hit.pos);
return 1;
}
}
}
else { //glass->air
co = -co;
float si = sqrtf(std::max(1 - co * co,float(0)));
float si2 = si / refractCoef;
// cerr << "check B " << si2 << " "<<ray.d[0]<<"w"<<ray.d[1]<<"w"<<ray.d[2]<<" "<<hit.t<<"\n";
// std::cerr << si << "w" << si2 << " "<< 1 - co * co <<"?\n";
if (si2 <= 1) { //total reflection otherwise
//get refraction ray as a linear combination
float beta = asinf(si), alpha = asinf(si2);
// std::cerr << "refract B " << si << "->" << si2 << " " << beta << "->" << alpha << "\n";
float s = beta - alpha;
Ray ray2;
ray2.o = ray.pos(hit.t);
if (si != 0) //unchanged otherwise
ray2.d = (ray.d + hit.norm.normalized() * (sinf(s) / si)).normalized();
else ray2.d = ray.d;
float u = 1 - Vector3f::dot(hit.norm, ray2.d);
// std::cerr << u << "?\n";
float r = r0 + (1 - r0) * u * u * u * u * u;
r = 0.25 + 0.5 * r;
// cerr << "prob=" << r << " "<<r0<<"\n";
if (randf(g) > r) //refract
{
// std::cerr << "cool refract!\n";
ray = ray2;
coe = coe * refractColor.get(hit.pos);
return 1;
}
}
}
// std::cerr << "reflect :(\n";
//reflect
coe = coe * reflectColor.get(hit.pos);
Vector3f np = hit.norm * 2 * Vector3f::dot(hit.norm, -ray.d) + ray.d; np.normalize();
ray.o = ray.pos(hit.t); ray.d = np; return 1;
/*
float co = Vector3f::dot(-ray.d, hit.norm);
bool in = co > 0; if (!in) co = -co;
float c = in ? (1 / refractCoef) : refractCoef;
float cos2t = 1 - c * c * (1 - co * co);
if (cos2t < 0) //internal
{
coe = coe * reflectColor.get(hit.pos);
Vector3f np = hit.norm * 2 * Vector3f::dot(hit.norm, -ray.d) + ray.d; np.normalize();
ray.o = ray.pos(hit.t); ray.d = np; return 1;
}
Vector3f rp;
float c = 1 - (in ? co : Vector3f::dot(rp, hit.norm));
float p = r0 + (1 - r0) * c * c * c * c * c;
if (randf(g) < p) { //reflect
coe = coe * reflectColor.get(hit.pos);
Vector3f np = hit.norm * 2 * Vector3f::dot(hit.norm, -ray.d) + ray.d; np.normalize();
ray.o = ray.pos(hit.t); ray.d = np; return 1;
}
else { //refract
coe = coe * reflectColor.get(hit.pos);
Vector3f np = hit.norm * 2 * Vector3f::dot(hit.norm, -ray.d) + ray.d; np.normalize();
ray.o = ray.pos(hit.t); ray.d = np; return 1;
}
bool into = n.dot(nl) > 0; // Ray from outside going in?
double nc = 1, nt = 1.5, nnt = into ? nc / nt : nt / nc, ddn = r.d.dot(nl), cos2t;
if ((cos2t = 1 - nnt * nnt * (1 - ddn * ddn)) < 0) // Total internal reflection
return obj.e + f.mult(radiance(reflRay, depth, Xi));
Vec tdir = (r.d * nnt - n * ((into ? 1 : -1) * (ddn * nnt + sqrt(cos2t)))).norm();
double a = nt - nc, b = nt + nc, R0 = a * a / (b * b), c = 1 - (into ? -ddn : tdir.dot(n));
double Re = R0 + (1 - R0) * c * c * c * c * c, Tr = 1 - Re, P = .25 + .5 * Re, RP = Re / P, TP = Tr / (1 - P);
return obj.e + f.mult(depth > 2 ? (erand48(Xi) < P ? // Russian roulette
radiance(reflRay, depth, Xi) * RP : radiance(Ray(x, tdir), depth, Xi) * TP) :
radiance(reflRay, depth, Xi) * Re + radiance(Ray(x, tdir), depth, Xi) * Tr);*/
}
};
/*
class SolidGlassMaterial : public Material {
public:
const Texture reflectColor;
const Texture refractColor;
float refractCoef;// , r0;
SolidGlassMaterial(float coef = 1,
const Texture & reflect_color = Vector3f::ZERO,
const Texture & refract_color = Vector3f::ZERO) :
refractCoef(coef),
reflectColor(reflect_color), refractColor(refract_color) {
refractCoef = 1 / refractCoef;
// r0 = (1 - refractCoef) / (1 + refractCoef); r0 *= r0;
}
Vector3f shade(const Ray& ray, const Hit& hit, const PointLight& l) const override {
return Vector3f(0, 0, 0);
}
bool need_coords() const override {
return reflectColor.need_coord() || refractColor.need_coord();
}
bool sample(Vector3f& col, const Hit& hit, Ray& ray, Vector3f& coe, Runtime& rt, bool emit) const override {
using std::cerr;
RNG_TYPE& g = *rt.g;
float co = Vector3f::dot(-ray.d, hit.norm);
// std::cerr << co << "sample!"<<" "<<hit.norm[0]<<"w"<<hit.norm[1]<<"w"<<hit.norm[2]<<" "<<ray.d[0]<<"w"<<ray.d[1]<<"w"<<ray.d[2]<<"\n";
if (co > 0) { //air->glass
float si = sqrtf(std::max(1 - co * co, float(0)));
float si2 = si * refractCoef;
// cerr << "check A " << si2 << "\n";
if (si2 <= 1) { //total reflection otherwise
float co2 = sqrt(1 - si2 * si2);
float rs = (refractCoef * co2 - co) / (refractCoef * co2 + co);
float rp = (co2 - refractCoef * co) / (co2 + refractCoef * co);
float r = (rs * rs + rp * rp) / 2;
// cerr << "prob=" << r << " " << r0 << "\n";
if (randf(g) > r) //refract
{
//get refraction ray as a linear combination
float beta = asinf(si), alpha = asinf(si2);
// std::cerr << "refract A " << si << "->" << si2 << " "<<beta<<"->"<<alpha<<"\n";
float s = beta - alpha;
ray.o = ray.pos(hit.t);
// ray.d = -((-ray.d) * s + hit.norm).normalized();
if (si != 0) //unchanged otherwise
ray.d = (ray.d - hit.norm.normalized() * (sinf(s) / si)).normalized();
coe = coe * refractColor.get(hit.pos);
return 1;
}
}
}
else { //glass->air
co = -co;
float si = sqrtf(std::max(1 - co * co, float(0)));
float si2 = si / refractCoef;
if (si2 <= 1) { //total reflection otherwise
//get refraction ray as a linear combination
float beta = asinf(si), alpha = asinf(si2);
float s = beta - alpha;
Ray ray2;
ray2.o = ray.pos(hit.t);
if (si != 0) //unchanged otherwise
ray2.d = (ray.d + hit.norm.normalized() * (sinf(s) / si)).normalized();
else ray2.d = ray.d;
float co2 = Vector3f::dot(hit.norm, ray2.d);
float rs = (refractCoef * co - co2) / (refractCoef * co + co2);
float rp = (co - refractCoef * co2) / (co + refractCoef * co2);
float r = (rs * rs + rp * rp) / 2;
if (randf(g) > r) //refract
{
ray = ray2;
coe = coe * refractColor.get(hit.pos);
return 1;
}
}
}
coe = coe * reflectColor.get(hit.pos);
Vector3f np = hit.norm * 2 * Vector3f::dot(hit.norm, -ray.d) + ray.d; np.normalize();
ray.o = ray.pos(hit.t); ray.d = np; return 1;
}
};*/
#endif