Jump to content
  • Advertisement
Sign in to follow this  
Waaayoff

Normal map from heightmap?

This topic is 2207 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I think i read somewhere that if you generate a normal map from the heightmap you don't need normal, binormal and tangent vectors per vertex to transform into object space? I'm not sure but i want to use normal maps to do lighting in my terrain.

Share this post


Link to post
Share on other sites
Advertisement
Here is the code I use to to this:


for(int z = 0; z<terrainHeight; z++){
for(int x = 0; x<terrainWidth; x++){
indexNm = z*terrainWidth+x;

if(z > terrainHeight-1){ //aboth
nmOffset = terrainWidth;
}else{
nmOffset = 0;
}
D3DXVECTOR3 N1 = D3DXVECTOR3(x, heightMap[indexNm+nmOffset],z+1);

if(x > 0){ //left
nmOffset = -1;
}else{
nmOffset = 0;
}
D3DXVECTOR3 N2 = D3DXVECTOR3(x-1, heightMap[indexNm+nmOffset],z);

if(x > terrainWidth-1){ //right
nmOffset = 1;
}else{
nmOffset = 0;
}
D3DXVECTOR3 N3 = D3DXVECTOR3(x+1, heightMap[indexNm+nmOffset],z);

if(z > 0){//below
nmOffset = -terrainWidth;
}else{
nmOffset = 0;
}
D3DXVECTOR3 N4 = D3DXVECTOR3(x, heightMap[indexNm+nmOffset],z-1);
D3DXVec3Cross(&normalMap[indexNm], &(N2 - N3), &(N1 - N4)); // cross and done!
D3DXVec3Normalize(&normalMap[indexNm], &normalMap[indexNm]);
}
}

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!