bonaz 发表于 2014-4-18 10:17:54

关于osgearth自带例子中Sample osgearth_qt_windows的问题

本帖最后由 bonaz 于 2014-4-18 10:33 编辑

#include <osg/Notify>
#include <osgViewer/CompositeViewer>
#include <osgEarthUtil/EarthManipulator>
#include <osgEarthQt/ViewWidget>
#include <QtGui/QApplication>
#include <QtGui/QDialog>
#include <QtGui/QMainWindow>
#include <QtGui/QPushButton>
#include <QtGui/QLayout>

#ifdef Q_WS_X11
#include <X11/Xlib.h>
#endif

using namespace osgEarth;
using namespace osgEarth::Util;
using namespace osgEarth::QtGui;

//------------------------------------------------------------------

int
usage( const std::string& msg, osg::ArgumentParser& args )
{
    OE_NOTICE << msg << std::endl << std::endl;
    OE_NOTICE << "USAGE: " << args << " file.earth" << std::endl;
      
    return -1;
}

//------------------------------------------------------------------

struct ViewController
{
    virtual void addView() =0;
};


struct MyMainWindow : public QMainWindow, public ViewController
{
    QTimer                     _timer;
    osgViewer::CompositeViewer _viewer;
    osg::ref_ptr<osg::Node>    _scene;

    MyMainWindow(osg::ArgumentParser& args, osg::Node* scene)
      : _viewer(args), _scene(scene)
    {
      // we have to add a starting view, otherwise the CV will automatically
      // mark itself as done :/
      addView();

      // timer fires a paint event.
      connect(&_timer, SIGNAL(timeout()), this, SLOT(update()));
      _timer.start(20);
    }

    void paintEvent(QPaintEvent* e)
    {
      // refresh all the views.
      if (_viewer.getRunFrameScheme() == osgViewer::ViewerBase::CONTINUOUS ||
            _viewer.checkNeedToDoFrame() )
      {
            _viewer.frame();
      }
    }

    void addView()
    {
      // the new View we want to add:
      osgViewer::View* view = new osgViewer::View();

      // a widget to hold our view:
      QWidget* viewWidget = new osgEarth::QtGui::ViewWidget(view);

      // a dialog to hold the view widget:
      QDialog* win = new QDialog(this);
      win->setModal( false );
      win->setLayout( new QHBoxLayout() );
      win->layout()->addWidget( viewWidget );
      int x = osgEarth::Random().next( 1024 );
      int y = osgEarth::Random().next( 768 );
      win->setGeometry( x, y, 640, 480 );
      win->show();

      // set up the view
      view->setCameraManipulator( new osgEarth::Util::EarthManipulator );
      view->setSceneData( _scene.get() );
      view->getDatabasePager()->setUnrefImageDataAfterApplyPolicy(true,false);

      // add it to the composite viewer.
      _viewer.addView( view );
    }
};

//------------------------------------------------------------------

struct AddButton : public QPushButton
{
    ViewController* _vc;

    AddButton(ViewController* vc, const char* text)
      : QPushButton(text), _vc(vc) { }

    void mousePressEvent( QMouseEvent* e )
    {
      _vc->addView();
    }
};

//------------------------------------------------------------------

int
main(int argc, char** argv)
{
    osg::ArgumentParser args(&argc,argv);
    if ( args.find("--help") >= 0)
      return usage("Help", args);

    // load something
    osg::Node* node = osgDB::readNodeFiles( args );
    if ( !node )
      return usage("Can't load a scene!", args);


#ifdef Q_WS_X11
    // required for multi-threaded viewer on linux:
    XInitThreads();
#endif

    // Qt setup:
    QApplication q(argc, argv);

    // fire up our controller:
    MyMainWindow win( args, node );

    // A button for adding new views:
    QPushButton* addButton = new AddButton( &win, "Add a view" );

    // Main window for the button.
    //QMainWindow win;
    win.setCentralWidget( addButton );
    win.setGeometry( 100, 100, 200, 100 );
    win.show();

    q.exec();
}


这个例子是如何把场景加进来的?系统自带的例子运行效果如图1,可是我自己根据需要改编后运行效果如图2。(.earth配置文件都是一样的,如图3)。求指点迷津

bonaz 发表于 2014-4-18 10:19:16

:dizzy:

bonaz 发表于 2014-4-18 10:36:12

