# 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.)

• If you've found the solution, please go ahead and answer your question. :) – Julien Guertault Feb 10 '17 at 10:32
• You have ample resources on the Web to learn about basic geometry and basic principle of 3D rendering such as scratchapixel.com/lessons/… and scratchapixel.com/lessons/3d-basic-rendering/… for an introduction to cross product and shading. It will be very hard for to try to get something as advanced as bump mapping working if you don't even understand what a cross product is. Learn the basic first, then progress step by step. People are not here to teach. – user18490 Feb 11 '17 at 15:27
• @user18490 although we expect people to attempt to solve things themselves first, this question shows effort and describes the calculations and learning done so far, and I don't see a problem with it. I like your links to relevant resources but I strongly disagree with "People are not here to teach". There are lots of people here who put huge amounts of their time in to writing very helpful explanations in their many answers. – trichoplax Feb 12 '17 at 1:06
• @trichoplax. I understand your point but try in a constructive way to prove my point. Site like stack overflow are essentially there for people to ask questions and people to answer these questions. While you can always argue that any question is a valid question, answering things such as "how do we render an image of a 3D model" or in that case, "how I do I do bump mapping on a sphere while I don't even know what a cross product is" are just for people willing to help on these forum, questions that are unarguably too large and can't be answered unless you write a book. That's teaching. – user18490 Feb 12 '17 at 8:24
• I was attempting to derive from first principles how to bump map a ray-traced sphere, and had what I thought was a reasonable question (exactly how do you know which order u x v or v x u will get you a normal pointing away from the sphere center.) I don't consider my question a question in basic geometry... – Walt Donovan Feb 13 '17 at 0:40