查看: 2188|回复: 1

osg加载模型提高速度参考办法

[复制链接]
  • TA的每日心情
    开心
    2020-3-20 17:50
  • 签到天数: 1 天

    [LV.1]初来乍到

    发表于 2020-3-14 14:40:46 | 显示全部楼层 |阅读模式
    提高加载速度几种方法:
    1,精简模型;
    2,精简纹理图片,图像尺寸改为2的N次方;
    3,异步加载;
  • TA的每日心情
    开心
    2020-3-20 17:50
  • 签到天数: 1 天

    [LV.1]初来乍到

     楼主| 发表于 2020-3-14 14:52:41 | 显示全部楼层
    本帖最后由 liyihongcug 于 2020-3-14 16:14 编辑

    上自动平面裁剪避免遮挡:
    添加头文件
    #include <osgEarthUtil/AutoClipPlaneHandler>
    在main函数中添加
    //加上自动裁剪。否则显示会出现遮挡       
    viewer->getCamera()->addCullCallback(new osgEarth::Util::AutoClipPlaneCullCallback(mapNode));


    osgearth关于近地面Camera远近裁剪面的设置
    视点接近地面时,由于默认的近裁剪面near太大,导致看不见靠近摄像机的物体,这时需要重新调节near和far的值(或者near/far的比率),可以使用osgEarth::Util::AutoClipPlaneCullCallback(osgEarth::MapNode* mapNode)
    例如:
    osgViewer::Viewer viewer;
    viewer.getCamera()->addCullCallback( new osgEarth::Util::AutoClipPlaneCullCallback(mapNode) );


    osgEarth:icker pick(view, view->getSceneData());
    osgEarth::Picker::Hits hits;
    if (pick.pick(x,y,hits))
    {
    for(osgEarth::Picker::Hits::iterator hit = hits.begin(); hit != hits.end(); ++hit )
    {
    //这里转成你需要的类型
    osgEarth::Annotation::FeatureNode* node = pick.getNode<osgEarth::Annotation::FeatureNode>(*hit);
    if (node)
    { 业务逻辑 }
    }

    Using osgEarth 2.7, I'm trying to apply RTTPicker to a PlaceNode, which has an icon and a text label. For the icon, this works fine, and I get the correct ObjectID. But the text for some reason returns its own color as the ID. See the following code. Here 0x600dc0de is the ID I would expect from RTTPicker for the whole PlaceNode. But instead for text, whose color I set to 0xbadc0de1, the ID appears equal to this 0xbadc0de1.

    What am I doing wrong?

    #include <QDir>
    #include <iostream>
    #include <QMainWindow>
    #include <QApplication>
    #include <QTemporaryFile>
    #include <osgEarth/Registry>
    #include <osgEarth/ObjectIndex>
    #include <osgEarthUtil/RTTPicker>
    #include <osgEarthQt/ViewerWidget>
    #include <osgEarthAnnotation/PlaceNode>
    #include <osgEarthUtil/EarthManipulator>


    class DemoMainWindow : public QMainWindow
    {
    public:
        DemoMainWindow(osgEarth::MapNode* mapNode, osgViewer::Viewer* viewer)
        {
            using namespace osgEarth::Annotation;
            setCentralWidget(new osgEarth:tGui::ViewerWidget(viewer));

            const auto pNode=new PlaceNode(mapNode, osgEarth::GeoPoint(mapNode->getMapSRS(), 37, 55, 0),
                                           "Icon on map");
            mapNode->addChild(pNode);

            auto style=pNode->getStyle();
            style.getOrCreateSymbol<TextSymbol>()->fill()->color()=osg::Vec4(0xba,0xdc,0x0d,0xe1)/255.;
            pNode->setStyle(style);

            pNode->setIconImage(osgDB::readImageFile("/usr/share/icons/hicolor/32x32/apps/firefox.png"));

            pNode->getOrCreateStateSet()->setMode(GL_DEPTH_TEST, osg::StateAttribute::OFF);

            const osgEarth::ObjectID id=0x600dc0de;
            osgEarth::Registry:bjectIndex()->tagNode(pNode,id);
        }
    };

    struct MyPickCallback : public osgEarth::Util::RTTPicker::Callback
    {
        void onHit(osgEarth::ObjectID id) override
        {
            std::cerr << "onHit, id: " << std::hex << id << std::dec << "\n";
        }
        bool accept(const osgGA::GUIEventAdapter& ea, const osgGA::GUIActionAdapter&) override
        {
            return ea.getEventType() == ea.MOVE;
        }
    };

    int main(int argc, char** argv)
    {
        using namespace osgEarth::Util;

        QApplication app(argc, argv);
        osg::setNotifyLevel(osg::WARN);
        osgEarth::setNotifyLevel(osg::WARN);
        QTemporaryFile earthFile(QDir::tempPath()+QDir::separator()+"XXXXXX.earth");
        earthFile.open();
        earthFile.write("<map><options><terrain><color>#555555ff</color></terrain></options></map>");
        earthFile.close();
        const auto earthNode = osgDB::readNodeFile(earthFile.fileName().toStdString());
        if(!earthNode)
        {
            OE_WARN << "Unable to load earth model \"" << earthFile.fileName().toStdString() << "\"\n";
            return EXIT_FAILURE;
        }

        const auto viewer = new osgViewer::Viewer;
        viewer->setThreadingModel(osgViewer::ViewerBase::SingleThreaded);
        viewer->setSceneData(earthNode);

        const auto windowSize=300;
        osgEarth::Util::RTTPicker* picker = new osgEarth::Util::RTTPicker(windowSize);
        viewer->addEventHandler(picker);
        picker->setDefaultCallback(new MyPickCallback());

        const auto mapNode = osgEarth::MapNode::findMapNode(earthNode);
        picker->addChild(mapNode);

        DemoMainWindow appWin(mapNode, viewer);
        appWin.resize(windowSize,windowSize);

        const auto manip = new osgEarth::Util::EarthManipulator;
        viewer->setCameraManipulator(manip, 2.);
        manip->setViewpoint(osgEarth::Viewpoint("CameraCenter", 37, 55, 0, 0.0, -90.0, 300000));

        appWin.show();
        return app.exec();
    }
    您需要登录后才可以回帖 登录 | 注册

    本版积分规则

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

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

    联系我们

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