OpenGL - How to calculate normals in a terrain height grid?
Asked Answered
K

1

24

My approach is to calculate two tangent vectors parallel to axis X and Y respectively. Then calculate the cross product to find the normal vector.

The tangent vector is given by the line that crosses the middle point on the two nearest segments as is shown in the following picture.

enter image description here

I was wondering whether there is a more direct calculation, or less expensive in terms of CPU cycles.

Kimmel answered 21/12, 2012 at 1:44 Comment(1)
Can I ask what you used to draw this image? It's really nice.Flexor
M
74

You can actually calculate it without a cross product, by using the "finite difference method" (or at least I think it is called in this way).

Actually it is fast enough that I use it to calculate the normals on the fly in a vertex shader.

  // # P.xy store the position for which we want to calculate the normals
  // # height() here is a function that return the height at a point in the terrain

  // read neightbor heights using an arbitrary small offset
  vec3 off = vec3(1.0, 1.0, 0.0);
  float hL = height(P.xy - off.xz);
  float hR = height(P.xy + off.xz);
  float hD = height(P.xy - off.zy);
  float hU = height(P.xy + off.zy);

  // deduce terrain normal
  N.x = hL - hR;
  N.y = hD - hU;
  N.z = 2.0;
  N = normalize(N);
Maymaya answered 21/12, 2012 at 2:18 Comment(10)
Too bad I can't give you more than +1: I doubled my FPS by using your simple algorithm!Scalp
Very nice, works perfectly for my pre calculations on the CPUForeland
Very nice! Can you extend your solution to 3D textures?Powerhouse
Is there somewhere a mathematical explaination for this?Chemurgy
That works flawlessly! Just a note: numbers in the "off" vector needs to be tweaked for your specific case (I had to use a slightly bigger offset to make it work in my case)Statuesque
not entirely sure why, but I had to use N.z =~ .06. Also, for unity users, z and y are flipped. Other than that it produces pretty decent results :)Mordant
A more expensive, but slightly more accurate version here https://mcmap.net/q/581762/-calculating-normals-in-a-triangle-meshMordant
ok that makes sense, N.z has to be 2 times your offset, so in my case I use .03 as offset giving N.z = .06Mordant
Consider your surface: z = h(x, y), where h is the height map. So, z - h(x,y) = 0 This is an equation is a contour at g=0 for the following function: g(x,y,z) = z - f(x,y) The gradient of g points in the direction of max increase. It is also orthogonal to the contour plane. So it is the normal. To find the discrete gradient, center at (x,y), and take the difference of its neighbors. grad_x = f(x+1,y) - f(x-1,y) / 2 grad_y = f(x,y+1) - f(x,y-1) / 2 grad_z = (z+1) - (z-1) / 2 = 2/2 = 1 The whole thing can be scaled without changing direction so multiply by 2. Then normalize. Tada!Christenson
Elegant solution, just tested it. Works like a charm.Lonnielonny

© 2022 - 2024 — McMap. All rights reserved.