23

我的方法是分别计算平行于 X 轴和 Y 轴的两个切向量。然后计算叉积以找到法向量。

切线向量由穿过两个最近线段中点的线给出,如下图所示。

在此处输入图像描述

我想知道是否有更直接的计算,或者在 CPU 周期方面更便宜。

4

1 回答 1

70

您实际上可以使用“有限差分法”(或者至少我认为以这种方式调用它)来计算它而无需叉积。

实际上它足够快,我用它来计算顶点着色器中的法线。

  // # 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);
于 2012-12-21T02:18:49.560 回答