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

本文介绍了一种计算三角形面上法线、切线和副切线的算法,通过输入三个顶点的位置及UV坐标,利用向量运算如向量减法、叉乘、点乘等,来精确计算出三角形表面的法线、切线和副切线,这些向量在3D图形处理中用于光照和纹理映射。
摘要由CSDN通过智能技术生成

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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值