# Something wrong with my normal mapping code...

This topic is 842 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Ive got this normal map:

And, well, something isnt quite right:

Here is my tangent generation:

void CalculateTangents() {
tangents.clear();
for (int i = 0; i < vertices.size(); i += 3) {
glm::vec3 edge1 = vertices[i + 1] - vertices[i];
glm::vec3 edge2 = vertices[i + 2] - vertices[i];
glm::vec2 deltaUV1 = uvs[i + 1] - uvs[i];
glm::vec2 deltaUV2 = uvs[i + 2] - uvs[i];

GLfloat f = 1.0f / (deltaUV1.x * deltaUV2.y - deltaUV2.x * deltaUV1.y);
glm::vec3 tangent;
tangent.x = f * (deltaUV2.y * edge1.x - deltaUV1.y * edge2.x);
tangent.y = f * (deltaUV2.y * edge1.y - deltaUV1.y * edge2.y);
tangent.z = f * (deltaUV2.y * edge1.z - deltaUV1.y * edge2.z);
tangent = glm::normalize(tangent);

tangents.push_back(tangent);
tangents.push_back(tangent);
tangents.push_back(tangent);
}
}


vec3 T = normalize(vec3(transform * vec4(tangent, 0.0)));
vec3 N = normalize(vec3(transform * vec4(normal, 0.0)));
T = normalize(T - dot(T, N) * N);
vec3 B = cross(T, N);
vs_out.TBN = mat3(T, B, N);


gNormal.xyz = normalize(fs_in.TBN * (texture(normal, fs_in.uv).rgb * 2.0 - 1.0));


The normals _should_ be in world space, so i cant quite find what's wrong.

##### Share on other sites

Not quite right how?  It looks like there's a light source off from the corner of the box somewhere, and that matches up on both sides.  Should it be lit from some other direction?

##### Share on other sites

its hard to say whatever is failing maybe you should explain what your code should work like

##### Share on other sites

Not quite right how?  It looks like there's a light source off from the corner of the box somewhere, and that matches up on both sides.  Should it be lit from some other direction?

The bottom side looks off, I think?

The problem may be the normal map though. It's trying to look like there's a hole there, but there isn't and prespective completely messes up the illusion, that's why normal map is normally limited to only small bumps. Parallax mapping is what you'd use for deep holes, but most of the time you aren't going to go that far.

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 10
• 11
• 13
• 9
• 11
• ### Forum Statistics

• Total Topics
634090
• Total Posts
3015432
×