# Does anyone have a algorithm or code for generating a normal map ?

As the title says im sure with a bit of time i can implement one myself but if someone has the code already it would be appreciated as it would save time.

I have a program that will do it but that is somewhat inconvenient.

A normal map is quite simply just a representation of Vector3 as RGB (well, it’s mapped from -1…1 range to 0…1) … in Deferred Shading a Normal Map is often a regular part of the process and is quite easy to do in HLSL by simply writing normal data in PS

but I am not entirely sure, if that’s what you want to do …

Maybe i didn’t word that clearly. I mean to generate a normal map from a image.

for example texture image on the left the normal map on the right.
while i have a function in gimp to do this id like to generate them myself programmatically.

found here however im terrible at translating algebra to code as i simply have no formal mathematical training for all the symbolism used and reading the functions below into regular math is for me the equivalent of decoding assembly language or translating hieroglyphs, extremely time consuming and painfully annoying.

I figured maybe someone did this already and has it in a class file im sure its much simpler looking in code.

nVidia’s texture tools source has been floating around for a fairly long while. Uses the multiple sobel filters method (fine/medium/large frequency sliders you always see). https://github.com/castano/nvidia-texture-tools/tree/master/src/nvimage it’s mostly C-with-classes code and fairly straight-forward to port/read (at least compared to nVidia’s half-edge mesh) - results are roughly equivalent to most tools.

The math gist is just derivatives/slope, that’s why NVTT just skips straight to multiple sobel filters.

Works fine for surfaces that aren’t machined (or are old and dented machined), for clean machined surfaces you want to use a watershed fill to bias normals towards consistency.

You need to do it in realtime? Preferably on GPU?

Either way cpu would probably be fine.

I guess i can just try to dig it out of that texture tool later on its not a priority
I just wanted the function to do it figured other people had already done it.

Some time ago I wondered the same thing and searched over the net.
I found this link and last post seems easy to port to C# (haven’t done it yet, just copied it to harddrive and forgot it )

https://www.gamedev.net/forums/topic/475213-generate-normal-map-from-heightmap-algorithm/

I have had an idea creating models, textures (and normalmaps) on realtime randomly, but haven’t start it yet.

-m

edit: image should converted to bw first (like heightmap) and then use that algoritm.

Looks like there is two algorithms on that page.
nice find thanks.

Here are a few methods that might suit your needs: https://github.com/prime31/Nez/blob/master/Nez.Portable/Graphics/Textures/TextureUtils.cs