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.
I was wondering whether there is a more direct calculation, or less expensive in terms of CPU cycles.
A surface normal for a triangle can be calculated by taking the vector cross product of two edges of that triangle. The order of the vertices used in the calculation will affect the direction of the normal (in or out of the face w.r.t. winding).
A normal vector (or normal, for short) is a vector that points in a direction that's perpendicular to a surface. For a flat surface, one perpendicular direction is the same for every point on the surface, but for a general curved surface, the normal direction might be different at each point on the surface.
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With