(转)Ogre中计算Tangent, 感觉很不错!
2010-10-09 11:15
190 查看
代码
Vector3 Math::calculateTangentSpaceVector(
const Vector3& position1, const Vector3& position2, const Vector3& position3,
Real u1, Real v1, Real u2, Real v2, Real u3, Real 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.
Vector3 side0 = position1 - position2;
Vector3 side1 = position3 - position1;
//Calculate face normal
Vector3 normal = side1.crossProduct(side0);
normal.normalise();
//Now we use a formula to calculate the tangent.
Real deltaV0 = v1 - v2;
Real deltaV1 = v3 - v1;
Vector3 tangent = deltaV1 * side0 - deltaV0 * side1;
tangent.normalise();
//Calculate binormal
Real deltaU0 = u1 - u2;
Real deltaU1 = u3 - u1;
Vector3 binormal = deltaU1 * side0 - deltaU0 * side1;
binormal.normalise();
//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
Vector3 tangentCross = tangent.crossProduct(binormal);
if (tangentCross.dotProduct(normal) < 0.0f)
{
tangent = -tangent;
binormal = -binormal;
}
return tangent;
}
Vector3 Math::calculateTangentSpaceVector(
const Vector3& position1, const Vector3& position2, const Vector3& position3,
Real u1, Real v1, Real u2, Real v2, Real u3, Real 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.
Vector3 side0 = position1 - position2;
Vector3 side1 = position3 - position1;
//Calculate face normal
Vector3 normal = side1.crossProduct(side0);
normal.normalise();
//Now we use a formula to calculate the tangent.
Real deltaV0 = v1 - v2;
Real deltaV1 = v3 - v1;
Vector3 tangent = deltaV1 * side0 - deltaV0 * side1;
tangent.normalise();
//Calculate binormal
Real deltaU0 = u1 - u2;
Real deltaU1 = u3 - u1;
Vector3 binormal = deltaU1 * side0 - deltaU0 * side1;
binormal.normalise();
//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
Vector3 tangentCross = tangent.crossProduct(binormal);
if (tangentCross.dotProduct(normal) < 0.0f)
{
tangent = -tangent;
binormal = -binormal;
}
return tangent;
}
相关文章推荐
- 一篇不错的讲解Java异常的文章(转载)----感觉很不错,读了以后很有启发
- 刚从C++转过来,感觉C#很不错
- 最通俗的说法告诉你:矩阵是什么(从校内看到,认真读了,感觉很不错)
- 一篇不错的讲解Java异常的文章(转载)----感觉很不错,读了以后很有启发
- 今天用了 NOPI 感觉还真的很不错呵呵!
- 今天学习了一下,CS代码按我们的要求加载前台代码需要的JS,感觉很不错【值可以按我们的需要的加载】
- 一篇不错的讲解Java异常的文章(转载)----感觉很不错,读了以后很有启发
- 我的操盘机器人新模型上线第一天(计算出来10个股票),感觉开挂了
- 刚想到一个计算地图计算距离的算法,自我感觉是最牛的算法,没有之一
- 一篇不错的讲解Java异常的文章(转载)----感觉很不错,读了以后很有启发
- NetBeans 6.5 感觉很不错
- iOS 5 取景器和实时滤镜的制作(AVCaptureSession 讲解的很不错感觉)
- 一篇不错的讲解Java异常的文章(转载)----感觉很不错,读了以后很有启发
- 我的操盘机器人新模型上线第一天(计算出来10个股票),感觉开挂了
- 刚想到一个计算地图计算距离的算法,自我感觉是最牛的算法,没有之一
- 一篇不错的讲解Java异常的文章(转载)----感觉很不错,读了以后很有启发
- 今天看到一句话,感觉很不错
- Ogre代码学习之1——Ogre中地形lod的基础:deltaHeight的计算
- Tangent Space的基向量计算方法
- 一篇不错的讲解Java异常的文章----感觉很不错,读了以后很有启发