# Issue with tangents/bitangents generation for normal maps

I'm generating tangents and bitangents for a normal mapping effect and while using the same method as several tutorials, I get tangents that are not very continuous. Here, we can see on the sphere especially that the tangents are very weird.

Also, my normal are totally correct and don't have this issue.

And when using these tangents with my normal mapping shader they "work", but create a very harsh "break" in the surface. Here is the code used to generate the tangents and bitangents :

for (size_t i = 0; i < out_vertices.size(); i += 3)
{
const auto& v0 = out_vertices[i];
const auto& v1 = out_vertices[i + 1];
const auto& v2 = out_vertices[i + 2];

const auto& uv0 = out_uvs[i];
const auto& uv1 = out_uvs[i + 1];
const auto& uv2 = out_uvs[i + 2];

const auto& dd_v0 = v1 - v0;
const auto& dd_v1 = v2 - v0;

const auto& dd_uv0 = uv1 - uv0;
const auto& dd_uv1 = uv2 - uv0;

float r = 1.0f / (dd_uv0.x * dd_uv1.y - dd_uv0.y * dd_uv1.x);
glm::vec3 tangent = glm::normalize((dd_v0 * dd_uv1.y - dd_v1 * dd_uv0.y) * r);
glm::vec3 bitangent = glm::normalize((dd_v1 * dd_uv0.x - dd_v0 * dd_uv1.x) * r);

out_tangents.push_back(tangent);
out_tangents.push_back(tangent);
out_tangents.push_back(tangent);

out_bitangents.push_back(bitangent);
out_bitangents.push_back(bitangent);
out_bitangents.push_back(bitangent);
}


I'm not sure if I need to average the (bi)tangents that share the same vertices/uvs, but I'm also not sure how to do it.

Thanks for any help.

You need to average tangents and binormals from the neighbor triangles to get a the correct result. I suggest you take a look at Martin Mittrings (Crytek) code: