   Tangent space
  Hi !please, someone can explain me how to compute the tangent and the binormal of a point from a heightmap ?I only found methods to compute them from a vertex, but I am using a LOD algorithm and the vertex''s position change during rendering, and I don''t want to compute them at every change. That''s why I want to compute them from the initial heightmap. 
  The normal is given by the partial derivatives in x and y.You approximate them with the variation rates. Consider two vectors joining two consecutive samples in the x direction dz/dx = (1,0,z(x+1,y)-z(x,y))and in the y direction dy/dz = (0,1,z(x,y+1)-z(x,y)). Their cross product is // to the normal.N.x = -z(x+1,y)+z(x,y)N.y =  z(x,y+1)-z(x,y)N.z = 1Then normalize(N).I am not sure what you mean by "tangent" but for a point of a surface it''s a tangent plane. Which is given by the normal and the sample point. ;  A simple way would be:tangent = normal CROSS (1,0,0)You could replace cross product with swizzle operation. And the (x,x,x) vector depends on your texture orientation and coordinate system. You even don't have to normalize it becouse both parts of cross product are unit lenght.@Charles B: Tangents are used for bump mapping. You need normal, tangent and binormal to transform vectors from object space to tengent space and back.   You should never let your fears become the boundaries of your dreams.   [edited by - _DarkWIng_ on February 2, 2004 1:08:49 PM];quote:Original post by _DarkWIng_  ... You even don''t have to normalize it becouse both parts of cross product are unit lenght...Err... you do need to normalise it because only perpendicular vectors cross to produce a vector of the same length, sinceaXb = length ( a ) length ( b ) sin theta ( normal to a and b )and this is the same as the length of a * b if and only if theta is a right angle.Parallel vectors of any length always cross to give the zero vector.  "Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley ;  Darkwing :OK I googled and found this on the ATI site :"It is convenient to define a coordinate space local to each vertex which uses as its basis the vertex normal, a tangent to the surface which represents the direction of increase of the t texture coordinate and their cross product (aka the binormal)."I did not know about these qualifiers (tangent T and binormal B vectors) in the case of general texture mapping (that is parametric surfaces), never seen them in my math formation, and I doubt these are very academic terms. But they are actually the 2 vectors I have mentionned without naming them and it's better when one can put names on things. Well they are simply the tangents to the intersection curves between the surface and the x=xo and y=yo planes. And it's more logical to compute the normal out of them : N=T^B (instead of the contrary that you suggested).but I am using a LOD algorithm and the vertex's position change during rendering  Still it's unclear what Valfadir meant exactly here. Geomorphing ? What kind of position change is it all about.Else I am very experienced with terrain rendering. And I warn Valfadir that it's very damageable to use per vertex lighting with CLOD and terrain meshes. With CLOD you'll get huge light popping artefacts. You'll never have enough vertices to hide this undesired effects. You could reduce it by computing normals out of a mip mapped level of the same local resolution that the terrain mesh. That is reduce spatial frequencies when your mesh is less detailed. If your verts are 1 meter distant from each other pick the normal out of the mipmap level where height samples average square meter.Better use lightmaps with a much higher resolution that will model much more accurately the high spatial frequencies in lighting. I hope he uses the tangent space for something else than lighting. [edited by - Charles B on February 2, 2004 7:04:40 PM];quote:Original post by Paradigm Shifter  quote:Original post by _DarkWIng_   ... You even don''t have to normalize it becouse both parts of cross product are unit lenght...Err... you do need to normalise it because only perpendicular vectors cross to produce a vector of the same length, sinceaXb = length ( a ) length ( b ) sin theta ( normal to a and b )and this is the same as the length of a * b if and only if theta is a right angle.Parallel vectors of any length always cross to give the zero vector.  "Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley   Ups...  sorry about that. I messed up. You should never let your fears become the boundaries of your dreams. 