0
\$\begingroup\$

I'm implementing normal mapping and was trying different techniques of doing that. All seemed not to give me a correct result (e.g. the normals point in weird directions). To find the problem, I broke everything down to the simplest possible solution, but I cant find the problem.

Here is a picture of my scene outputting only the world-space normals:

enter image description here

And here is the scene outputting the transformed normals with the normal map:

enter image description here

As you can see, the normals are not pointed the way they should be (green should be to the right, for example). Here is the way I caluclate the TBN-Matrix (I know, its not the correct way of doing that, but I tried the "correct" ways too).

vec3 tangent = cross(worldspaceNormal, vec3(0,0,1));
vec3 biTangent = cross(tangent, worldspaceNormal);
mat3 tbn = mat3(tangent, biTangent, worldspaceNormal);

vec3 map = texture(normal, texCoord).rgb;
map = 2 * map - vec3(1);

outNormal = vec4(tbn * map, 0.0);

The worldspaceNormal is just the normal of the surface multiplied by the model-matrix, with w-component 0.0, of course.

\$\endgroup\$
  • \$\begingroup\$ Red (.x) is up in world space? Is that common in opengl? TBN vs -> ps looks like mine usually (in HLSL), but ofc i use mul() instead of *. But i guess i'd expect green (.y) to be up? \$\endgroup\$ – Stormwind Aug 25 '16 at 12:45
  • \$\begingroup\$ Yes, green is usually up, its just the lookAt matrix with z being up. \$\endgroup\$ – Liess Jemai Aug 25 '16 at 16:06
  • \$\begingroup\$ Ok.. (nm lookat). I just wonder if you should have worldspaceNormal.yxz or worldspaceNormal.zxy everywhere Plus that the normals in the texture must fit accordingly. You have the .xyz components correctly, not such that for example .y is omitted? \$\endgroup\$ – Stormwind Aug 25 '16 at 17:23
  • \$\begingroup\$ Found the problem. The TBN apparently needs to be constructed as the following: mat3(worldspaceNormal, biTangent, tangent);. So tangent and normal need to be flipped. Don't know why, maybe a Vulkan thing. \$\endgroup\$ – Liess Jemai Aug 25 '16 at 18:17
  • \$\begingroup\$ Hmok, yes the order must be correct. And it's always good to ensure it is homogenous throughout the application. Ie. if you try with your other 3D models and normal textures, they all behave well :-). \$\endgroup\$ – Stormwind Aug 25 '16 at 19:12
1
\$\begingroup\$

Found the problem. The TBN apparently needs to be constructed as the following: mat3(worldspaceNormal, biTangent, tangent);. So tangent and normal need to be flipped. Don't know why, maybe a Vulkan thing.

\$\endgroup\$
0
\$\begingroup\$

The normal matrix should be the transpose inverse of the modelview matrix. You can't simply multiply with model-matrix. Hope this clears your problem.

\$\endgroup\$
  • \$\begingroup\$ Unfortunately not. You only need to multiply by the normal-matrix if you use non-uniform scaling. \$\endgroup\$ – Liess Jemai Aug 24 '16 at 22:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.