Implement texture coordinate for spheres

This commit is contained in:
CJSatnarine
2024-07-08 21:46:53 -04:00
parent 18b2acb1fe
commit 34586bc685

View File

@@ -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<material> 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;