Normal map sampling (works, but why?)

I got normal mapping to work, but only after changing a line in the fragment shader to match other peoples code:

vec3 tangent_space_normal = 2 * texture(normal_map, in_texcoord).xyz - 1;

What is the purpose of multiplying the sampled normal by 2 and subtracting 1?

For instance, Sasha Willems does it like that here (Shaders section, near bottom of page):
Sasha Willems GLTF Scene Rendering

I’m using GLTF 2.0 and Blender.

That code is appropriate if the image format of your normal map are normalized integers on the range [0, 1]. The multiplication by 2 transforms the range to [0, 2], and the subtraction by 1 transforms it to [-1, 1].

1 Like

This topic was automatically closed 183 days after the last reply. New replies are no longer allowed.