|
楼主 |
发表于 2009-6-6 10:14:46
|
显示全部楼层
array 你好 我参考了您的osgModeling::rotateMatrix函数的内容 这个函数就是根据旋转轴参数axis,角度参数angle构造了一个矩阵吧 我把您的osgModeling::rotateMatrix函数的内容 在osg中封装了下 但是结果还是不对 我调试了旋转的矩阵 里面是有内容的 而且不是X/Y/Z轴 下面是封装和调用的代码 请您之指点
封装代码:
osg::Matrix CContralView::rotateMatrix(osg::Vec3 axis, const double angle)
{
double cosA = cos(angle);
double sinA = sin(angle);
axis.normalize();
if ( axis==osg::Vec3(1.0f,0.0f,0.0f) ) // X
{
return osg::Matrix(
1.0f, 0.0f, 0.0f, 0.0f,
0.0f, cosA, -sinA, 0.0f,
0.0f, sinA, cosA, 0.0f,
0.0f, 0.0f, 0.0f, 1.0f
);
}
else if ( axis==osg::Vec3(0.0f,1.0f,0.0f) ) // Y
{
return osg::Matrix(
cosA, 0.0f, sinA, 0.0f,
0.0f, 1.0f, 0.0f, 0.0f,
-sinA, 0.0f, cosA, 0.0f,
0.0f, 0.0f, 0.0f, 1.0f
);
}
else if ( axis==osg::Vec3(0.0f,0.0f,1.0f) ) // Z
{
return osg::Matrix(
cosA, -sinA, 0.0f, 0.0f,
sinA, cosA, 0.0f, 0.0f,
0.0f, 0.0f, 1.0f, 0.0f,
0.0f, 0.0f, 0.0f, 1.0f
);
}
double a1 = axis.x();
double a2 = axis.y();
double a3 = axis.z();
// Rotate matrix of any vector, use the Rodriguez formula [Hecker 1997]
// v' = v * cosA + axis*(v*axis) * (1-cosA) + (axis^vec) * sinA
// So, T = I * cosA + (axis@axis) * (1-cosA) + M~ * sinA
// @ means tensor product and M~ is a temp matrix of calculating cross product.
return osg::Matrix(
cosA+a1*a1*(1-cosA), a2*a1*(1-cosA)-a3*sinA, a3*a1*(1-cosA)+a2*sinA, 0.0f,
a1*a2*(1-cosA)+a3*sinA, cosA+a2*a2*(1-cosA), a3*a2*(1-cosA)-a1*sinA, 0.0f,
a1*a3*(1-cosA)-a2*sinA, a2*a3*(1-cosA)+a1*sinA, cosA+a3*a3*(1-cosA), 0.0f,
0.0f, 0.0f, 0.0f, 1.0f
);
}
调用代码:
osg::Camera* camera = viewer->getCamera();
camera->getViewMatrixAsLookAt(l_veye,l_vcenter,l_vup);
viewer->setCamera(camera);
osg::Vec3f axis = l_vcenter ^ g_vcenter;
axis.normalize();
float t = (l_vcenter - g_vcenter).length() / (2.0 *0.8);
if (t > 1.0) t = 1.0;
if (t < -1.0) t = -1.0;
float angle = asin(t);
osg::Matrix mm;
mm = rotateMatrix(axis,angle);
//mm.makeRotate(-angle,axis);
mt->setMatrix(mm); |
|