为什么这个3D矩阵旋转失败?

时间:2013-04-04 09:16:54

标签: c++ matrix rotation point

应用于对象的3D点的旋转会产生“波浪效果”而不是旋转。 这个代码在某处错了吗?

// ----------- Init Rot Matrix(浮动rotX = 45,rotY = 45,rotZ = 45)-------------

boost::numeric::ublas::vector<float> Axisboost (4);
Axisboost(0) = 0;
Axisboost(1) = 0;
Axisboost(2) = 0;
Axisboost(3) = 1;

//RotX Matrix
Axisboost (1) = 1;
boost::numeric::ublas::matrix<double> mRotAXIS_X = GeoMath::GetSingletonPtr()->FromAxisAngle4(Axisboost, rotX*M_PI/180.);

//RotY Matrix
Axisboost (1) = 0;//(cancel previous axis)
Axisboost (2) = 1;
boost::numeric::ublas::matrix<double> mRotAXIS_Y = GeoMath::GetSingletonPtr()->FromAxisAngle4(Axisboost, rotY*M_PI/180.);

//RotZ Matrix
Axisboost (2) = 0;//(cancel previous axis)
Axisboost (3) = 1;
boost::numeric::ublas::matrix<double> mRotAXIS_Z = GeoMath::GetSingletonPtr()->FromAxisAngle4(Axisboost, rotZ*M_PI/180.);

[...]

//--------- Try to apply rotation matrix on a Cube --------------
boost::numeric::ublas::matrix<double> coordsOrig(4, 4);
coordsOrig(3,3)=1;

float* ptr_pPosX, *ptr_pPosY, *ptr_pPosZ;
float* pPos = static_cast<float*>(vbuf->lock(Ogre::HardwareBuffer::HBL_DISCARD));//ptr to points of cube

for(int i=0; i<72; i+=3){ //72 = 3coords * 2pts * 12lines
     //Get coordinates of 1 point
     ptr_pPosX=pPos;
     coordsOrig(0,0)=(double)*pPos++;
     ptr_pPosY=pPos;
     coordsOrig(1,1)=(double)*pPos++;
     ptr_pPosZ=pPos;
     coordsOrig(2,2)=(double)*pPos++;

     //Try to rotate 1 point around X, Y and Z axis       
     boost::numeric::ublas::matrix<double> afterRotX = boost::numeric::ublas::prod(coordsOrig, mRotAXIS_X);

     boost::numeric::ublas::matrix<double> afterRotXY = boost::numeric::ublas::prod(afterRotX, mRotAXIS_Y);

     boost::numeric::ublas::matrix<double> afterRotXYZ = boost::numeric::ublas::prod(afterRotXY, mRotAXIS_Z);

     //Set rotation result of 1 point
     *ptr_pPosX = (float)afterRotXYZ(0,0);
     *ptr_pPosY = (float)afterRotXYZ(1,1);
     *ptr_pPosZ = (float)afterRotXYZ(2,2);  
}

0 个答案:

没有答案