图形学基础 | 如何计算切线空间的切线和副切线

http://www.cnblogs.com/ThreeThousandBigWorld/archive/2012/07/16/2593892.html
http://blog.chinaunix.net/uid-26651460-id-3083223.html
http://stackoverflow.com/questions/5255806/how-to-calculate-tangent-and-binormal

具体代码:

void calculate_tangent_and_binormal(vector_t *tangent, vector_t *binormal, const vector_t *position1, const vector_t *position2, const vector_t *position3,
                                          float u1, float v1, float u2, float v2, float u3, float v3)
{
    //side0 is the vector along one side of the triangle of vertices passed in,
    //and side1 is the vector along another side. Taking the cross product of these returns the normal.
    vector_t side0 = {0.0f, 0.0f, 0.0f, 1.0f};
    vector_sub(&side0, position1, position2);
    vector_t side1 = {0.0f, 0.0f, 0.0f, 1.0f};
    vector_sub(&side1, position3, position1);
    //Calculate face normal
    vector_t normal = {0.0f, 0.0f, 0.0f, 0.0f};
    vector_crossproduct(&normal, &side1, &side0);
    vector_normalize(&normal);
    //Now we use a formula to calculate the tangent.
    float deltaV0 = v1 - v2;
    float deltaV1 = v3 - v1;
    *tangent = side0;
    vector_scale(tangent, deltaV1);
    vector_t temp = side1;
    vector_scale(&temp, deltaV0);
    vector_sub(tangent, tangent, &temp);
    vector_normalize(tangent);
    //Calculate binormal
    float deltaU0 = u1 - u2;
    float deltaU1 = u3 - u1;
    *binormal = side0;
    vector_scale(binormal, deltaU1);
    temp = side1;
    vector_scale(&temp, deltaU0);
    vector_sub(binormal, binormal, &temp);
    vector_normalize(binormal);
    //Now, we take the cross product of the tangents to get a vector which
    //should point in the same direction as our normal calculated above.
    //If it points in the opposite direction (the dot product between the normals is less than zero),
    //then we need to reverse the s and t tangents.
    //This is because the triangle has been mirrored when going from tangent space to object space.
    //reverse tangents if necessary
    vector_t tangentCross;
    vector_crossproduct(&tangentCross, tangent, binormal);
    if (vector_dotproduct(&tangentCross, &normal) < 0.0f)
    {
        tangent->w = -1;
        //vector_inverse(tangent);
        //vector_inverse(binormal);
    }
    
    //binormal->w = 0.0f;
}

你可能感兴趣的:([图形学基础])