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

前端 未结 1 425
没有蜡笔的小新
没有蜡笔的小新 2020-12-07 14:57

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

相关标签:
1条回答
  • 2020-12-07 15:28

    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);
    
    0 讨论(0)
提交回复
热议问题