Skip to content

get_sphere_uv does not correspond to its explanation #533

@D-K-E

Description

@D-K-E

The function get_sphere_uv in NextWeek listing 42 at section Texture 6.1, is given as

void get_sphere_uv(const point3& p, double& u, double& v) {
    auto phi = atan2(p.z(), p.x());
    auto theta = asin(p.y());
    u = 1-(phi + pi) / (2*pi);
    v = (theta + pi/2) / pi;
}

whereas in the formula given in the explanation is:

To compute θ and ϕ, for a given hitpoint, the formula for 
spherical coordinates of a unit radius sphere on the origin is:

x=cos(ϕ)cos(θ)
y=sin(ϕ)cos(θ)
z=sin(θ)

We need to invert that. Because of the lovely <cmath> 
function atan2() (which takes any number proportional 
to sine and cosine and returns the angle), we can pass in x
and y (the cos(θ) cancel):

ϕ=atan2(y,x)

The atan2
returns values in the range −π to π, so we need to 
take a little care there. The θ is more straightforward:

θ=asin(z)

which returns numbers in the range −π/2
to π/2. 

The problem also exist in dev-major branch.

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions