# Thread: Normal mapping

1. ## Normal mapping

So i have a vector, a surface normal, pointing away from the surface. i also have a vector, sampled from a normal map, indicating the normal vector of that fragment.
now i want the normal map vectors y axis to go along the surface normal.
for example: i have up-pointing normal map vector vec3(0.0, 0.8, 0.3) and my surface normal vec3(0, 0, 1). now i want to "rotate" my mapped vector, so it would result in vec3(0.0, 0.3, 0.8).
how would i have to calculate it, that it works with any two vectors???

2. To use a normal map, you need a tangent vector for each point on the surface, as well as a normal vector. Otherwise, there's simply no way to interpret the normals from the normal map. E.g. consider the vectors (0,0.5,0.866) and (0.5,0,0.866). Each of those deviates from the normal by 30 degrees, but one deviates along the X axis, the other along the Y axis. What does that mean in terms of the final normal vector?

Once you have a surface normal vector N and tangent vector T, you can derive a bitangent vector B=N×T, which is guaranteed to be perpendicular to N, and regenerate the tangent T'=B×N, which is guaranteed to be perpendicular to both, so T,B,N form an orthogonal basis. Then, given a surface-space (tangent-space) vector V from the normal map, the object-space vector V'=Vx*T+Vy*B+Vz*N (i.e. V'=M*V, where M is a matrix whose columns are T,B,N).

If you're generating the normal map for a specific surface e.g. based upon a higher-detail version of the surface, it doesn't particularly matter how you assign tangents. If you're tiling a normal map as a pattern, then you typically want the tangents to have a constant direction in texture space, so you'd use the tangent to lines of constant S or T. In the latter case, you can determine the tangent vector at a vertex by considering the difference in texture coordinates between that vertex and adjacent vertices.

3. I have tried using tangent vectors before, and doing all my calculations in tangent space. allthough this just causes problems with my code, and wont work for what i am doing.
i have thought of getting the x, y and z rotation values between the surface vector, and an uppointing vector. this would give me values to rotate the normal map vector by, beacuse a normap map vector is aligned to the y axis.
so:
i take the x, y and z rotational difference between surface and vec3(0,1,0) to get the rotations i need
then use rotation matrices to rotate the normal map vector according to the values i have.
use that as normal vector.

my problem is:
how would i get these rotation values?

4. You need more than one vector. Three coordinates require three axes, which requires at least two vectors (their cross product gives you a third which is guaranteed to be perpendicular to the other two).

If you're trying to navigate using coordinates, knowing which way is up doesn't help. You also need to know which direction is north (or east, or whatever).

5. Originally Posted by GClements
You need more than one vector. Three coordinates require three axes, which requires at least two vectors (their cross product gives you a third which is guaranteed to be perpendicular to the other two).
i have two vectors: the y-axis vector(0, 1, 0), and the surface normal. now i want to determine the angles on x y and z between those. maybe you are meaning something else with
Originally Posted by GClements
You need more than one vector.

6. still not working! help!

7. ## help

I now have this:
Code :
```        vec3 u = vec3(0, 0, 1);
vec3 v = unit_surface;
float xangle = acos(dot(u.yz, v.yz));
float yangle = acos(dot(u.xz, v.xz));
float zangle = acos(dot(u.xy, v.xy));

mat3 xrot;
mat3 yrot;
mat3 zrot;
RotMatX(xangle, xrot);
RotMatY(yangle, yrot);
RotMatZ(zangle, zrot);

vec3 normal_mapped = unit_normalmap * xrot * yrot * zrot;```

i noticed that the x, y and zangle variables represent incorrect values!
how do i get these?!?!?!?

8. Not sure if this is helpful. I only briefly read through the thread and I haven't done any normal mapping code in over a year and don't have any code examples handy I can show. So, I didn't have really anything helpful to add. But there might be some articles out there that explain the whole process and shed some light on the issue.

I think you have to calculate a tangent and a bitangent that align with the texture's UV coordinates on the surface of the triangle you are drawing, from what I can recall. I believe the normal map itself is an offset from the triangle's face normal for each pixel. At least, that's what I recall off the top of my head.

9. Originally Posted by BBeck1
Not sure if this is helpful. I only briefly read through the thread and I haven't done any normal mapping code in over a year and don't have any code examples handy I can show. So, I didn't have really anything helpful to add. But there might be some articles out there that explain the whole process and shed some light on the issue.