title: Generating a normal map from a height map?
body: <p>I'm working on procedurally generating patches of dirt using randomized fractals for a video game. I've already generated a height map using the midpoint displacement algorithm and saved it to a texture. I have some ideas for how to turn that into a texture of normals, but some feedback would be much appreciated.</p>
  
  <p>My height texture is currently a 257 x 257 gray-scale image (height values are scaled for visibility purposes):</p>
  
  <p><img src="https://i.stack.imgur.com/bPuyw.png" alt="enter image description here"></p>
  
  <p>My thinking is that each pixel of the image represents a lattice coordinate in a 256 x 256 grid (hence, why there are 257 x 257 heights). That would mean that the normal at coordinate (i, j) is determined by the heights at (i, j), (i, j + 1), (i + 1, j), and (i + 1, j + 1) (call those A, B, C, and D, respectively).</p>
  
  <p>So given the 3D coordinates of A, B, C, and D, would it make sense to:</p>
  
  <ol>
  <li>split the four into two triangles: ABC and BCD</li>
  <li>calculate the normals of those two faces via cross product</li>
  <li>split into two triangles: ACD and ABD</li>
  <li>calculate the normals of those two faces</li>
  <li>average the four normals</li>
  </ol>
  
  <p>...or is there a much easier method that I'm missing?</p>