From bf18756a3d8b56f18198607f0eb191b4bb3faf28 Mon Sep 17 00:00:00 2001 From: Yoel <s73017@beuth-hochschule.de> Date: Tue, 16 Mar 2021 14:38:52 +0100 Subject: [PATCH] Sphere intersection optimized --- RayTracer/shape/Sphere.cpp | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/RayTracer/shape/Sphere.cpp b/RayTracer/shape/Sphere.cpp index 65ab64f..87ca48d 100644 --- a/RayTracer/shape/Sphere.cpp +++ b/RayTracer/shape/Sphere.cpp @@ -19,21 +19,17 @@ std::optional<cam::Hit> Sphere::intersect(const cam::Ray& r) const { float discrim = b * b - 4 * a * c; if (discrim >= 0) { - float t1 = (-b - sqrt(b * b - 4 * a * c)) / (2 * a); + float t1 = (-b - sqrt(discrim)) / (2 * a); if (r.in_range(t1)) { util::Vec3 t1HitPoint = r(t1); - float theta = acos(t1HitPoint.y() / radius); - float phi = M_PI + atan2(t1HitPoint.x(), t1HitPoint.z()); return std::optional<cam::Hit>({t1HitPoint, t1HitPoint, texture_coordinates(t1HitPoint), t1, material}); } else { - float t2 = (-b + sqrt(b * b - 4 * a * c)) / (2 * a); + float t2 = (-b + sqrt(discrim)) / (2 * a); if (r.in_range(t2)) { util::Vec3 t2HitPoint = r(t2); - float theta = acos(t2HitPoint.y() / radius); - float phi = M_PI + atan2(t2HitPoint.x(), t2HitPoint.z()); return std::optional<cam::Hit>({t2HitPoint, t2HitPoint, texture_coordinates(t2HitPoint), t2, material}); @@ -74,10 +70,12 @@ util::Vec3 Sphere::calculateLightEmission(const util::SurfacePoint& p, // the light in regard to the angle. // Uniform pdf of shape is 1/area, converting to pdf over solid angle is // pdf/(dot/length^2). + // This is wrong. We just need the normal pdf, per area, as we do not sample + // with regard to a direction. auto emission = p.emission(); auto dot = std::max<float>(util::dot(p.normal(), d.normalize()), 0); auto area = 4 * M_PI * std::pow(radius, 2); - auto pdf = std::pow(d.length(), 2) / (dot * area); + auto pdf = 1 / area; return emission / pdf; } } // namespace shapes -- GitLab