Height Map to Normal Map Algorithm

Started by
4 comments, last by jamie655321 12 years, 9 months ago
Hi guys,

Will you please point me in the right direction to write my own algorithm for creating a normal map from a height map. I tried a few things and they just aren't working correctly.

Thank you.
Advertisement
Normal generation for grids is pretty straightforward:

If you want to generate the normal for point P, consider its grid neighbors N1 to N4 laid out like this (seen from "above"):

X - N1 - X
| | |
N2 - P - N3
| | |
X - N4 - X


The 'X' are just points you don't care about.

Now

normal (P) = (N2 - N3) x (N1 - N4)
'x' is the cross product here, '-' is vector difference.

Note that P does not show up at the right hand side of the equation, and how the directions of the two difference vectors determine if your normal points up or down ("right hand rule").

Essentially, it's taking the two "bars" connecting N1/N4 and N2/N3 and calculate a vector perpendicular to these using the cross product.

Special consideration has to be given to the edges, where a point may have only two or three neighbors, instead of the regular four. If you try to tile height maps, you have to fill up the missing neighbors from the adjacent tiles. If you don't, use P instead of any missing neighbors.

Hope that helped!
I've seen this algorithm used in SDK's by NVidia and ATI.

http://en.wikipedia.org/wiki/Sobel_operator
When Sgt Barnes talks about Nx as vectors... how are they actually created. The only thing I thought of is if you just used the indices for the position into the grid of pixels. For instance, assuming the image was only 3x3 pixels in this case and the height at N1 was 2, the N1 vector would just be = (1, 2, 0). Would that work? Or is there something I'm missing?

EDIT: I tried out the method I was describing and ended up with this... which is definitely not what the GIMP Normal Map plugin splits out!!!
generatedplanetnormalma.png


Keep in mind, the height is stored in the alpha!
Douglas Eugene Reisinger II
Projects/Profile Site
Never mind, I solved it... if anyone wants to see the code lemme know :)
Douglas Eugene Reisinger II
Projects/Profile Site
Hey Palo, Could you post your code please?

Thanks,

This topic is closed to new replies.

Advertisement