Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Explain sphere uv #762

Closed
wants to merge 12 commits into from
Closed

Explain sphere uv #762

wants to merge 12 commits into from

Conversation

hollasch
Copy link
Collaborator

Resolves #533

Also, get_sphere_uv() is in sphere.h for book 2, but hittable.h for book
3. This change also moves the function to sphere.h for book 3.

Resolves #533
The sphere::hit() function calls get_sphere_uv() with the intersection
point transformed to its position on a unit sphere centered at the
origin. But this calculation was already performed for the normal
vector, so we should just reuse that result instead of calculating it
twice.
The prior sphere::hit() methods duplicated the main update block with a
single change in variable value. This change solves and selects the
intersection root first, then updates the hit record for a good hit,
simplifying the code and the text.
@hollasch hollasch added this to the v3.2.2 milestone Oct 12, 2020
@hollasch hollasch requested review from trevordblack and a team October 12, 2020 05:52
@hollasch hollasch self-assigned this Oct 12, 2020
CHANGELOG.md Outdated Show resolved Hide resolved
@@ -43,10 +43,17 @@ bool sphere::bounding_box(double time0, double time1, aabb& output_box) const {
}


void get_sphere_uv(const point3& p, double& u, double& v) {
static void get_sphere_uv(const point3& p, double& u, double& v) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you want to make the source static, you'll need to make the inline src in the text static.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This really should be private, but everything's awkward with our approach of doing everything in a header.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Made private static.

@@ -1215,7 +1215,7 @@
vec3 outward_normal = (rec.p - center) / radius;
rec.set_face_normal(r, outward_normal);
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight
get_sphere_uv((rec.p-center)/radius, rec.u, rec.v);
get_sphere_uv(outward_normal, rec.u, rec.v);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This has some interesting implications.
The functionality is identical before and after change.

A texture shaded on the inside is reversed. Which is what you'd expect.

@trevordblack
Copy link
Collaborator

Uhhh, Steve...

Source:

    ...
    auto phi = atan2(p.z(), p.x());
    auto theta = asin(p.y());
    ...

Text:

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

The z and y in the latex equations in the text need be flipped. The source is correct

@trevordblack
Copy link
Collaborator

I'm pretty sure this is what the original issue was bringing up

@hollasch
Copy link
Collaborator Author

Interesting. Later comments suggest that there's no issue. I wasn't aware there was a disagreement between the source and the text; I'll revisit.

@trevordblack
Copy link
Collaborator

I added another comment that was what I think @D-K-E was referring to.

@hollasch
Copy link
Collaborator Author

Thanks. Having gone over it, I still think there are issues. What I'm wrestling with right now is a better explanation for the atan2 arguments, which are juggled to move past the sawtooth discrepancy in the return values (0→π / -π→0). I understand that it works, but am trying to find a way to develop it naturally.

@rjkilpatrick
Copy link
Member

rjkilpatrick commented Oct 21, 2020

The convention in ce97f02 is taking θ as the angle from the x-z plane and then ϕ as the polar angle from the z-axis, is this intended?
i.e.

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

Relates to the geometry:
IMG_20201021_233023
Which is atypical for spherical coordinates.
I would suggest following the original definition of θ being from the pole along the z-axis [0, π) and ϕ being the angle from the x-axis, in the x-y plane [0, 2π).

@trevordblack
Copy link
Collaborator

I think you might be slurring your variables a bit.
Our y axis points up.

Do you propose we make the z axis point up?

If you propose making the θ as the angle coming down from the up axis (in this case y), then, yes, we are considering this (and will probably do it). It's an old bug that no one caught cause there isn't a test for it

@hollasch
Copy link
Collaborator Author

I've always seen θ going around the equator (for example, longitude), and φ indicating the angle from one pole to the other (like latitude). Like John, I was all set to assert that the book was "nonstandard". Then I did an image search on {spherical coordinates}, and realized it was just another item on the list of

  • left-handed versus right-handed coordinates
  • clockwise vs counter-clockwise winding order
  • row major vs column major matrix memory layout
  • column vs row vectors
  • +Y up vs +Y down
  • and on and on and on and on and on and on and on and on and on and on and on and on and on and on and on and on and on and on and on and on and on and on and on and on and on and on and on and on and on and on and on and on and on and on and on and on and on and on

What's really needed here is a proper illustration (in the same manner that John posted above) and a bit more explanation.

@trevordblack
Copy link
Collaborator

I have an immense urge to go through your list and explain how there's EXACTLY one right option for each...

@hollasch
Copy link
Collaborator Author

This change has been split up into and is superseded by #774 and #775

@hollasch hollasch closed this Oct 25, 2020
@hollasch hollasch deleted the explain-sphere-uv branch August 20, 2021 23:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants