When I use TECHNIQUE_GPU or TECHNIQUE_DRAPE The scene disappears.

classic Classic list List threaded Threaded
3 messages Options
buaahc buaahc
Reply | Threaded
Open this post in threaded view
|

When I use TECHNIQUE_GPU or TECHNIQUE_DRAPE The scene disappears.

Hello everyone:
This is a very simple application,
but when I set "pathStyle.getOrCreate<osgEarth::Symbology::AltitudeSymbol>()->technique() = osgEarth::Symbology::AltitudeSymbol::TECHNIQUE_GPU or TECHNIQUE_Drape", the scene disappears.
I guess is there have problem with the graphic context?
Because when I use MFC or win32-exe, there's no problem.
Looking forward to your reply. Thanks~

class QtOSGWidget : public QOpenGLWidget
{
public:
  QtOSGWidget(qreal scaleX, qreal scaleY, QWidget* parent = 0)
    : QOpenGLWidget(parent)
    , _mViewer(new osgViewer::Viewer)
    , m_scaleX(scaleX)
    , m_scaleY(scaleY)
  {



        osg::DisplaySettings* ds = osg::DisplaySettings::instance().get();
        osg::ref_ptr<osg::GraphicsContext::Traits> traits = new osg::GraphicsContext::Traits(ds);
        traits->readDISPLAY();
        if (traits->displayNum < 0) traits->displayNum = 0;
        //this->resize(QSize(800, 600));
        traits->windowDecoration = false;
        traits->x = 0; //x();
        traits->y = 0; //y();
        traits->width = 100; // width();
        traits->height = 100; //height();
        traits->doubleBuffer = true;
        traits->alpha = ds->getMinimumNumAlphaBits();
        traits->stencil = ds->getMinimumNumStencilBits();
        traits->sampleBuffers = ds->getMultiSamples();
        traits->samples = ds->getNumMultiSamples();
        traits->sharedContext = nullptr;
        //traits->inheritedWindowData = new osgQt::GraphicsWindowQt::WindowData(this);


        this->_mGraphicsWindow = new osgViewer::GraphicsWindowEmbedded(traits/*this->x(), this->y(),
                this->width(), this->height()*/);

    _mGraphicsWindow->resized(0,0,800,600);
    osg::Cylinder* cylinder = new osg::Cylinder(osg::Vec3(0.f, 0.f, 0.f), 0.25f, 0.5f);
    osg::ShapeDrawable* sd = new osg::ShapeDrawable(cylinder);
    sd->setColor(osg::Vec4(0.8f, 0.5f, 0.2f, 1.f));
    osg::Geode* geode = new osg::Geode;
    geode->addDrawable(sd);

    osg::Camera* camera = _mViewer->getCamera();
    camera->setViewport(0, 0, this->width(), this->height());
    camera->setClearColor(osg::Vec4(0.9f, 0.9f, 1.f, 1.f));
    float aspectRatio = static_cast<float>(this->width()) / static_cast<float>(this->height());
    camera->setProjectionMatrixAsPerspective(30.f, aspectRatio, 1.f, 1000.f);
    camera->setGraphicsContext(_mGraphicsWindow);
        camera->setDrawBuffer(traits->doubleBuffer ? GL_BACK : GL_FRONT);
        camera->setReadBuffer(traits->doubleBuffer ? GL_BACK : GL_FRONT);

    _mViewer->setCamera(camera);
    _mViewer->setSceneData(geode);
        _mViewer->addEventHandler(new osgViewer::StatsHandler);
        osgGA::TrackballManipulator* manipulator = new osgGA::TrackballManipulator;
    manipulator->setAllowThrow(false);
    this->setMouseTracking(true);
    _mViewer->setCameraManipulator(manipulator);
    _mViewer->setThreadingModel(osgViewer::Viewer::SingleThreaded);
    _mViewer->realize();

        this->_mViewer->getCamera()->setNearFarRatio(0.00001);
        this->_mViewer->getCamera()->setSmallFeatureCullingPixelSize(-1.0f);



        osg::ref_ptr<osg::Group>root = new osg::Group;
        osg::ref_ptr<osgEarth::MapNode>m_pMapNode = dynamic_cast<osgEarth::MapNode*>(osgDB::readNodeFile("D:\\LILY\\lib\\opensource\\osgearth-osgearth-2.10.1\\tests/arcgisonline.earth"));
        root->addChild(m_pMapNode);
        _mViewer->setSceneData(root);
        osgEarth::GLUtils::setGlobalDefaults(this->_mViewer->getCamera()->getOrCreateStateSet());
        ///testsssssssssssssssssssssssssssssssssssssss
        osgEarth::Annotation::FeatureNode* pathNode = 0;
        {
                osgEarth::Symbology::Geometry* path = new osgEarth::Symbology::LineString();
                path->push_back(osg::Vec3d(-74, 40.714, 0));   // New York
                path->push_back(osg::Vec3d(139.75, 35.68, 0)); // Tokyo

                osgEarth::Features::Feature* pathFeature = new osgEarth::Features::Feature(path, m_pMapNode->getMapSRS());
                pathFeature->geoInterp() = osgEarth::GeoInterpolation::GEOINTERP_GREAT_CIRCLE;

                osgEarth::Symbology::Style pathStyle;
                pathStyle.getOrCreate<osgEarth::Symbology::LineSymbol>()->stroke()->color() = osgEarth::Symbology::Color::White;
                pathStyle.getOrCreate<osgEarth::Symbology::LineSymbol>()->stroke()->width() = 1.0f;
                pathStyle.getOrCreate<osgEarth::Symbology::LineSymbol>()->stroke()->smooth() = true;
                pathStyle.getOrCreate<osgEarth::Symbology::LineSymbol>()->tessellationSize() = 75000;
                pathStyle.getOrCreate<osgEarth::Symbology::PointSymbol>()->size() = 8;
                pathStyle.getOrCreate<osgEarth::Symbology::PointSymbol>()->fill()->color() = osgEarth::Symbology::Color::Red;
                pathStyle.getOrCreate<osgEarth::Symbology::PointSymbol>()->smooth() = true;

                pathStyle.getOrCreate<osgEarth::Symbology::AltitudeSymbol>()->clamping() = osgEarth::Symbology::AltitudeSymbol::CLAMP_TO_TERRAIN;

                pathStyle.getOrCreate<osgEarth::Symbology::AltitudeSymbol>()->technique() = osgEarth::Symbology::AltitudeSymbol::TECHNIQUE_GPU;//problem
               //pathStyle.getOrCreate<osgEarth::Symbology::AltitudeSymbol>()->technique() = osgEarth::Symbology::AltitudeSymbol::TECHNIQUE_DRAPE;//problem

               //pathStyle.getOrCreate<osgEarth::Symbology::AltitudeSymbol>()->technique() = osgEarth::Symbology::AltitudeSymbol::TECHNIQUE_MAP;//no problem
               //pathStyle.getOrCreate<osgEarth::Symbology::AltitudeSymbol>()->technique() = osgEarth::Symbology::AltitudeSymbol::TECHNIQUE_SCENE;//no problem

                pathStyle.getOrCreate<osgEarth::Symbology::RenderSymbol>()->depthOffset()->enabled() = true;

                //OE_INFO << "Path extent = " << pathFeature->getExtent().toString() << std::endl;

                pathNode = new osgEarth::Annotation::FeatureNode(pathFeature, pathStyle);
                m_pMapNode->addChild(pathNode);

        }

  }


