Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to achieve smooth tangent space normals?

I'm trying to add bump mapping functionality to my application but I'm getting very faceted models:

enter image description here

The reason it is happening is because I'm calculating tangent, binormal and normal on per face basis and completely ignoring the normals I'm getting from the model file.

The calculation currently uses two edges of the triangle and texture space vectors to get tangent and binormal, which are then used to calculate normal by cross product. It is all done on the CPU as soon as the model loads and the values are then stored as a part of model's geometry.

    vector1 = vertex2.coords - vertex1.coords;      
    vector2 = vertex3.coords - vertex1.coords;

    tuVector = vertex2.texcoords - vertex1.texcoords;
    tvVector = vertex3.texcoords - vertex1.texcoords;

    float den = 1.0f / (tuVector.x * tvVector.y - tuVector.y * tvVector.x);

    tangent.x = (tvVector.y * vector1.x - tvVector.x * vector2.x) * den;
    tangent.y = (tvVector.y * vector1.y - tvVector.x * vector2.y) * den;
    tangent.z = (tvVector.y * vector1.z - tvVector.x * vector2.z) * den;

    binormal.x = (tuVector.x * vector2.x - tuVector.y * vector1.x) * den;
    binormal.y = (tuVector.x * vector2.y - tuVector.y * vector1.y) * den;
    binormal.z = (tuVector.x * vector2.z - tuVector.y * vector1.z) * den;

    D3DXVec3Normalize(&tangent, &tangent);
    D3DXVec3Normalize(&binormal, &binormal);

    D3DXVec3Cross(&normal, &tangent, &binormal);    
    D3DXVec3Normalize(&normal, &normal);

Is there a way to either calculate these values on per vertex basis, perhaps using the normal supplied with the model or to smooth them out somehow so the model doesn't appear faceted?

like image 777
jaho Avatar asked Oct 02 '22 14:10

jaho


People also ask

What is tangent space normals?

Tangent space is a space that's local to the surface of a triangle: the normals are relative to the local reference frame of the individual triangles. Think of it as the local space of the normal map's vectors; they're all defined pointing in the positive z direction regardless of the final transformed direction.

How do you find tangent and Bitangent from normal?

To calculate the bitangent, we take the cross product of the normal and tangent vectors then multiply it by a constant in tangent. w which is the handedness of the tangent space. The bitangent points along the V texture coordinate axis of the face.

What is tangent space in shaders?

This is the coordinate system in which the texture coordinates for a face are specified. The tangent space system will most likely vary for any two faces. In this coordinate system visualize the X axis pointing in the direction in which the U value increases and the Y axis in the direction in which V value increases.


1 Answers

For smooth surfaces (no edges) I do it like this:

  1. create space for per vertex

    double N[3]; //normal
    int cnt;
    
  2. per vertex init

    N={0.0,0.0,0.0}
    cnt=0;
    
  3. compute per face normal

    normal must be normalized length=1.0 !!! add this Normal to all vertexes used in face and increment cnt to all vertexes used in face

  4. per vertex normalize

    N/=cnt; // N = average normal from all vertex - neighbour faces
    

    be aware of cnt=0 for unused vertexes (division by zero)

  5. per vertex N contains the Normal you want

    now compute T,B vectors for TBN matrix (per vertex) as you do now

  6. output image is smooth

    My earth preview (with atmospheric scattering, bump mapping and more...) is here

hope it helps

like image 86
Spektre Avatar answered Oct 13 '22 11:10

Spektre