# 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 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.

## Create an account

Register a new account

• ## Partner Spotlight

• ### Forum Statistics

• Total Topics
627662
• Total Posts
2978515
• ### Similar Content

• Both functions are available since 3.0, and I'm currently using glMapBuffer(), which works fine.
But, I was wondering if anyone has experienced advantage in using glMapBufferRange(), which allows to specify the range of the mapped buffer. Could this be only a safety measure or does it improve performance?
Note: I'm not asking about glBufferSubData()/glBufferData. Those two are irrelevant in this case.
• By xhcao
Before using void glBindImageTexture(    GLuint unit, GLuint texture, GLint level, GLboolean layered, GLint layer, GLenum access, GLenum format), does need to make sure that texture is completeness.
• By cebugdev
hi guys,
are there any books, link online or any other resources that discusses on how to build special effects such as magic, lightning, etc. in OpenGL? i mean, yeah most of them are using particles but im looking for resources specifically on how to manipulate the particles to look like an effect that can be use for games,. i did fire particle before, and I want to learn how to do the other 'magic' as well.
Like are there one book or link(cant find in google) that atleast featured how to make different particle effects in OpenGL (or DirectX)? If there is no one stop shop for it, maybe ill just look for some tips on how to make a particle engine that is flexible enough to enable me to design different effects/magic
let me know if you guys have recommendations.
• By dud3
How do we rotate the camera around x axis 360 degrees, without having the strange effect as in my video below?
Mine behaves exactly the same way spherical coordinates would, I'm using euler angles.
Tried googling, but couldn't find a proper answer, guessing I don't know what exactly to google for, googled 'rotate 360 around x axis', got no proper answers.

References:
Code: https://pastebin.com/Hcshj3FQ
The video shows the difference between blender and my rotation:

• By Defend
I've had a Google around for this but haven't yet found some solid advice. There is a lot of "it depends", but I'm not sure on what.
My question is what's a good rule of thumb to follow when it comes to creating/using VBOs & VAOs? As in, when should I use multiple or when should I not? My understanding so far is that if I need a new VBO, then I need a new VAO. So when it comes to rendering multiple objects I can either:
* make lots of VAO/VBO pairs and flip through them to render different objects, or
* make one big VBO and jump around its memory to render different objects.
I also understand that if I need to render objects with different vertex attributes, then a new VAO is necessary in this case.
If that "it depends" really is quite variable, what's best for a beginner with OpenGL, assuming that better approaches can be learnt later with better understanding?

• 10
• 10
• 12
• 22
• 13