根据已有成果编写了控制漫游器类RoamManipulator,其中由成员变量m_vPosition和m_vRotation分别控制摄像机位置和姿态。
问题是:如何根据确定的位置和姿态角值将摄像机定位到确定的位置,即怎么实现视点定位?下面附带编写思路:
osg::Vec3 m_vPosition;
osg::Vec3 m_vRotation;
在类RoamManipulator中,重载虚函数setByMatrix()和setByInverseMatrix(),在源文件中
void RoamManipulator::setByMatrix(const osg::Matrixd&matrix)
{//设置矩阵(空)
}
void RoamManipulator::setByInverseMatrix(const osg::Matrixd&matrix)
{//设置逆矩阵(空)
}
视点定位点击相应后执行以下代码,但是点击后视点位置和姿态没有发生变化,不知为何,请群内兄弟分析一下。谢谢。
osg::ref_ptr<osgViewer::Viewer> mViewer = (osg::ref_ptr<osgViewer::Viewer>)mOSG->mViewer; RoamManipulator* camera = (RoamManipulator*)mViewer->getCameraManipulator(); pFile = fopen("camera.dat","r");//从文件中读取摄像机的位置position和rotation AfxMessageBox("文件打开失败!"); fscanf(pFile,"%lf %lf %lf %lf %lf %lf",&position.x(),&position.y(),&position.z(),&rotation.x(),&rotation.y(),&rotation.z() ); osg::Matrixd myCamMatrix; osg::Matrixd camRotation; osg::Matrixd camPosition;
camRotation.makeRotate(rotation.x(),osg::Vec3(1.0,0.0,0.0), rotation.y(),osg::Vec3(0.0,1.0,0.0), rotation.z(),osg::Vec3(0.0,0.0,1.0)); camPosition.makeTranslate(position.x(),position.y(),position.z()); myCamMatrix = camRotation * camPosition; osg::Matrixd i = myCamMatrix.inverse(myCamMatrix); camera->setByInverseMatrix(osg::Matrixd(i.ptr())*osg::Matrix::rotate(-osg::PI_2,1,0,0)); |