# Bump mapping a ray-traced sphere

I'm attempting to apply a height map to a ray-traced sphere. The height map is stored as a texture. I have the intersection point on the sphere $p$, and I compute the normal vector at that point $N = normalize(p - c)$, where $c$ is the center of the sphere.

I then use the components of the normalized $N$ with a sqrt and a couple of atan2's to get texture coordinates $u$ and $v$ (which correspond to sphere longitude and latitude, respectively.)

Unfortunately, I can't quite see how to get a perturbed normal from the height map. Here's what I have so far:

$p^{'} = p + h(u,v) N$ is the sphere perturbed by the height map $h$ in the direction of the normalized normal $N$.

The perturbed normal is then, by definition, $N^{'} = p^{'}_u \times p^{'}_v$.

The first question I have is, how do I know that normal points to the outside of the sphere? (To light the sphere properly, I do need outward-pointing normals.)

I know that, at worst, I can just run the shader and change the sign if it looks wrong, but I'd like to understand from first principles how I should order the cross product. I've looked around, and could not find any discussion of this point.

Now, we can define the sphere as $p = c + N r$ with $c$ the center and $r$ the scalar radius. Substituting this into the perturbed height map equation, we get

$p^{'} = c + N(r + h)$, which makes perfect sense: the radius is being perturbed by the height.

Differentiating that, we get

$p^{'}_u = N_u(r+h) + h_uN$ and $p^{'}_v = N_v(r+h) + h_vN$.

Computing the cross product directly, we get

$N^{'} = (N_u(r+h) + h_uN) \times (N_v(r+h) + h_vN)$

$N^{'} = (r+h)^2(N_u \times N_v) + (r+h)(h_u(N \times N_v) + h_v(N_u \times N))$

$N^{'} = (r+h)(N_u \times N_v) + h_u(N \times N_v) + h_v(N_u \times N)$

$N^{'} = (r+h)N + h_u(N \times N_v) + h_v(N_u \times N)$

(because $N \times N = 0$, the common factor $(r+h)$ will drop out from the normalized vector, and $N = N_u \times N_v$.)

The next question I have is that the sphere longitude is discontinuous ($u$ jumps from 1 to 0 as you cross the meridian); won't that cause the cross product to be computed incorrectly?

The final problem is, I'm not seeing how to compute $N(u,v)$. I'm just hitting a blank here. ($h_u$ and $h_v$ are easy enough to compute; they're just the derivative of the texture.)

Boy do I feel stupid. I just use spherical to cartesian to get a point on the sphere as a function of $u$ and $v$. And that is precisely the function $N(u,v)$. D'oh.

I still have my first question pending, and I'll see if there are any problems with singularities after I compute the derivative of $N$ (I suspect there will be no issues.)