我是这样写的:
//多窗口管理

                osgViewer::CompositeViewer _viewer;
      QTimer_timer;

                void MultiWindow()
                {
                       
                       // the new View we want to add:
               
      osgViewer::View* view = new osgViewer::View();
                osg::ref_ptr<osg::Node> _earthNode;
               
      // a widget to hold our view:
      QWidget* viewWidget = new osgEarth::QtGui::ViewWidget(view);

      // a dialog to hold the view widget:
      QDialog* win = new QDialog(this);
      win->setModal( false );
      win->setLayout( new QHBoxLayout() );
      win->layout()->addWidget( viewWidget );
      int x = osgEarth::Random().next( 1024 );
      int y = osgEarth::Random().next( 768 );
      win->setGeometry( x, y, 640, 480 );
      win->show();

      // set up the view
      view->setCameraManipulator( new osgEarth::Util::EarthManipulator );
               
                view->setSceneData( _earthNode.get() );
      view->getDatabasePager()->setUnrefImageDataAfterApplyPolicy(true,false);

      // add it to the composite viewer.
                // osgViewer::CompositeViewer _viewer;
          _viewer.addView( view );
               
                  connect(&_timer, SIGNAL(timeout()), win, SLOT(update()));
         _timer.start(20);
                }

               void paintEvent(QPaintEvent* e)
                {
                        // refresh all the views.
                        if (_viewer.getRunFrameScheme() == osgViewer::ViewerBase::CONTINUOUS ||
                                _viewer.checkNeedToDoFrame() )
                        {
                                _viewer.frame();
                        }
                }

bonaz 发表于 2014-4-20 21:29:12

有木有谁给点提示啊,困惑好久了:'(

ysw 发表于 2014-4-22 10:07:50

void MultiWindow() 和 void paintEvent(QPaintEvent* e),只是两个函数吗,还是与上面的类一样,进行了继承?osg::ref_ptr<osg::Node> _earthNode;这个场景数据节点你怎么赋值的啊,没见你读取数据的函数调用。basin3d?这里面是否有场景数据。

bonaz 发表于 2014-4-24 22:04:59

ysw 发表于 2014-4-22 10:07
void MultiWindow() 和 void paintEvent(QPaintEvent* e),只是两个函数吗,还是与上面的类一样,进行了继 ...

现在我就是显示不出场景,我想问题应该就是场景数据没有读进来,你说的场景数据赋值是什么意思呢?_earthNode = osgDB::readNodeFile(earthFile);在同一个项目中其它cpp文件里有,我只是把这个场景数据调过来使用。可是就是显示不出场景:'(

bonaz 发表于 2014-4-24 22:07:31

另一个问题就是,我的项目中有那么多的节点,我怎么去知道哪个节点是做什么的?有什么方法么。这个程序是别人写的,我们看,可是看不懂:'(

bonaz 发表于 2014-4-24 22:07:40

public:
                osg::ref_ptr<osg::Group> _root;
                osg::ref_ptr<osg::Node> _earthNode;
                osg::ref_ptr<osgEarth::MapNode> _mapNode;
                osg::ref_ptr<osgEarth::Util::EarthManipulator> _earthManip;

                osg::ref_ptr<osgEarth::Util::Controls::ControlCanvas> _canvas;
                osg::ref_ptr<osgEarth::Util::Controls::HBox> Shipinfomation;

                osg::ref_ptr<osg::Group> _annoGroup;
                osg::ref_ptr<osgEarth::Util::SkyNode> _sky;

                osg::ref_ptr<osgViewer::ViewerBase> _viewerBase;
                osgViewer::Viewer *_viewer;

ysw 发表于 2014-4-25 08:50:07

bonaz 发表于 2014-4-24 22:07
public:
                osg::ref_ptr _root;
                osg::ref_ptr _earthNode;


OpenSceneGraph 三维渲染引擎编程指南 肖鹏 清华大学出版社 这本书写的很详细

bonaz 发表于 2014-4-25 16:55:55

ysw 发表于 2014-4-25 08:50
OpenSceneGraph 三维渲染引擎编程指南 肖鹏 清华大学出版社 这本书写的很详细

是“第四章:场景的组织结构”吗?

ysw 发表于 2014-4-25 17:59:29

bonaz 发表于 2014-4-25 16:55
是“第四章:场景的组织结构”吗?

上面有些简单的osg的节点的介绍,最好的学习的话,可以看看osg和osgearth的自带的例子,里面各个种类的节点运行一下的话,就了解差不多了
页: [1]
查看完整版本: 关于osgearth自带例子中Sample osgearth_qt_windows的问题