Refraction is fully there, with magic fresnel!

This commit is contained in:
Godzil
2020-02-22 01:27:48 +00:00
parent e45dbad59e
commit 51a6bbebb9
9 changed files with 126 additions and 3 deletions

View File

@@ -23,6 +23,32 @@ struct Computation
object(object), t(t), hitPoint(point), eyeVector(eyev), normalVector(normalv), inside(inside),
overHitPoint(overHitP), underHitPoint(underHitP), reflectVector(reflectV), n1(n1), n2(n2) { };
double schlick()
{
/* Find the cos of the angle betzeen the eye and normal vector */
double cos = this->eyeVector.dot(this->normalVector);
double r0;
/* Total internal reflection can only occur when n1 > n2 */
if (this->n1 > this->n2)
{
double n, sin2_t;
n = this->n1 / this->n2;
sin2_t = (n * n) * (1.0 - (cos * cos));
if (sin2_t > 1.0)
{
return 1.0;
}
/* Compute the cos of theta */
cos = sqrt(1.0 - sin2_t);
}
r0 = ((this->n1 - this->n2) / (this->n1 + this->n2));
r0 = r0 * r0;
return r0 + (1 - r0) * ((1 - cos)*(1 - cos)*(1 - cos)*(1 - cos)*(1 - cos));
};
Shape *object;
double t;
Tuple hitPoint;

View File

@@ -104,6 +104,14 @@ Tuple World::shadeHit(Computation comps, uint32_t depthCount)
Tuple reflected = this->reflectColour(comps, depthCount);
Tuple refracted = this->refractedColour(comps, depthCount);
if ((comps.object->material.reflective > 0) && (comps.object->material.transparency > 0))
{
double reflectance = comps.schlick();
return surface + reflected * reflectance + refracted * (1 - reflectance);
}
return surface + reflected + refracted;
}