查看: 2450|回复: 1

RTT在Qt的QGraphicsView框架中的问题

[复制链接]

该用户从未签到

发表于 2015-11-23 18:32:30 | 显示全部楼层 |阅读模式
本帖最后由 hisong7 于 2015-11-24 10:41 编辑


       我在使用osg + qt(QGraphicsView -QGraphicsScene ),框架正常显示osg场景没有问题,但是在加入RTT节点就有问题:
提示错误如下截图
error.png
,另外RTT在纯OSG环境下可以显示正常,请各位帮忙看看是什么原因?
    以下是我写的demo,Qt4.8.6(64bit) + osg 3.0.1(64bit),win8环境 64bit环境下

main.cpp
  1. #include <QtGui/QApplication>
  2. #include <QtGui/QMainWindow>
  3. #include <QGraphicsView>

  4. #include "GraphicView.h"

  5. int main(int argc, char *argv[])  
  6. {  
  7.     QApplication a(argc, argv);

  8.     GraphicsView graphicsView;
  9.     graphicsView.setFixedSize(960, 640);
  10.     graphicsView.show();
  11.     return a.exec();  
  12. }  
复制代码


EventAdapter.h
  1. #pragma once

  2. #include <QGraphicsView>
  3. #include <QInputEvent>
  4. #include <osgViewer/GraphicsWindow>

  5. class EventAdapter : public QGraphicsView
  6. {
  7. public:
  8.     EventAdapter();
  9.     ~EventAdapter();

  10. protected:
  11.     // 处理按键事件
  12.     virtual void keyPressEvent(QKeyEvent* event);
  13.     virtual void keyReleaseEvent(QKeyEvent* event);

  14.     // 处理鼠标单击事件
  15.     virtual void mousePressEvent(QMouseEvent* event);
  16.     virtual void mouseReleaseEvent(QMouseEvent* event);

  17.     // 处理鼠标双击事件
  18.     virtual void mouseDoubleClickEvent(QMouseEvent* event);
  19.     virtual void mouseMoveEvent(QMouseEvent* event);

  20.     // 处理鼠标中轮滑动事件
  21.     virtual void wheelEvent(QWheelEvent* event);

  22.     // 处理窗口大小改变事件
  23.     virtual void resizeEvent(QResizeEvent *event);

  24.     // 处理鼠标移动事件
  25.     virtual void moveEvent(QMoveEvent* event);

  26.     // timerEvent事件
  27.     virtual void timerEvent(QTimerEvent *event);

  28. private:
  29.     void setKeyboardModifiers(QInputEvent* event);

  30. protected:
  31.     osgViewer::GraphicsWindow* m_pGraphicsWindow;
  32. };
复制代码


EventAdapter.cpp
  1. #include "EventAdapter.h"


  2. EventAdapter::EventAdapter(void)
  3. {
  4. }


  5. EventAdapter::~EventAdapter(void)
  6. {
  7. }
  8. void EventAdapter::keyPressEvent(QKeyEvent* event)
  9. {
  10.     QPoint pos = QCursor::pos();
  11.     QList<QGraphicsItem*> listItems = items(mapToScene(pos.x(), pos.y()).toPoint());
  12.     if (listItems.size() <= 0)
  13.     {
  14.         setKeyboardModifiers(event);
  15.         m_pGraphicsWindow->getEventQueue()->keyPress(
  16.             (osgGA::GUIEventAdapter::KeySymbol)*(event->text().toLatin1().data()));
  17.     }
  18.     QGraphicsView::keyPressEvent(event);
  19. }

  20. void EventAdapter::keyReleaseEvent(QKeyEvent* event)
  21. {
  22.     setKeyboardModifiers(event);
  23.     m_pGraphicsWindow->getEventQueue()->keyRelease(
  24.         (osgGA::GUIEventAdapter::KeySymbol)*(event->text().toLatin1().data()));

  25.     QGraphicsView::keyReleaseEvent(event);
  26. }

  27. void EventAdapter::mousePressEvent(QMouseEvent* event)
  28. {
  29.     QPoint pos = event->pos();
  30.     QList<QGraphicsItem*> listItems = items(mapToScene(pos.x(), pos.y()).toPoint());
  31.     if (listItems.size() > 0)
  32.     {
  33.         QGraphicsView::mousePressEvent(event);
  34.     }
  35.     else
  36.     {
  37.         int button = 0;
  38.         switch (event->button())
  39.         {
  40.         case Qt::LeftButton: button = 1; break;
  41.         case Qt::MidButton: button = 2; break;
  42.         case Qt::RightButton: button = 3; break;
  43.         case Qt::NoButton: button = 0; break;
  44.         default: button = 0; break;
  45.         }
  46.         setKeyboardModifiers(event);
  47.         m_pGraphicsWindow->getEventQueue()->mouseButtonPress(event->x(), event->y(), button);
  48.     }
  49. }

  50. void EventAdapter::mouseReleaseEvent(QMouseEvent* event)
  51. {
  52.     int button = 0;
  53.     switch (event->button())
  54.     {
  55.     case Qt::LeftButton: button = 1; break;
  56.     case Qt::MidButton: button = 2; break;
  57.     case Qt::RightButton: button = 3; break;
  58.     case Qt::NoButton: button = 0; break;
  59.     default: button = 0; break;
  60.     }
  61.     setKeyboardModifiers(event);
  62.     m_pGraphicsWindow->getEventQueue()->mouseButtonRelease(event->x(), event->y(), button);

  63.     QGraphicsView::mouseReleaseEvent(event);
  64. }

  65. void EventAdapter::mouseDoubleClickEvent(QMouseEvent* event)
  66. {
  67.     QPoint pos = QCursor::pos();
  68.     QList<QGraphicsItem*> listItems = items(mapToScene(pos.x(), pos.y()).toPoint());
  69.     if (listItems.size() > 0)
  70.     {
  71.         return;
  72.     }
  73.     int button = 0;
  74.     switch (event->button())
  75.     {
  76.     case Qt::LeftButton: button = 1; break;
  77.     case Qt::MidButton: button = 2; break;
  78.     case Qt::RightButton: button = 3; break;
  79.     case Qt::NoButton: button = 0; break;
  80.     default: button = 0; break;
  81.     }
  82.     setKeyboardModifiers(event);
  83.     m_pGraphicsWindow->getEventQueue()->mouseDoubleButtonPress(event->x(), event->y(), button);

  84.     QGraphicsView::mouseDoubleClickEvent(event);
  85. }

  86. void EventAdapter::mouseMoveEvent(QMouseEvent* event)
  87. {
  88.     setKeyboardModifiers(event);
  89.     m_pGraphicsWindow->getEventQueue()->mouseMotion(event->x(), event->y());
  90.     QGraphicsView::mouseMoveEvent(event);
  91. }

  92. void EventAdapter::wheelEvent(QWheelEvent* event)
  93. {
  94.     setKeyboardModifiers(event);
  95.     m_pGraphicsWindow->getEventQueue()->mouseScroll(
  96.         event->orientation() == Qt::Vertical ?
  97.         (event->delta() > 0 ? osgGA::GUIEventAdapter::SCROLL_UP : osgGA::GUIEventAdapter::SCROLL_DOWN) :
  98.         (event->delta() > 0 ? osgGA::GUIEventAdapter::SCROLL_LEFT : osgGA::GUIEventAdapter::SCROLL_RIGHT));
  99.     QGraphicsView::wheelEvent(event);
  100. }

  101. void EventAdapter::resizeEvent(QResizeEvent *event)
  102. {
  103.     if (scene())
  104.     {
  105.         scene()->setSceneRect(QRect(QPoint(0, 0), event->size()));
  106.     }

  107.     const QSize& size = event->size();
  108.     m_pGraphicsWindow->resized(x(), y(), size.width(), size.height());
  109.     m_pGraphicsWindow->getEventQueue()->windowResize(x(), y(), size.width(), size.height());
  110.     m_pGraphicsWindow->requestRedraw();

  111.     const QSize& oldSize = event->oldSize();
  112.     int oldWidth = oldSize.width();
  113.     int oldHeight = oldSize.height();

  114.     int newWidth = size.width();
  115.     int newHeight = size.height();

  116.     double widthChangeRatio = double(newWidth) / double(oldWidth);
  117.     double heigtChangeRatio = double(newHeight) / double(oldHeight);
  118.     double aspectRatioChange = widthChangeRatio / heigtChangeRatio;
  119.     QGraphicsView::resizeEvent(event);
  120. }

  121. void EventAdapter::moveEvent(QMoveEvent* event)
  122. {
  123.     const QPoint& pos = event->pos();
  124.     m_pGraphicsWindow->resized(pos.x(), pos.y(), width(), height());
  125.     m_pGraphicsWindow->getEventQueue()->windowResize(pos.x(), pos.y(), width(), height());

  126.     QGraphicsView::moveEvent(event);
  127. }

  128. void EventAdapter::timerEvent(QTimerEvent *event)
  129. {
  130.     scene()->update();
  131. }

  132. void EventAdapter::setKeyboardModifiers(QInputEvent* event)
  133. {
  134.     int modkey = event->modifiers() & (Qt::ShiftModifier | Qt::ControlModifier | Qt::AltModifier);
  135.     unsigned int mask = 0;
  136.     if (modkey & Qt::ShiftModifier) mask |= osgGA::GUIEventAdapter::MODKEY_SHIFT;
  137.     if (modkey & Qt::ControlModifier) mask |= osgGA::GUIEventAdapter::MODKEY_CTRL;
  138.     if (modkey & Qt::AltModifier) mask |= osgGA::GUIEventAdapter::MODKEY_ALT;

  139.     m_pGraphicsWindow->getEventQueue()->getCurrentEventState()->setModKeyMask(mask);
  140. }
