liyihongcug 发表于 2020-3-14 14:40:46

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

提高加载速度几种方法:
1,精简模型;
2,精简纹理图片,图像尺寸改为2的N次方;
3,异步加载;

liyihongcug 发表于 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::Picker 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::QtGui::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::objectIndex()->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();
}
页: [1]
查看完整版本: osg加载模型提高速度参考办法