|
本帖最后由 hisong7 于 2015-11-24 10:41 编辑
我在使用osg + qt(QGraphicsView -QGraphicsScene ),框架正常显示osg场景没有问题,但是在加入RTT节点就有问题:
提示错误如下截图
,另外RTT在纯OSG环境下可以显示正常,请各位帮忙看看是什么原因?
以下是我写的demo,Qt4.8.6(64bit) + osg 3.0.1(64bit),win8环境 64bit环境下
main.cpp
- #include <QtGui/QApplication>
- #include <QtGui/QMainWindow>
- #include <QGraphicsView>
- #include "GraphicView.h"
- int main(int argc, char *argv[])
- {
- QApplication a(argc, argv);
- GraphicsView graphicsView;
- graphicsView.setFixedSize(960, 640);
- graphicsView.show();
- return a.exec();
- }
复制代码
EventAdapter.h
- #pragma once
- #include <QGraphicsView>
- #include <QInputEvent>
- #include <osgViewer/GraphicsWindow>
- class EventAdapter : public QGraphicsView
- {
- public:
- EventAdapter();
- ~EventAdapter();
- protected:
- // 处理按键事件
- virtual void keyPressEvent(QKeyEvent* event);
- virtual void keyReleaseEvent(QKeyEvent* event);
- // 处理鼠标单击事件
- virtual void mousePressEvent(QMouseEvent* event);
- virtual void mouseReleaseEvent(QMouseEvent* event);
- // 处理鼠标双击事件
- virtual void mouseDoubleClickEvent(QMouseEvent* event);
- virtual void mouseMoveEvent(QMouseEvent* event);
- // 处理鼠标中轮滑动事件
- virtual void wheelEvent(QWheelEvent* event);
- // 处理窗口大小改变事件
- virtual void resizeEvent(QResizeEvent *event);
- // 处理鼠标移动事件
- virtual void moveEvent(QMoveEvent* event);
- // timerEvent事件
- virtual void timerEvent(QTimerEvent *event);
- private:
- void setKeyboardModifiers(QInputEvent* event);
- protected:
- osgViewer::GraphicsWindow* m_pGraphicsWindow;
- };
复制代码
EventAdapter.cpp
- #include "EventAdapter.h"
- EventAdapter::EventAdapter(void)
- {
- }
- EventAdapter::~EventAdapter(void)
- {
- }
- void EventAdapter::keyPressEvent(QKeyEvent* event)
- {
- QPoint pos = QCursor::pos();
- QList<QGraphicsItem*> listItems = items(mapToScene(pos.x(), pos.y()).toPoint());
- if (listItems.size() <= 0)
- {
- setKeyboardModifiers(event);
- m_pGraphicsWindow->getEventQueue()->keyPress(
- (osgGA::GUIEventAdapter::KeySymbol)*(event->text().toLatin1().data()));
- }
- QGraphicsView::keyPressEvent(event);
- }
- void EventAdapter::keyReleaseEvent(QKeyEvent* event)
- {
- setKeyboardModifiers(event);
- m_pGraphicsWindow->getEventQueue()->keyRelease(
- (osgGA::GUIEventAdapter::KeySymbol)*(event->text().toLatin1().data()));
- QGraphicsView::keyReleaseEvent(event);
- }
- void EventAdapter::mousePressEvent(QMouseEvent* event)
- {
- QPoint pos = event->pos();
- QList<QGraphicsItem*> listItems = items(mapToScene(pos.x(), pos.y()).toPoint());
- if (listItems.size() > 0)
- {
- QGraphicsView::mousePressEvent(event);
- }
- else
- {
- int button = 0;
- switch (event->button())
- {
- case Qt::LeftButton: button = 1; break;
- case Qt::MidButton: button = 2; break;
- case Qt::RightButton: button = 3; break;
- case Qt::NoButton: button = 0; break;
- default: button = 0; break;
- }
- setKeyboardModifiers(event);
- m_pGraphicsWindow->getEventQueue()->mouseButtonPress(event->x(), event->y(), button);
- }
- }
- void EventAdapter::mouseReleaseEvent(QMouseEvent* event)
- {
- int button = 0;
- switch (event->button())
- {
- case Qt::LeftButton: button = 1; break;
- case Qt::MidButton: button = 2; break;
- case Qt::RightButton: button = 3; break;
- case Qt::NoButton: button = 0; break;
- default: button = 0; break;
- }
- setKeyboardModifiers(event);
- m_pGraphicsWindow->getEventQueue()->mouseButtonRelease(event->x(), event->y(), button);
- QGraphicsView::mouseReleaseEvent(event);
- }
- void EventAdapter::mouseDoubleClickEvent(QMouseEvent* event)
- {
- QPoint pos = QCursor::pos();
- QList<QGraphicsItem*> listItems = items(mapToScene(pos.x(), pos.y()).toPoint());
- if (listItems.size() > 0)
- {
- return;
- }
- int button = 0;
- switch (event->button())
- {
- case Qt::LeftButton: button = 1; break;
- case Qt::MidButton: button = 2; break;
- case Qt::RightButton: button = 3; break;
- case Qt::NoButton: button = 0; break;
- default: button = 0; break;
- }
- setKeyboardModifiers(event);
- m_pGraphicsWindow->getEventQueue()->mouseDoubleButtonPress(event->x(), event->y(), button);
- QGraphicsView::mouseDoubleClickEvent(event);
- }
- void EventAdapter::mouseMoveEvent(QMouseEvent* event)
- {
- setKeyboardModifiers(event);
- m_pGraphicsWindow->getEventQueue()->mouseMotion(event->x(), event->y());
- QGraphicsView::mouseMoveEvent(event);
- }
- void EventAdapter::wheelEvent(QWheelEvent* event)
- {
- setKeyboardModifiers(event);
- m_pGraphicsWindow->getEventQueue()->mouseScroll(
- event->orientation() == Qt::Vertical ?
- (event->delta() > 0 ? osgGA::GUIEventAdapter::SCROLL_UP : osgGA::GUIEventAdapter::SCROLL_DOWN) :
- (event->delta() > 0 ? osgGA::GUIEventAdapter::SCROLL_LEFT : osgGA::GUIEventAdapter::SCROLL_RIGHT));
- QGraphicsView::wheelEvent(event);
- }
- void EventAdapter::resizeEvent(QResizeEvent *event)
- {
- if (scene())
- {
- scene()->setSceneRect(QRect(QPoint(0, 0), event->size()));
- }
- const QSize& size = event->size();
- m_pGraphicsWindow->resized(x(), y(), size.width(), size.height());
- m_pGraphicsWindow->getEventQueue()->windowResize(x(), y(), size.width(), size.height());
- m_pGraphicsWindow->requestRedraw();
- const QSize& oldSize = event->oldSize();
- int oldWidth = oldSize.width();
- int oldHeight = oldSize.height();
- int newWidth = size.width();
- int newHeight = size.height();
- double widthChangeRatio = double(newWidth) / double(oldWidth);
- double heigtChangeRatio = double(newHeight) / double(oldHeight);
- double aspectRatioChange = widthChangeRatio / heigtChangeRatio;
- QGraphicsView::resizeEvent(event);
- }
- void EventAdapter::moveEvent(QMoveEvent* event)
- {
- const QPoint& pos = event->pos();
- m_pGraphicsWindow->resized(pos.x(), pos.y(), width(), height());
- m_pGraphicsWindow->getEventQueue()->windowResize(pos.x(), pos.y(), width(), height());
- QGraphicsView::moveEvent(event);
- }
- void EventAdapter::timerEvent(QTimerEvent *event)
- {
- scene()->update();
- }
- void EventAdapter::setKeyboardModifiers(QInputEvent* event)
- {
- 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;
- m_pGraphicsWindow->getEventQueue()->getCurrentEventState()->setModKeyMask(mask);
- }
复制代码
GraphicsView.h
Graphics.cpp
- #include "GraphicView.h"
- #include <osgGA/GUIEventAdapter>
- #include <osgViewer/ViewerEventHandlers>
- #include <QGLWidget>
- #include <osgGA/StateSetManipulator>
- #include <osgGA/TrackballManipulator>
- #include <osgDB/ReadFile>
- #include <osg/MatrixTransform>
- #include <osg/Texture2D>
- GraphicsView::GraphicsView(QWidget *parent)
- {
- this->setScene(new QGraphicsScene);
- init();
- }
- GraphicsView::~GraphicsView()
- {
- }
- void GraphicsView::init()
- {
- // QGraphicsView的Viewport一定要设置为QGLWidget,因为只有这样才能用OpenGL来对OSG进行更新
- QGLWidget* glViewPort = new QGLWidget;
- glViewPort->setMouseTracking(true);
- glViewPort->setMaximumSize(2000, 2000);
- this->setViewport(glViewPort);
- this->setViewportUpdateMode(QGraphicsView::FullViewportUpdate);
- startTimer(10);
- osg::Group* pRoot = new osg::Group;
- pRoot->setName("root");
- m_pSceneData = new osg::Group;
- m_pSceneData->setName("SceneData");
- osg::Node* node = osgDB::readNodeFile("cow.osg");
- m_pStateSet = new osg::StateSet;
- m_pStateSet->setMode(GL_DEPTH_TEST, osg::StateAttribute::ON);
- pRoot->setStateSet(m_pStateSet);
- m_pViewer = new osgViewer::Viewer;
- osg::Image* image = new osg::Image;
- osg::Node* imgNode = createTex(image, m_pViewer);
- pRoot->addChild(node);
- pRoot->addChild(imgNode);
- UserEventHanlder* handler = new UserEventHanlder;
- handler->setImage(image);
- m_pViewer->addEventHandler(handler);
- m_pViewer->setThreadingModel(osgViewer::ViewerBase::SingleThreaded);
- //
- // m_pEarthManipulator->getSettings()->setMinMaxPitch(-90.0, -7.0);
- m_pViewer->setCameraManipulator(new osgGA::TrackballManipulator);
- m_pViewer->addEventHandler(new osgViewer::StatsHandler);
- m_pViewer->addEventHandler(new osgGA::StateSetManipulator(m_pViewer->getCamera()->getOrCreateStateSet()));
- m_pViewer->getCamera()->setNearFarRatio(0.0000002);
- m_pViewer->getCamera()->setComputeNearFarMode(osg::CullSettings::COMPUTE_NEAR_FAR_USING_PRIMITIVES);
- m_pViewer->setUpViewerAsEmbeddedInWindow(0, 0, width(), height());
- m_pViewer->setSceneData(pRoot);
- m_pGraphicsWindow = dynamic_cast<osgViewer::GraphicsWindow*>(
- m_pViewer->getCamera()->getGraphicsContext());
- }
- void GraphicsView::timerEvent(QTimerEvent *event)
- {
- this->scene()->update();
- }
- void GraphicsView::drawBackground(QPainter *painter, const QRectF& rect)
- {
- if (painter->paintEngine()->type() != QPaintEngine::OpenGL2)
- {
- return;
- }
- // Save the painter state
- painter->save();
- painter->beginNativePainting();
- // OSG帧更新
- m_pViewer->frame();
- painter->endNativePainting();
- painter->restore();
- }
- osg::Node* GraphicsView::createTex( osg::Image* image, osgViewer::Viewer* viewer )
- {
- osg::Group * parent = new osg::Group;
- // osg::Texture* texture = 0;
- unsigned tex_width = 420;
- unsigned tex_height = 750;
- osg::Node * cow = osgDB::readNodeFile("humanBody.osgb");
- osg::MatrixTransform * subgraph = new osg::MatrixTransform;
- subgraph->addChild(cow);
- subgraph->getOrCreateStateSet()->setMode(GL_LIGHTING, osg::StateAttribute::OFF);
- osg::Camera* camera = new osg::Camera;
- {
- // set up the background color and clear mask.
- camera->setClearColor(osg::Vec4(0.1f,0.1f,0.3f,1.0f));
- camera->setClearMask(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- const osg::BoundingSphere& bs = subgraph->getBound();
- if (!bs.valid())
- {
- return subgraph;
- }
- float znear = 1.0f*bs.radius();
- float zfar = 3.0f*bs.radius();
- // 2:1 aspect ratio as per flag geometry below.
- float proj_top = 0.25f*znear;
- float proj_right = 0.5f*znear;
- znear *= 0.1f;
- zfar *= 1.1f;
- // set up projection.
- //camera->setProjectionMatrixAsFrustum(-proj_right, proj_right, -proj_top, proj_top, znear, zfar);
- double fov = osg::RadiansToDegrees(4 * atan(0.25));
- camera->setProjectionMatrixAsPerspective(fov, 0.5, znear, zfar);
- // set view
- camera->setReferenceFrame(osg::Transform::ABSOLUTE_RF);
- camera->setViewMatrixAsLookAt(bs.center() - osg::Vec3(0.0f, 2.0f, 0.0f) * bs. radius(), bs.center(), osg::Vec3(0.0f, 0.0f, 1.0f));
- // set viewport
- camera->setViewport(0, 0, tex_width, tex_height);
- // set the camera to render before the main camera.
- camera->setRenderOrder(osg::Camera::PRE_RENDER);
- // tell the camera to use OpenGL frame buffer object where supported.
- camera->setRenderTargetImplementation(osg::Camera::PIXEL_BUFFER);
- // attach the texture and use it as the color buffer.
- //osg::Image* image = new osg::Image;
- osg::Texture2D* texture = new osg::Texture2D;
- texture->setTextureSize(200, 200);
- texture->setInternalFormat(GL_RGBA32I_EXT);
- texture->setSourceFormat(GL_RGBA_INTEGER_EXT);
- texture->setSourceType(GL_BYTE);
- texture->setFilter(osg::Texture2D::MIN_FILTER, osg::Texture2D::NEAREST);
- texture->setFilter(osg::Texture2D::MAG_FILTER, osg::Texture2D::NEAREST);
- texture->setWrap(osg::Texture::WRAP_S, osg::Texture::CLAMP_TO_EDGE);
- texture->setWrap(osg::Texture::WRAP_T, osg::Texture::CLAMP_TO_EDGE);
- texture->setDataVariance(osg::Object::DYNAMIC);
- texture->setUseHardwareMipMapGeneration(false);
- texture->setResizeNonPowerOfTwoHint(false);
- camera->attach(osg::Camera::COLOR_BUFFER, texture);
- //camera->attach(osg::Camera::COLOR_BUFFER, image);
- //camera->setPostDrawCallback(new PhotoCallback(image, viewer));
- //osgDB::writeImageFile(*image, "screen.bmp");
- }
- camera->addChild(subgraph);
- parent->addChild(camera);
- //viewer->addSlave(camera, false);
- return parent;
- }
复制代码
创建RTT的代码是createTex()函数,请各位大神帮忙看看,不胜感谢 |
|