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);
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…