Tangents for Mesh not Calculated Correctly?

Started by
-1 comments, last by BlueSpud 7 years, 7 months ago

I'm trying to implement normal mapping but I've been having some trouble calculating the tangents for my model. I'm loading in from an .obj exported from blender if that makes any difference. For each triangle (not currently using VBO indexing although I am averaging the tangents properly) I runt he following calculations to calculate the tangent:


glm::vec3 edge1 = _verts[vi.y] - _verts[vi.x];
glm::vec3 edge2 = _verts[vi.z] - _verts[vi.x];

glm::vec2 d_UV1 = _tex_coords[ti.y] - _tex_coords[ti.x];
glm::vec2 d_UV2 = _tex_coords[ti.z] - _tex_coords[ti.x];

float f = 1.0f / (d_UV1.x * d_UV2.y - d_UV1.y * d_UV2.x);

glm::vec3 tangent = glm::normalize((edge1 * d_UV2.y - edge2 * d_UV1.y) * f);

Where vi is a vector3 containing the indices of the 3 vertices and ti is also a vector3 containing the indices of the texture coordinates. I based the code off of here: http://www.opengl-tutorial.org/intermediate-tutorials/tutorial-13-normal-mapping/. When I try visualizing the tangents in the shader, all I am seeing is black on a cube, whereas the normal are showing color and when I make a TBN matrix and when I use a blank normal map I get some odd results that are not the same as the regular normals, even though in theory they should be. Any ideas?

This topic is closed to new replies.

Advertisement