Sign in to follow this  
dsh

OpenGL Tangent Space & Bump Mapping - inconsistent tangent vectors [SOLVED]

Recommended Posts

I'm playing with OpenGL effects via GLSL. I tesselate a sphere by subdividing an octahedron. Then I generate UV coordinates and fix the seam by duplicating vertices on it and adjusting tex coords. Normals are calculated per-face and then smoothed by averaging normals of each face that contains the vertex currently processed. I did the same with the (T)angent vector - calculated per face and then smoothed for each vertex. The problem is that in some areas of the sphere (or ellipsoid) T vectors of adjacent triangles face opposite directions. This causes strange artifacts (like strange edges or beveled areas) that are especially visible with specular lighting. Rendering the final effect. The edges are not the normal map - they're artifacts! Mapping tangent vectors to gl_FragColor: Light vector visualisation converted to tangent space: (I realized that the last screenshot was using a different method of averaging tangents than the one I wrote about). Anybody knows how to fix this? I've read other posts on similar topic here but they were about uv mirroring issues - I don't have any mirroring. I haven't done much 3D (or advanced 3D) so treat me as a newbie. ================================== I fixed this. Now it works perfectly. (1) At first I calculated tangents per face and then, for every vertex, averaged the tangents from faces containing that vertex. (2) Later I came up with another method. a) For every vertex find all faces that it is part of b) Compute tangents for each face treating current vertex as the first one in the tangent calculations c) Sum and normalize the results and store it as the vertex's tangent This method also produced the same artifacts (same type, but different pattern). Today I found out that you can flip the tangent based on whether the UV diff matrix's determinant is negative. I applied it to method (2) and it works flawlessly. I realize that the method (2) is probably a huge overkill, so I'll try with the 1st one and see if it helps. This is the improved tangent calc code:
	Vertex dv1 = {vb->x - va->x, vb->y - va->y, vb->z - va->z};
	Vertex dv2 = {vc->x - va->x, vc->y - va->y, vc->z - va->z};
	TexCoords dt1 = {xb->u - xa->u, xb->v - xa->v};
	TexCoords dt2 = {xc->u - xa->u, xc->v - xa->v};

	Vertex t;

	float area = (dt1.u * dt2.v - dt2.u - dt2.v);
	float fc = 1.0f / area;

	if(area == 0.0f)
		fc = 1.0f;

	if(area < 0.0f)
		fc = -fc;

	t.x = fc * (dt2.v * dv1.x - dt1.v * dv2.x);
	t.y = fc * (dt2.v * dv1.y - dt1.v * dv2.y);
	t.z = fc * (dt2.v * dv1.z - dt1.v * dv2.z);
where va, vb, vc are the vertices of triangle ABC and xa, xb, xc are the corresponding UV coordinates. How it looks now: What's more there are no visible lighting incosistencies on the seam! [Edited by - dsh on February 19, 2010 7:54:47 AM]

Share this post


Link to post
Share on other sites
Actually, I now found out that the bug was here:

float area = (dt1.u * dt2.v - dt2.u - dt2.v);

but should be

float area = (dt1.u * dt2.v - dt2.u * dt1.v);

If you set area = 0.0 (or fc=1) then the lighting also works because the vectors are normalized anyway so this multiplication doesn't matter

There was no flaw in the algorithm itself but just a typo.

And there are lighting incosistencies on the seam... ;(

Oh and the bump map seems reversed because I calculated the tangent as if the triangle vertices were in CCW order but they are CW in my app.

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