Implement texture coordinate for spheres
This commit is contained in:
16
sphere.h
16
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<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;
|
||||
|
Reference in New Issue
Block a user