-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMaterial.cpp
More file actions
52 lines (46 loc) · 1.62 KB
/
Copy pathMaterial.cpp
File metadata and controls
52 lines (46 loc) · 1.62 KB
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
#include "Material.h"
bool Lambertian::scatter(const Ray &rIn, const HitRecord &rec, Vec3f &attenuation, Ray &scattered) const
{
Vec3f scatterDirection = rec.normal + Vec3f::randomUnitVector();
if (scatterDirection.nearZero())
{
scatterDirection = rec.normal;
}
scattered = {rec.p, scatterDirection};
attenuation = albedo;
return true;
}
bool Metal::scatter(const Ray &rIn, const HitRecord &rec, Vec3f &attenuation, Ray &scattered) const
{
Vec3f reflected = rIn.dir.reflect(rec.normal);
reflected = reflected.unitVector() + (fuzz * Vec3f::randomUnitVector());
scattered = {rec.p, reflected};
attenuation = albedo;
return (scattered.dir.dot(rec.normal) > 0);
}
bool Dielectric::scatter(const Ray &rIn, const HitRecord &rec, Vec3f &attenuation, Ray &scattered) const
{
attenuation = {1.0, 1.0, 1.0};
float ri = rec.frontFace ? (1.0f / refractionIndex) : refractionIndex;
Vec3f unitDirection = rIn.dir.unitVector();
double cosTheta = std::fmin(-unitDirection.dot(rec.normal), 1.0);
double sinTheta = std::sqrt(1.0 - (cosTheta * cosTheta));
bool cannotRefract = ri * sinTheta > 1.0;
Vec3f direction;
if (cannotRefract || reflectance(cosTheta, ri) > randomFloat())
{
direction = unitDirection.reflect(rec.normal);
}
else
{
direction = unitDirection.refract(rec.normal, ri);
}
scattered = {rec.p, direction};
return true;
}
double Dielectric::reflectance(double cosine, double refractionIndex)
{
double r0 = (1 - refractionIndex) / (1 + refractionIndex);
r0 *= r0;
return r0 + ((1 - r0) * std::pow((1 - cosine),5));
}