Friday, January 20, 2017

mathematics - Understanding normal maps on terrain


I'm having trouble understanding some of the math behind normal map textures even though I've got it to work using borrowed code, I want to understand it.



I have a terrain based on a heightmap. I'm generating a mesh of triangles at load time and rendering that mesh.


Now for each vertex I need to calculate a normal, a tangent, and a bitangent. My understanding is as follows, have I got this right?


normal is a unit vector facing outwards from the surface of the triangle. For a vertex I take the average of the normals of the triangles using that vertex.


tangent is a unit vector in the direction of the 'u' coordinates of the texture map.


As my texture u,v coordinates follow the x and y coordinates of the terrain, then my understanding is that this vector is simply the vector along the surface in the x direction. So should be able to calculate this as simply the difference between vertices in the x direction to get a vector, (and normalize it).


bitangent is a unit vector in the direction of the 'v' coordinates of the texture map.


As my texture u,v coordinates follow the x and y coordinates of the terrain, then my understanding is that this vector is simply the vector along the surface in the y direction. So should be able to calculate this as simply the difference between vertices in the y direction to get a vector, (and normalize it).


However the code I have borrowed seems much more complicated than this and takes into account the actual values of u, and v at each vertex which I don't understand the need for as they increase in exactly the same direction as x, and y.


I implemented what I thought from above, and it simply doesn't work, the normals are clearly not working for lighting.


Have I misunderstood something? Or can someone explain to me the physical meaning of the tangent and bitangent vectors when applied to a mesh generated from a hightmap like this, when u and v texture coordinates map along the x and y directions.



Thanks for any help understanding this.




No comments:

Post a Comment

Simple past, Present perfect Past perfect

Can you tell me which form of the following sentences is the correct one please? Imagine two friends discussing the gym... I was in a good s...