From 34586bc68596399a04bbac5c6557f1074c0aba9a Mon Sep 17 00:00:00 2001 From: CJSatnarine Date: Mon, 8 Jul 2024 21:46:53 -0400 Subject: [PATCH] Implement texture coordinate for spheres --- sphere.h | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/sphere.h b/sphere.h index 041887b..2290602 100644 --- a/sphere.h +++ b/sphere.h @@ -18,6 +18,21 @@ class sphere : public hittable { return centre1 + time * centreVec; } + static void getSphereUV(const point3& p, double & u, double& v) { + // p: a given point on the sphere of radius one, centered at the origin. + // u: returned value [0,1] of angle around the Y axis from X=-1. + // v: returned value [0,1] of angle from Y=-1 to Y=+1. + // <1 0 0> yields <0.50 0.50> <-1 0 0> yields <0.00 0.50> + // <0 1 0> yields <0.50 1.00> < 0 -1 0> yields <0.50 0.00> + // <0 0 1> yields <0.25 0.50> < 0 0 -1> yields <0.75 0.50> + + auto theta = acos(-p.y()); + auto phi = atan2(-p.z(), p.x()) + pi; + + u = phi / (2 * pi); + v = theta / pi; + } + public: // Stationary sphere. sphere(const point3& centre, double radius, shared_ptr mat) : centre1(centre), radius(fmax(0, radius)), mat(mat), isMoving(false) { @@ -63,6 +78,7 @@ class sphere : public hittable { rec.p = r.at(rec.t); vec3 outwardNormal = (rec.p - centre) / radius; rec.setFaceNormal(r, outwardNormal); + getSphereUV(outwardNormal, rec.u, rec.v); rec.mat = mat; return true;