  virtual ~QtOSGWidget() {}

  void setScale(qreal X, qreal Y)
  {
    m_scaleX = X;
    m_scaleY = Y;
    this->resizeGL(this->width(), this->height());
  }

protected:

  virtual void paintGL() {
    _mViewer->frame();
  }

  virtual void resizeGL(int width, int height)
  {
    this->getEventQueue()->windowResize(this->x()*m_scaleX, this->y() * m_scaleY, width*m_scaleX, height*m_scaleY);
    _mGraphicsWindow->resized(this->x()*m_scaleX, this->y() * m_scaleY, width*m_scaleX, height*m_scaleY);
    osg::Camera* camera = _mViewer->getCamera();
    camera->setViewport(0, 0, this->width()*m_scaleX, this->height()* m_scaleY);
  }

  virtual void initializeGL() {
    osg::StateSet* stateSet = _mViewer->getSceneData()->getOrCreateStateSet();
    osg::Material* material = new osg::Material;
    material->setColorMode(osg::Material::AMBIENT_AND_DIFFUSE);
    stateSet->setAttributeAndModes(material, osg::StateAttribute::ON);
    stateSet->setMode(GL_DEPTH_TEST, osg::StateAttribute::ON);
  }

  virtual void mouseMoveEvent(QMouseEvent* event)
  {
    this->getEventQueue()->mouseMotion(event->x()*m_scaleX, event->y()*m_scaleY);
  }

