我能够找到它Sobel Filter
用于从置换贴图创建法线贴图。它并不完美,因为我仍然需要在球体周围旋转法线,但它非常好。
vec4 GenerateNormal(int imgWid, int imgHei, GLuint *displacementMap, int texX, int texY)
{
float normalStrength = 8;
int xCoord = texX;// (int)(texX*(float)imgWid);
int yCoord = texY;// (int)(texY*(float)imgHei);
float tl = abs(GetDisplacement(displacementMap, texX, texY, imgWid, imgHei, -1, -1)); // top left
float l = abs(GetDisplacement(displacementMap, texX, texY, imgWid, imgHei, -1, 0)); // left
float bl = abs(GetDisplacement(displacementMap, texX, texY, imgWid, imgHei, -1, 1)); // bottom left
float t = abs(GetDisplacement(displacementMap, texX, texY, imgWid, imgHei, 0, -1)); // top
float b = abs(GetDisplacement(displacementMap, texX, texY, imgWid, imgHei, 0, 1)); // bottom
float tr = abs(GetDisplacement(displacementMap, texX, texY, imgWid, imgHei, 1, -1)); // top right
float r = abs(GetDisplacement(displacementMap, texX, texY, imgWid, imgHei, 1, 0)); // right
float br = abs(GetDisplacement(displacementMap, texX, texY, imgWid, imgHei, 1, 1)); // bottom right
// Compute dx using Sobel:
// -1 0 1
// -2 0 2
// -1 0 1
float dX = tr + 2 * r + br - tl - 2 * l - bl;
// Compute dy using Sobel:
// -1 -2 -1
// 0 0 0
// 1 2 1
float dY = bl + 2 * b + br - tl - 2 * t - tr;
// Build the normalized normal
vec4 N = vec4(normalize(vec3(dX, 1.0f / normalStrength, dY)), 1.0f);
//convert (-1.0 , 1.0) to (0.0 , 1.0), if needed
return normalize(N * 0.5f + 0.5f);
}