2

I want to write an spherical mapping for my ray-tracer to generate UV-coordinates for the sphere. The texture applied with that mapping should looks like that: enter image description here

I have the sphere that is located at [0.0f,0.0f,0.0f] with the radius of 20.0f.

For given xyz point on sphere's surface (position where ray from ray-tracer hit the sphere) I calculate the uv coordinates with this formula:

float u = 0.5f + atan2(point.getX(), point.getZ()) / (2 * PI);
float v = 0.5f - asin(point.getY() / radius) / PI;

I decided to output u and v values to test the mapping.

As for v it looks quite ok (values from one pole to another, from 0.0f to 1.0f): enter image description here

But the u does not look so good: enter image description here Shouldn't it be an sphere where color depends on XZ (same for all points on the same longitude, gradually changing from one longitude to another)?

Am I wrong with interpretation of point, formula or something else?

PolGraphic
  • 3,233
  • 11
  • 51
  • 108

1 Answers1

0

The discontinuity at the centerline is because atan2() returns a longitude between -π and π. You divide this by 2π to scale it to between -0.5 and 0.5, then add 0.5 to scale it between 0 and 1. The result is a value that jumps discontinuously from 0 to 1, similar to how the local time suddenly jumps back by 24 hours as you cross the international date line, and for the same reason. To get a continuously-shaded sphere, map the value of u to luminance scaled to an even function, or better yet as a hue on the color wheel.

Davislor
  • 14,674
  • 2
  • 34
  • 49