Vertices in model space are composed in cartesian coordinates in 3D, where the domain of each dimension ranges from -1 to 1.

To convert sphere vertices from model space to UV space, a simple way is converting theta of a vertex in spherical space, and z value of the vertex in model space, to the UV space.

## Converting theta of a vertex in spherical space to the u value in UV space

Theta can be calculated using:

float theta = acos(x / sqrt(x * x + y * y));

The range of the calculated theta is [0, PI], because positive and negative y values yield the same result when calculating the radius (denominator). Thus, to expand the theta to [0, 2PI], we just need to extend theta when y is negative:

if (y < 0.0) theta = 2PI – theta;

Now we can convert theta to u:

float u = theta / 2PI;

The result of u can be visualized in the red channel of the fragment. Looking from the top of the sphere, we get the following result:

## Converting z value of a vertex in model space to the v value in UV space

v can be easily calculated by converting z from domain [-1, 1] to [0, 1]:

float v = 0.5 * (z + 1.0);

Visualize v in the blue channel of the fragment and looking at the sphere horizontally:

## One Response to “Convert Sphere Vertices from Model Space to UV Space in GLSL”

Post a comment## Trackbacks & Pingbacks