-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmaterial.h
68 lines (60 loc) · 2.1 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
#pragma once
#include "ray.h"
#include "hitable.h"
struct IMaterial {
virtual bool scatter(Ray& rayIn, HitRecord& rec, V3& attenuation, Ray& scatteredRay) const = 0;
};
struct Lambertian : IMaterial {
V3 albedo;
Lambertian(const V3& a) : albedo(a) {}
virtual bool scatter(Ray& rayIn, HitRecord& rec, V3& attenuation, Ray& scatteredRay) const {
V3 target = rec.p + rec.normal + randomInUnitSphere();
scatteredRay = Ray(rec.p, target-rec.p);
attenuation = albedo;
return true;
}
};
struct Metal : IMaterial {
V3 albedo;
float fuzz;
Metal(const V3& a, float f) : albedo(a) { if (f < 1) fuzz = f; else fuzz = 1; }
virtual bool scatter(Ray& rayIn, HitRecord& rec, V3& attenuation, Ray& scatteredRay) const {
V3 rayDir = rayIn.direction.getNormalized(); // TODO: check if this need to be normalized
V3 reflected = reflect(rayDir, rec.normal);
scatteredRay = Ray(rec.p, reflected + fuzz*randomInUnitSphere());
attenuation = albedo;
return (dot(scatteredRay.direction, rec.normal) > 0.0f);
}
};
struct Dielectric : IMaterial {
float refractionIndex;
Dielectric(float ri) : refractionIndex(ri) {}
virtual bool scatter(Ray& rayIn, HitRecord& rec, V3& attenuation, Ray& scatteredRay) const {
attenuation = V3(1.0f);
V3 outwardNormal, refractedRay;
float niOverNt, reflectProb, cosine;
V3 reflectedRay = reflect(rayIn.direction, rec.normal);
if (dot(rayIn.direction, rec.normal) > 0.0f) {
outwardNormal = -rec.normal;
niOverNt = refractionIndex;
cosine = refractionIndex*dot(rayIn.direction, rec.normal) / rayIn.direction.length();
} else {
outwardNormal = rec.normal;
niOverNt = 1.0f / refractionIndex;
cosine = -dot(rayIn.direction, rec.normal) / rayIn.direction.length();
}
if (refract(rayIn.direction, outwardNormal, niOverNt, refractedRay)) {
reflectProb = schlick(cosine,refractionIndex);
// scatteredRay = Ray(rec.p, refractedRay);
} else {
scatteredRay = Ray(rec.p, reflectedRay);
reflectProb = 1.0f;
}
if (getRandomNumber01() < reflectProb) {
scatteredRay = Ray(rec.p, reflectedRay);
} else {
scatteredRay = Ray(rec.p, refractedRay);
}
return true;
}
};