  virtual void mousePressEvent(QMouseEvent* event)
  {
    unsigned int button = 0;
    switch (event->button()) {
    case Qt::LeftButton:
      button = 1;
      break;
    case Qt::MiddleButton:
      button = 2;
      break;
    case Qt::RightButton:
      button = 3;
      break;
    default:
      break;
    }
    this->getEventQueue()->mouseButtonPress(event->x()*m_scaleX, event->y()*m_scaleY, button);
  }

  virtual void mouseReleaseEvent(QMouseEvent* event)
  {
    unsigned int button = 0;
    switch (event->button()) {
    case Qt::LeftButton:
      button = 1;
      break;
    case Qt::MiddleButton:
      button = 2;
      break;
    case Qt::RightButton:
      button = 3;
      break;
    default:
      break;
    }
    this->getEventQueue()->mouseButtonRelease(event->x()*m_scaleX, event->y()*m_scaleY, button);
  }

  virtual void wheelEvent(QWheelEvent* event)
  {
    int delta = event->delta();
    osgGA::GUIEventAdapter::ScrollingMotion motion = delta > 0 ?
      osgGA::GUIEventAdapter::SCROLL_UP : osgGA::GUIEventAdapter::SCROLL_DOWN;
    this->getEventQueue()->mouseScroll(motion);
  }

  virtual bool event(QEvent* event)
  {
    bool handled = QOpenGLWidget::event(event);
    this->update();
    return handled;
  }

  virtual void keyPressEvent(QKeyEvent * event)
  {
          int keyValue = s_QtKeyboardMap.remapKey(event);
          getEventQueue()->keyPress(keyValue);
  }

  virtual void keyReleaseEvent(QKeyEvent * event)
  {
          int keyValue = s_QtKeyboardMap.remapKey(event);
          getEventQueue()->keyRelease(keyValue);
  }

  void setKeyboardModifiers(QInputEvent * event)
  {
          Qt::KeyboardModifiers amod = event->modifiers();
          unsigned int modkey = event->modifiers() & (Qt::ShiftModifier |
                  Qt::ControlModifier |
                  Qt::AltModifier);
          unsigned int mask = 0;

          if (modkey & Qt::ShiftModifier) mask |= osgGA::GUIEventAdapter::MODKEY_SHIFT;
          if (modkey & Qt::ControlModifier) mask |= osgGA::GUIEventAdapter::MODKEY_CTRL;
          if (modkey & Qt::AltModifier) mask |= osgGA::GUIEventAdapter::MODKEY_ALT;

          this->getEventQueue()->getCurrentEventState()->setModKeyMask(mask);
  }
public:

  osgGA::EventQueue* getEventQueue() const {
    osgGA::EventQueue* eventQueue = _mGraphicsWindow->getEventQueue();
    return eventQueue;
  }

  osg::ref_ptr<osgViewer::GraphicsWindowEmbedded> _mGraphicsWindow;
  osg::ref_ptr<osgViewer::Viewer> _mViewer;
  qreal m_scaleX, m_scaleY;
};

int main(int argc, char** argv)
{
  QApplication qapp(argc, argv);

  //QMainWindow window;
  OSGQT window;
  QtOSGWidget* widget = new QtOSGWidget(1, 1, &window);
  //widget->move(QPoint())
  //widget->setGeometry(QRect(0, 0, 800, 600));
  //widget->setBaseSize(600, 800);

  //window.
  window.resize(600, 800);
  window.setCentralWidget(widget);
 
  QPushButton * pBtn = new QPushButton(widget);
  pBtn->setText("Button");
  QRect rc(50, 50, 100, 100);
  //window.showFullScreen();
  window.show();

  return qapp.exec();
}

gwaldron gwaldron
Reply | Threaded
Open this post in threaded view
|

Re: When I use TECHNIQUE_GPU or TECHNIQUE_DRAPE The scene disappears.

Hi Buaahc,

Try reading this thread and see if it helps.

http://forum.osgearth.org/solved-black-screen-with-drape-mode-in-a-QOpenGLWidget-td7592420.html#a7592421
Glenn Waldron / Pelican Mapping
buaahc buaahc
Reply | Threaded
Open this post in threaded view
|

Re: When I use TECHNIQUE_GPU or TECHNIQUE_DRAPE The scene disappears.

Amazing!Greate Job!