================= R_HeightmapToNormalMap it is not possible to convert a heightmap into a normal map properly without knowing the texture coordinate stretching. We can assume constant and equal ST vectors for walls, but not for characters. ================= */
| 71 | ================= |
| 72 | */ |
| 73 | static void R_HeightmapToNormalMap( byte *data, int width, int height, float scale ) { |
| 74 | int i, j; |
| 75 | byte *depth; |
| 76 | |
| 77 | scale = scale / 256; |
| 78 | |
| 79 | // copy and convert to grey scale |
| 80 | j = width * height; |
| 81 | depth = (byte *)R_StaticAlloc( j ); |
| 82 | for ( i = 0 ; i < j ; i++ ) { |
| 83 | depth[i] = ( data[i*4] + data[i*4+1] + data[i*4+2] ) / 3; |
| 84 | } |
| 85 | |
| 86 | idVec3 dir, dir2; |
| 87 | for ( i = 0 ; i < height ; i++ ) { |
| 88 | for ( j = 0 ; j < width ; j++ ) { |
| 89 | int d1, d2, d3, d4; |
| 90 | int a1, a3, a4; |
| 91 | |
| 92 | // FIXME: look at five points? |
| 93 | |
| 94 | // look at three points to estimate the gradient |
| 95 | a1 = d1 = depth[ ( i * width + j ) ]; |
| 96 | d2 = depth[ ( i * width + ( ( j + 1 ) & ( width - 1 ) ) ) ]; |
| 97 | a3 = d3 = depth[ ( ( ( i + 1 ) & ( height - 1 ) ) * width + j ) ]; |
| 98 | a4 = d4 = depth[ ( ( ( i + 1 ) & ( height - 1 ) ) * width + ( ( j + 1 ) & ( width - 1 ) ) ) ]; |
| 99 | |
| 100 | d2 -= d1; |
| 101 | d3 -= d1; |
| 102 | |
| 103 | dir[0] = -d2 * scale; |
| 104 | dir[1] = -d3 * scale; |
| 105 | dir[2] = 1; |
| 106 | dir.NormalizeFast(); |
| 107 | |
| 108 | a1 -= a3; |
| 109 | a4 -= a3; |
| 110 | |
| 111 | dir2[0] = -a4 * scale; |
| 112 | dir2[1] = a1 * scale; |
| 113 | dir2[2] = 1; |
| 114 | dir2.NormalizeFast(); |
| 115 | |
| 116 | dir += dir2; |
| 117 | dir.NormalizeFast(); |
| 118 | |
| 119 | a1 = ( i * width + j ) * 4; |
| 120 | data[ a1 + 0 ] = (byte)(dir[0] * 127 + 128); |
| 121 | data[ a1 + 1 ] = (byte)(dir[1] * 127 + 128); |
| 122 | data[ a1 + 2 ] = (byte)(dir[2] * 127 + 128); |
| 123 | data[ a1 + 3 ] = 255; |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | |
| 128 | R_StaticFree( depth ); |
| 129 | } |
| 130 |
no test coverage detected