void ComputeHeightfieldNormals(int xsize, int ysize, float *Heightmap, fvector *Normalmap) { int x, y; float l; fvector *N; int xsm = xsize - 1; int ysm = ysize - 1; for( y = 0; y < ysize; y++ ) for( x = 0; x < xsize; x++ ) { // Access current normalmap grid point N = &NormalMap[y*xsize+x]; // Compute normal by using the height differential N->x = HeightMap[y*xsize + ( !x ? 0 : x-1)] - HeightMap[y*xsize + ( x == xsm ? xsm : x+1 )]; N->y = HeightMap[( !y ? 0 : y-1)*xsize + x] - HeightMap[( y == ysm ? ysm : y+1 )*xsize + x]; N->z = (2.0f / (float)xsize) + (2.0f / (float)ysize); // Normalize it l = sqrt(N->x*N->x + N->y*N->y + N->z*N->z); if( l != 0 ) { N->x /= l; N->y /= l; N->z /= l; } else N->x = N->y = N->z = 0.0f; } }