查看: 573|回复: 0

osg三维场景修改相机视点失败,代码如下

[复制链接]

该用户从未签到

发表于 2023-12-13 20:43:42 | 显示全部楼层 |阅读模式
下面这段代码无法修改相机的视点:
        osgViewer::Viewer* OSGViewer = p3DRenderEngine->getViewer();
        osg::Node* sceneData = OSGViewer->getSceneData();
        osg::Group* root = dynamic_cast<osg::Group*>(sceneData);
        osg::Node* scene = root->getNumChildren() > 0 ? root->getChild(0) : nullptr;
        osg::ComputeBoundsVisitor boundVisitor;
        scene->accept(boundVisitor);
        osg::BoundingBox bb = boundVisitor.getBoundingBox();
        osg::Vec3d center = bb.center();
        double distance = bb.radius() * 2;
        osg::Vec3d target = osg::Vec3(center.x(), center.y(), 0);
        osg::Vec3d eye = target + osg::Vec3(0, 0, distance);
        osg::Vec3d up = osg::Vec3(0, 0, 1);
        OSGViewer->getCamera()->setViewMatrixAsLookAt(eye, target, up);
        OSGViewer->getCamera()->setProjectionMatrixAsOrtho(-distance, distance, -distance, distance, 1, 100);
下面这段代码可以:
        osg::Node* sceneData = OSGViewer->getSceneData();
        osg::Group* root = dynamic_cast<osg::Group*>(sceneData);
        osg::Node* scene = root->getNumChildren() > 0 ? root->getChild(0) : nullptr;
        osg::ComputeBoundsVisitor boundVisitor;
        scene->accept(boundVisitor);
        osg::BoundingBox bb = boundVisitor.getBoundingBox();
        osg::Vec3d center = bb.center();
        double distance = bb.radius() * 2;
        osg::Matrixd matrix;
        matrix.makeRotate(osg:uat(osg:I_2, osg::Vec3d(0, 0, 1)));
        matrix.preMultTranslate(osg::Vec3d(0, 0, distance));
        matrix.preMultScale(osg::Vec3d(1, 1, 1));
        OSGViewer->getCameraManipulator()->setByMatrix(matrix);
        osg::Matrixd projMatrix;
        projMatrix = osg::Matrixd:rtho2D(-distance, distance, -distance, distance);
        OSGViewer->getCamera()->setProjectionMatrix(projMatrix);
上面两端代码主要是为了实现俯视图的效果,但是仅仅通过相机来设置结果是不正确的,只有投影的效果,不管怎么该eye,target都没有作用!请教一下哪位大佬可以解答一下?
您需要登录后才可以回帖 登录 | 注册

本版积分规则

OSG中国官方论坛-有您OSG在中国才更好

网站简介:osgChina是国内首个三维相关技术开源社区,旨在为国内更多的技术开发人员提供最前沿的技术资讯,为更多的三维从业者提供一个学习、交流的技术平台。

联系我们

  • 工作时间:09:00--18:00
  • 反馈邮箱:1315785073@qq.com
快速回复 返回顶部 返回列表