Sign in to follow this  
Godlike

headache: tangent space normal mapping

Recommended Posts

Ive been trying for some time now to crate a glsl shader for tangent space normal mapping. Unfortunately despite my efforts I couldn't find a viable solution. In the above pic the right mesh appears correctly but the cube not. So I want to ask if the face tangents are incorrect or something. In the cube the bumps are facing out of the cube center. The tiny vectors are the blue for normal, red for tangent and green for bitangent. Pic: Code for calculating the tangents:
for( unsigned int i=0; i<tris_num; i++ )
{
	const triangle_t& tri = tris[i];
	const int vi0 = tri.vert_ids[0];
	const int vi1 = tri.vert_ids[1];
	const int vi2 = tri.vert_ids[2];
	const vec3_t& v0 = verts[ vi0 ].coords;
	const vec3_t& v1 = verts[ vi1 ].coords;
	const vec3_t& v2 = verts[ vi2 ].coords;
	vec3_t edge01 = v1 - v0;
	vec3_t edge02 = v2 - v0;
	vec2_t uvedge01 = uvs[vi1] - uvs[vi0];
	vec2_t uvedge02 = uvs[vi2] - uvs[vi0];


	float det = (uvedge01.Y * uvedge02.X) - (uvedge01.X * uvedge02.Y);
	DEBUG_ERR( IsZero(det) );
	det = 1.0f / det;

	vec3_t t = ( edge02 * uvedge01.Y - edge01 * uvedge02.Y ) * det;
	vec3_t b = ( edge02 * uvedge01.X - edge01 * uvedge02.X ) * det;

	t.Normalize();
	b.Normalize();

	face_tangents[i] = t;
	face_bitangents[i] = b;

}
Thanks in advance!

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this