Original Post
Hi all, was trying to create a normalmap from a heightmap. Found this code by googleing, problem is to translate it, as i dont know what the, NormalizeFast and FloatToByte functions are doing ? Or dose anyone have a better function than this ? (was hopeing to get a resulting normalmap, that looks somewhat like the nVIDIA tool) Hope someone can help :)
// converts heightmap to normalmap (bumpmap)
void CImageTools::ConvertHeightMapToNormalMap( CImage &heightMap, CImage &normalMap, const float strength ) {
// error checking
if ( heightMap.GetDepth() != 1 ) {
Log << "<!> Cant convert heightmap to normalmap : invalid input image depth\n";
return;
}
const unsigned int width = heightMap.GetWidth();
const unsigned int height = heightMap.GetHeight();
normalMap.Set( width, height, 3 );
const float normY = 256.0f / strength;
Math::CVector3 normal;
unsigned int pos = 0;
for ( unsigned int y=0; y<height; ++y ) {
for ( unsigned int x=0; x<width; ++x ) {
normal.Set( 0.0f, normY, 0.0f );
normal.x -= static_cast<float>( heightMap.data[ heightMap.GetDataPos( x-1, y ) ] );
normal.x += static_cast<float>( heightMap.data[ heightMap.GetDataPos( x+1, y ) ] );
normal.z -= static_cast<float>( heightMap.data[ heightMap.GetDataPos( x, y-1 ) ] );
normal.z += static_cast<float>( heightMap.data[ heightMap.GetDataPos( x, y+1 ) ] );
normal.NormalizeFast();
normalMap.data[ pos++ ] = Math::FloatToByte( normal.x * 127.0f + 128.0f );
normalMap.data[ pos++ ] = Math::FloatToByte( normal.z * 127.0f + 128.0f );
normalMap.data[ pos++ ] = Math::FloatToByte( normal.y * 127.0f + 128.0f );
}
}
normalMap.SetDataFormat( GL_RGB, GL_RGB );
}