Something wrong with my normal mapping code...

Started by
3 comments, last by Alberto Garc 7 years, 7 months ago

Ive got this normal map:

cuZsxeF.png

And, well, something isnt quite right:

LTVftlK.png

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);
	}
}

... My Vertex shader...:


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);

... And my Fragment shader:


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.

Advertisement

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?

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

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.

Don't pay much attention to "the hedgehog" in my nick, it's just because "Sik" was already taken =/ By the way, Sik is pronounced like seek, not like sick.

Maybe these tutorials can help you with your normal mapping shader:

http://www.geeks3d.com/20091019/shader-library-bump-mapping-shader-with-multiple-lights-glsl/

http://learnopengl.com/#!Advanced-Lighting/Normal-Mapping

Best regards,

Alberto.

This topic is closed to new replies.

Advertisement