复制代码


GraphicsView.h
  1. #pragma once

  2. #include  <QGraphicsView>
  3. #include <QInputEvent>

  4. #include "EventAdapter.h"
  5. #include <osgViewer/Viewer>
  6. #include <osgDB/WriteFile>
  7. #include <osg/ValueObject>
  8. #include <iostream>
  9. using namespace std;

  10. class PhotoCallback : public osg::Camera::DrawCallback
  11. {
  12. public:
  13.     PhotoCallback( osg::Image* img, osgViewer::Viewer* viewer)
  14.         : _image(img), m_viewer(viewer) {}

  15.     virtual void operator()( osg::RenderInfo& renderInfo ) const
  16.     {
  17.         bool capturing = false;
  18.         if ( _image.valid() && _image->getUserValue("Capture", capturing) )
  19.         {
  20.             osg::GraphicsContext* gc = renderInfo.getState()->getGraphicsContext();

  21.             if ( capturing && gc->getTraits() )
  22.             {
  23.                 std::stringstream ss; ss << "photo" << ".png";
  24.                 if ( osgDB::writeImageFile(*_image, ss.str()) )
  25.                 {
  26.                 }
  27.             }
  28.             _image->setUserValue( "Capture", false );
  29.         }
  30.     }

  31. protected:
  32.     osg::ref_ptr<osg::Image> _image;
  33.     mutable int _fileIndex;
  34.     osgViewer::Viewer*  m_viewer;
  35. };


  36. class UserEventHanlder : public osgGA::GUIEventHandler
  37. {
  38. public:
  39.     UserEventHanlder(){}
  40.     virtual bool handle(const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& aa)
  41.     {
  42.         switch (ea.getEventType())
  43.         {
  44.         case osgGA::GUIEventAdapter::KEYDOWN:
  45.             {
  46.                 switch (ea.getKey())
  47.                 {
  48.                 case osgGA::GUIEventAdapter::KEY_Q:
  49.                     {
  50.                         cout << "press QQQQ" << endl;
  51.                         if (m_photoImg.valid())
  52.                         {
  53.                             m_photoImg->setUserValue( "Capture", true );
  54.                         }
  55.                         return true;
  56.                         break;
  57.                     }
  58.                 }
  59.             }
  60.         }
  61.         return false;
  62.     }
  63.     void setImage(osg::Image* image)
  64.     {
  65.         m_photoImg = image;
  66.     }
  67. private:
  68.     osg::ref_ptr<osg::Image> m_photoImg;
  69. };

  70. struct Rect2D
  71. {
  72.     float left;
  73.     float right;
  74.     float top;
  75.     float bottom;
  76.     float depth;
  77. };

  78. class GraphicsView : public EventAdapter
  79. {
  80.     Q_OBJECT

  81. public:
  82.     GraphicsView(QWidget* parent = 0);
  83.     ~GraphicsView();

  84.     osg::Node* createTex(osg::Image* image, osgViewer::Viewer* viewer);

  85.     osgViewer::Viewer* getOSGViewer(){ return m_pViewer; }
  86.     osg::Group* getRoot(){ return m_pSceneData; }

  87. protected:
  88.     // 完成OSG每一帧的update操作
  89.     virtual void drawBackground(QPainter *painter, const QRectF& rect);
  90.     virtual void timerEvent(QTimerEvent *event);

  91. private:
  92.     void init();

  93. private:
  94.     osg::Group* m_pSceneData;
  95.     osg::StateSet* m_pStateSet;
  96.     osgViewer::Viewer* m_pViewer;
  97. };
