|
本帖最后由 redrose007 于 2012-6-17 11:01 编辑
目的:读入物体,得到其在世界坐标系的坐标,并可自己设置视点位置
遇到的问题:
osg::ref_ptr<osg::Node> model= osgDB::readNodeFile("cow.osg");
viewer->setCameraManipulator(new osgGA::TrackballManipulator);
osg::Vec3 eye;
osg::Vec3 center;
osg::Vec3 up;
viewer->getCamera()->getViewMatrixAsLookAt(eye, center, up);
这时候得到的eye(0, 0, 0) center(0, 0, -1) up(0, 1, 0)
class GetWorldCoordinateOfNodeVisitor : public osg::NodeVisitor
{
public:
GetWorldCoordinateOfNodeVisitor():
osg::NodeVisitor(NodeVisitor::TRAVERSE_PARENTS),done(false)
{
wcMatrix = new osg::Matrixd();
}
virtual void apply(osg::Node &node)
{
if (!done)
{
if (0 == node.getNumParents())
{
wcMatrix->set(osg::computeLocalToWorld(this->getNodePath()));
done = true;
}
traverse(node);
}
}
osg::Matrixd* giveUpDaMat()
{
return wcMatrix;
}
private:
bool done;
osg::Matrix* wcMatrix;
};
osg::Matrixd* getWorldCoords(osg::Node* node)
{
GetWorldCoordinateOfNodeVisitor* ncv = new GetWorldCoordinateOfNodeVisitor();
if (node && ncv)
{
node->accept(*ncv);
return ncv->giveUpDaMat();
}
else
{
return NULL;
}
}
osg::Matrixd* mat1;
mat1 = getWorldCoords(model);
这里得到的wcMatrix是单位矩阵,array解释说这个矩阵是从局部坐标系到世界坐标系的变换矩阵,那么,我想得到此时model在世界坐标系的位置如何得到呢?
osg::ref_ptr<osg::Node> model= osgDB::readNodeFile("cow.osg");
osg::Matrix cameraPos;
cameraPos.makeLookAt(osg::Vec3(0, -100, 0), osg::Vec3(0, 0, 0), osg::Vec3(0, 0, 1));
viewer->getCamera()->setViewMatrix(cameraPos);
osg::Vec3 eye;
osg::Vec3 center;
osg::Vec3 up;
viewer->getCamera()->getViewMatrixAsLookAt(eye, center, up);
这时候得到的eye(0, -100, 0) center(0, -99, 0) up(0, 0, 1) ,这时候center位置是怎么回事?
osg::Matrixd* mat1;
mat1 = getWorldCoords(viewer->getCamera());
这时候得到的wcMatrix还是单位矩阵,我想知道这时候相机位置到底在哪?
请大家明白的帮忙解答一下,谢谢大家! |
|