Skip to main content
gamedev.net
🔒 Locked

heightmap to normalmap

Started by Mythar Jun 2, 2006 at 10:16 AM 5 replies 1.1k views
Original Post
Mythar
Mythar
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 );
}

m4gnus
m4gnus
the functions do exactly what their names say. NormalizeFast is a (most probably optimized) function for normalizing a vector(the normal) and FloatToBytes convert a float value to a BYTE value.

regards,
m4gnus
"There are 10 types of people in the world... those who understand binary and those who don't."
anjee
anjee
Since you posted this in the DirectX forum, this might be useful:

Quote:

D3DXComputeNormalMap
Converts a height map into a normal map. The (x,y,z) components of each normal are mapped to the (r,g,b) channels of the output texture.

HRESULT D3DXComputeNormalMap(
LPDIRECT3DTEXTURE9 pTexture,
LPDIRECT3DTEXTURE9 pSrcTexture,
CONST PALETTEENTRY * pSrcPalette,
DWORD Flags,
DWORD Channel,
FLOAT Amplitude
);


:)
Mythar
Mythar
thx m4gnus : but i kinda already know that, what i need is the code for NormalizeFast and FloatToByte, sorry I did not specify this in my first post.

ajoling : I did look into this, but atm im not using D3DX9
andor
andor
Quote:
Original post by ajoling
Since you posted this in the DirectX forum, this might be useful:

Quote:

D3DXComputeNormalMap
Converts a height map into a normal map. The (x,y,z) components of each normal are mapped to the (r,g,b) channels of the output texture.

HRESULT D3DXComputeNormalMap(
LPDIRECT3DTEXTURE9 pTexture,
LPDIRECT3DTEXTURE9 pSrcTexture,
CONST PALETTEENTRY * pSrcPalette,
DWORD Flags,
DWORD Channel,
FLOAT Amplitude
);


:)


Hey, now there's a sexy function that I didn't know about! Neat.
anjee
anjee
Quote:
Original post by Mythar
thx m4gnus : but i kinda already know that, what i need is the code for NormalizeFast and FloatToByte, sorry I did not specify this in my first post.

ajoling : I did look into this, but atm im not using D3DX9


Ah, okay, sorry :).

You could search on "normalmap 127.5" (without quotes) since that'sa common value. A page with code from Id Software popped up:

https://zerowing.idsoftware.com/svn/radiant/GtkRadiant/trunk/plugins/shaders/shaders.cpp

Quote:

float norm = 1.0/sqrt(nx*nx + ny*ny + nz*nz);
out[0] = float_to_integer(((nx * norm) + 1) * 127.5);
out[1] = float_to_integer(((ny * norm) + 1) * 127.5);
out[2] = float_to_integer(((nz * norm) + 1) * 127.5);
out[3] = 255;


I used to have a working sample, but I removed it in favor of the other function, I think.
Mythar
Mythar
ajoling: thx got it working now :)

1 more question tho: what heightmap values should be used when , x-1 , y-1 becomes -1 etc. (when x,y value is outside the heightmap) ? wrap around ? or ?

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.