<p>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.</p>

<p>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.)</p>

<p>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>$p^{'} = p + h(u,v) N$ is the sphere perturbed by the height map $h$ in the direction of the normalized normal $N$.</p>

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

<p>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.)</p>

<p>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.</p>

<p>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>

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

<p>Differentiating that, we get</p>

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

<p>Computing the cross product directly, we get</p>

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

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

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

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

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

<p>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?</p>

<p>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.)</p>

<hr>

<p>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.</p>

<p>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.)</p>

CC BY-SA 3.0
2017-02-10T10:32:10.340Z
If you've found the solution, please go ahead and answer your question. :)
Julien Guertault
CC BY-SA 3.0
2017-02-11T15:27:54.290Z
You have ample resources on the Web to learn about basic geometry and basic principle of 3D rendering such as https://www.scratchapixel.com/lessons/mathematics-physics-for-computer-graphics/geometry/math-operations-on-points-and-vectors and https://www.scratchapixel.com/lessons/3d-basic-rendering/introduction-to-shading 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
CC BY-SA 3.0
2017-02-12T01:06:30.010Z
@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 is on Codidact now
CC BY-SA 3.0
2017-02-12T08:24:19.817Z
@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
CC BY-SA 3.0
2017-02-12T13:39:22.857Z
@user18490 I agree we do not want questions that are too broad. It seems we disagree on whether this particular question is too broad (I see the body of the question as more specific than the broad title). However, this is a community decision, and you are part of the community, so I encourage you to suggest closing the question, and then the community will vote on whether to close. [More information on flagging to close questions here.](http://meta.stackoverflow.com/questions/334115/without-close-flag-privileges-how-do-i-bring-a-low-quality-question-to-the-comm/334118)
trichoplax is on Codidact now
CC BY-SA 3.0
2017-02-13T00:40:04.110Z
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
CC BY-SA 3.0
2017-02-13T00:47:44.100Z
Also, I wasn't able to find an answer to my question in any of the 3D graphics texts I had or on the Web. If you've got such a link, please post it.
Walt Donovan
<raytracing><computational-geometry><normal-mapping><height-field>
Bump mapping a ray-traced sphere
Walt Donovan
# Bump mapping a ray-traced sphere

(Asked by: username_0 on 2017-02-09)

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

**Tags:** raytracing, computational-geometry, normal-mapping, height-field

> **Comments:**
> **username_1**: If you've found the solution, please go ahead and answer your question. :)  
> **username_2**: You have ample resources on the Web to learn about basic geometry and basic principle of 3D rendering such as https://www.scratchapixel.com/lessons/mathematics-physics-for-computer-graphics/geometry/math-operations-on-points-and-vectors and https://www.scratchapixel.com/lessons/3d-basic-rendering/introduction-to-shading 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.  
> **username_3**: @username_2 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.  
> **username_2**: @username_3. 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.  
> **username_3**: @username_2 I agree we do not want questions that are too broad. It seems we disagree on whether this particular question is too broad (I see the body of the question as more specific than the broad title). However, this is a community decision, and you are part of the community, so I encourage you to suggest closing the question, and then the community will vote on whether to close. [More information on flagging to close questions here.](http://meta.stackoverflow.com/questions/334115/without-close-flag-privileges-how-do-i-bring-a-low-quality-question-to-the-comm/334118)  
> **username_0**: 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...  
> **username_0**: Also, I wasn't able to find an answer to my question in any of the 3D graphics texts I had or on the Web. If you've got such a link, please post it.  

---

## 0 answers

