The vectors you add to construct the tangents, (theta, 0.0, 0.0)
and (0.0, theta, 0.0)
, are not tangent to the sphere. To get a tangent and bitangent, you should use the cross product:
// pos x (1,0,0) could be 0, so add pos x (0,1,0).
vec3 vecTangent = normalize(cross(tePosition, vec3(1.0, 0.0, 0.0))
+ cross(tePosition, vec3(0.0, 1.0, 0.0)));
// vecTangent is orthonormal to tePosition, compute bitangent
// (rotate tangent 90° around tePosition)
vec3 vecBitangent = normalize(cross(vecTangent, tePosition));
vec3 ptTangentSample = noisy(tePosition + theta * normalize(vecTangent));
vec3 ptBitangentSample = noisy(tePosition + theta * normalize(vecBitangent));
vec3 vecNorm = normalize(
cross(ptTangentSample - tePosition, ptBitangentSample - tePosition));
As a side note, I would advise against using the same variables for vectors (direction+length, mathematically (x,y,z,0)) and points (positions in a coordinate system, mathematically (x,y,z,1)).