复制代码


Graphics.cpp
  1. #include "GraphicView.h"

  2. #include <osgGA/GUIEventAdapter>
  3. #include <osgViewer/ViewerEventHandlers>
  4. #include <QGLWidget>
  5. #include <osgGA/StateSetManipulator>
  6. #include <osgGA/TrackballManipulator>
  7. #include <osgDB/ReadFile>
  8. #include <osg/MatrixTransform>
  9. #include <osg/Texture2D>

  10. GraphicsView::GraphicsView(QWidget *parent)
  11. {
  12.     this->setScene(new QGraphicsScene);
  13.     init();
  14. }

  15. GraphicsView::~GraphicsView()
  16. {

  17. }

  18. void GraphicsView::init()
  19. {
  20.     // QGraphicsView的Viewport一定要设置为QGLWidget,因为只有这样才能用OpenGL来对OSG进行更新
  21.     QGLWidget* glViewPort = new QGLWidget;
  22.     glViewPort->setMouseTracking(true);
  23.     glViewPort->setMaximumSize(2000, 2000);
  24.     this->setViewport(glViewPort);
  25.     this->setViewportUpdateMode(QGraphicsView::FullViewportUpdate);
  26.     startTimer(10);

  27.     osg::Group* pRoot = new osg::Group;
  28.     pRoot->setName("root");

  29.     m_pSceneData = new osg::Group;
  30.     m_pSceneData->setName("SceneData");
  31.     osg::Node* node = osgDB::readNodeFile("cow.osg");
  32.     m_pStateSet = new osg::StateSet;
  33.     m_pStateSet->setMode(GL_DEPTH_TEST, osg::StateAttribute::ON);
  34.     pRoot->setStateSet(m_pStateSet);

  35.     m_pViewer = new osgViewer::Viewer;
  36.     osg::Image* image = new osg::Image;
  37.     osg::Node* imgNode = createTex(image, m_pViewer);
  38.     pRoot->addChild(node);
  39.     pRoot->addChild(imgNode);

  40.     UserEventHanlder* handler = new UserEventHanlder;
  41.     handler->setImage(image);
  42.     m_pViewer->addEventHandler(handler);
  43.     m_pViewer->setThreadingModel(osgViewer::ViewerBase::SingleThreaded);
  44. //
  45. //     m_pEarthManipulator->getSettings()->setMinMaxPitch(-90.0, -7.0);
  46.     m_pViewer->setCameraManipulator(new osgGA::TrackballManipulator);
  47.     m_pViewer->addEventHandler(new osgViewer::StatsHandler);
  48.     m_pViewer->addEventHandler(new osgGA::StateSetManipulator(m_pViewer->getCamera()->getOrCreateStateSet()));
  49.     m_pViewer->getCamera()->setNearFarRatio(0.0000002);
  50.     m_pViewer->getCamera()->setComputeNearFarMode(osg::CullSettings::COMPUTE_NEAR_FAR_USING_PRIMITIVES);
  51.     m_pViewer->setUpViewerAsEmbeddedInWindow(0, 0, width(), height());
  52.     m_pViewer->setSceneData(pRoot);
  53.     m_pGraphicsWindow = dynamic_cast<osgViewer::GraphicsWindow*>(
  54.         m_pViewer->getCamera()->getGraphicsContext());
  55. }

  56. void GraphicsView::timerEvent(QTimerEvent *event)
  57. {
  58.     this->scene()->update();
  59. }

  60. void GraphicsView::drawBackground(QPainter *painter, const QRectF& rect)
  61. {
  62.     if (painter->paintEngine()->type() != QPaintEngine::OpenGL2)
  63.     {
  64.         return;
  65.     }

  66.     // Save the painter state
  67.     painter->save();
  68.     painter->beginNativePainting();

  69.     // OSG帧更新
  70.     m_pViewer->frame();

  71.     painter->endNativePainting();
  72.     painter->restore();
  73. }

  74. osg::Node* GraphicsView::createTex( osg::Image* image, osgViewer::Viewer* viewer )
  75. {
  76.     osg::Group * parent = new osg::Group;
  77.     //    osg::Texture* texture = 0;

  78.     unsigned tex_width = 420;
  79.     unsigned tex_height = 750;

  80.     osg::Node * cow = osgDB::readNodeFile("humanBody.osgb");
  81.     osg::MatrixTransform * subgraph = new osg::MatrixTransform;
  82.     subgraph->addChild(cow);
  83.     subgraph->getOrCreateStateSet()->setMode(GL_LIGHTING, osg::StateAttribute::OFF);

  84.     osg::Camera* camera = new osg::Camera;
  85.     {

  86.         // set up the background color and clear mask.
  87.         camera->setClearColor(osg::Vec4(0.1f,0.1f,0.3f,1.0f));
  88.         camera->setClearMask(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

  89.         const osg::BoundingSphere& bs = subgraph->getBound();
  90.         if (!bs.valid())
  91.         {
  92.             return subgraph;
  93.         }

  94.         float znear = 1.0f*bs.radius();
  95.         float zfar  = 3.0f*bs.radius();

  96.         // 2:1 aspect ratio as per flag geometry below.
  97.         float proj_top   = 0.25f*znear;
  98.         float proj_right = 0.5f*znear;

  99.         znear *= 0.1f;
  100.         zfar *= 1.1f;

  101.         // set up projection.
  102.         //camera->setProjectionMatrixAsFrustum(-proj_right, proj_right, -proj_top, proj_top, znear, zfar);
  103.         double fov = osg::RadiansToDegrees(4 *  atan(0.25));
  104.         camera->setProjectionMatrixAsPerspective(fov,  0.5, znear, zfar);
  105.         // set view
  106.         camera->setReferenceFrame(osg::Transform::ABSOLUTE_RF);
  107.         camera->setViewMatrixAsLookAt(bs.center() - osg::Vec3(0.0f, 2.0f, 0.0f) * bs. radius(), bs.center(), osg::Vec3(0.0f, 0.0f, 1.0f));

  108.         // set viewport
  109.         camera->setViewport(0, 0, tex_width, tex_height);

  110.         // set the camera to render before the main camera.
  111.         camera->setRenderOrder(osg::Camera::PRE_RENDER);

  112.         // tell the camera to use OpenGL frame buffer object where supported.
  113.         camera->setRenderTargetImplementation(osg::Camera::PIXEL_BUFFER);

  114.         // attach the texture and use it as the color buffer.
  115.         //osg::Image* image = new osg::Image;
  116.         osg::Texture2D* texture = new osg::Texture2D;
  117.         texture->setTextureSize(200, 200);
  118.         texture->setInternalFormat(GL_RGBA32I_EXT);
  119.         texture->setSourceFormat(GL_RGBA_INTEGER_EXT);
  120.         texture->setSourceType(GL_BYTE);
  121.         texture->setFilter(osg::Texture2D::MIN_FILTER, osg::Texture2D::NEAREST);
  122.         texture->setFilter(osg::Texture2D::MAG_FILTER, osg::Texture2D::NEAREST);
  123.         texture->setWrap(osg::Texture::WRAP_S, osg::Texture::CLAMP_TO_EDGE);
  124.         texture->setWrap(osg::Texture::WRAP_T, osg::Texture::CLAMP_TO_EDGE);
  125.         texture->setDataVariance(osg::Object::DYNAMIC);
  126.         texture->setUseHardwareMipMapGeneration(false);
  127.         texture->setResizeNonPowerOfTwoHint(false);
  128.         camera->attach(osg::Camera::COLOR_BUFFER, texture);
  129.         //camera->attach(osg::Camera::COLOR_BUFFER, image);
  130.         //camera->setPostDrawCallback(new PhotoCallback(image, viewer));
  131.         //osgDB::writeImageFile(*image, "screen.bmp");
  132.     }

  133.     camera->addChild(subgraph);
  134.     parent->addChild(camera);
  135.     //viewer->addSlave(camera, false);
  136.     return parent;
  137. }
复制代码


创建RTT的代码是createTex()函数,请各位大神帮忙看看,不胜感谢

该用户从未签到

 楼主| 发表于 2015-12-17 17:18:25 | 显示全部楼层
自己的帖子来结了吧
  1. camera->setRenderTargetImplementation(osg::Camera::PIXEL_BUFFER);
复制代码

改为
  1. camera->setRenderTargetImplementation(osg::Camera::FRAME_BUFFER_OBJECT);
复制代码

就可以了
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

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

联系我们

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