我的方法是分别计算两个平行于X轴和Y轴的切向量。然后计算叉积以找到法向量。
切向量由与两个最近的线段上的中点相交的直线给出,如下图所示。
我想知道是否有更直接的计算,或者在CPU周期方面更便宜。
发布于 2012-12-21 10:18:50
你实际上可以不使用叉积来计算它,通过使用“有限差分法”(或者至少我认为它是这样叫的)。
实际上,它已经足够快了,我可以用它来计算顶点着色器中的法线。
// # 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);
https://stackoverflow.com/questions/13983189
复制相似问题