Fixed some major mathematical errors

This commit is contained in:
arvid schröder 2022-11-17 22:27:10 +01:00
parent 37753f96af
commit 70963aaf71

View file

@ -23,8 +23,8 @@ Color RefractionShader::shade(Scene const &scene, Ray const &ray) const {
// Check whether we are entering or leaving the object // Check whether we are entering or leaving the object
if (dotProduct(v, n) > 0) { if (dotProduct(v, n) > 0) {
eta1 = indexInside; n = -n;
eta2 = indexOutside; std::swap(eta1, eta2);
} }
// Calculate refraction percentage // Calculate refraction percentage
@ -49,17 +49,16 @@ Color RefractionShader::shade(Scene const &scene, Ray const &ray) const {
n * std::sqrt(denominator)); n * std::sqrt(denominator));
// Calculate reflected ray direction // Calculate reflected ray direction
Vector3d cosTheta1 = crossProduct(v, n); auto cosTheta1 = dotProduct(-n, normalized(v));
Vector3d cosTheta2 = -1.0f * crossProduct(n, t); auto cosTheta2 = std::sqrt(denominator);
Vector3d polarizationParallel = (eta2 * cosTheta1 - eta1 * cosTheta2) / auto polarizationParallel = (eta2 * cosTheta1 - eta1 * cosTheta2) /
(eta2 * cosTheta1 + eta1 * cosTheta2); (eta2 * cosTheta1 + eta1 * cosTheta2);
Vector3d polarizationOrthogonal = (eta1 * cosTheta1 - eta2 * cosTheta2) / auto polarizationOrthogonal = (eta1 * cosTheta1 - eta2 * cosTheta2) /
(eta1 * cosTheta1 + eta2 * cosTheta2); (eta1 * cosTheta1 + eta2 * cosTheta2);
Vector3d reflectionVector = normalized( auto reflectionFactor = (polarizationParallel * polarizationParallel + polarizationOrthogonal * polarizationOrthogonal) / 2;
0.5f * (polarizationParallel * polarizationParallel + polarizationOrthogonal * polarizationOrthogonal)); Vector3d reflectionVector = v - 2.0f * dotProduct(n, v) * n;
// Calculate rays // Calculate rays
Vector3d rayOrigin = ray.origin + ray.length * ray.direction; Vector3d rayOrigin = ray.origin + ray.length * ray.direction;
Ray refractionRay = Ray(rayOrigin, t); Ray refractionRay = Ray(rayOrigin, t);
@ -74,7 +73,7 @@ Color RefractionShader::shade(Scene const &scene, Ray const &ray) const {
Color refractionColor = scene.traceRay(refractionRay); Color refractionColor = scene.traceRay(refractionRay);
Color reflectionColor = scene.traceRay(reflectionRay); Color reflectionColor = scene.traceRay(reflectionRay);
Color resultingColor = denominator * refractionColor + (1-denominator) * reflectionColor; Color resultingColor = (1-reflectionFactor) * refractionColor + reflectionFactor * reflectionColor;
return resultingColor; return resultingColor;
} }