Jump to content
  • Advertisement
Sign in to follow this  
Jim70

Something wrong with my normal mapping code...

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!