|
平台:OSG2.8+MinGW+Qt +Windows2008
症状:
1.使用控制台程序用osgDB::readNodeFile("lz.osg") 可以显示数据
2.Qt和OSG结合的代码,如果Node是用代码构建的 可以显示
如果Node是通过osgDB::readNodeFile读出来的 系统崩溃
代码如下:
1.控制台程序的代码:- #include <iostream>
- #include <osg/Node>
- #include <osgViewer/Viewer>
- #include <osgDB/ReadFile>
- using namespace std;
- using namespace osg;
- using namespace osgViewer;
- int main() {
- Viewer viewer;
- Node* node=osgDB::readNodeFile(string("lz.osg"));
- viewer.setSceneData(node);
- viewer.realize();
- viewer.run();
- return 0;
- }
复制代码 2.和Qt结合代码 SceneCanvas.h- #ifndef QSCENECANVAS_H
- #define QSCENECANVAS_H
- #include <osg/ref_ptr>
- #include <osgViewer/Viewer>
- #include <osgViewer/CompositeViewer>
- #include <osgViewer/ViewerEventHandlers>
- #include <osgGA/TrackballManipulator>
- #include <osgDB/ReadFile>
- #include <QtGui/QWidget>
- #include <QtCore/QString>
- #include <QtCore/QTimer>
- #include <QtGui/QKeyEvent>
- #include <QtGui/QApplication>
- #include <QtOpenGL/QGLWidget>
- #include <QtGui/QMainWindow>
- #include <QtGui/QMdiSubWindow>
- #include <QtGui/QMdiArea>
- using Qt::WindowFlags;
- #include "ui_QSceneCanvas.h"
- using namespace osg;
- using namespace osgViewer;
- using namespace osgGA;
- class QSceneCanvas: public QGLWidget, public osgViewer::Viewer {
- Q_OBJECT
- public:
- QSceneCanvas(QWidget *parent = 0);
- ~QSceneCanvas();
- GraphicsWindowEmbedded* getGraphicsWindow() {
- return _gw.get();
- }
- const GraphicsWindowEmbedded* getGraphicsWindow() const {
- return _gw.get();
- }
- public:
- virtual void paintGL();
- protected:
- //void init();
- virtual void resizeGL(int width, int height);
- virtual void keyPressEvent(QKeyEvent* event);
- virtual void keyReleaseEvent(QKeyEvent* event);
- virtual void mousePressEvent(QMouseEvent* event);
- virtual void mouseReleaseEvent(QMouseEvent* event);
- virtual void mouseMoveEvent(QMouseEvent* event);
- ref_ptr<GraphicsWindowEmbedded> _gw;
- private:
- QTimer _timer;
- GUIEventAdapter::KeySymbol getSceneKeyCode(const QKeyEvent& event);
- private:
- Ui::QSceneCanvasClass ui;
- };
- #endif // QSCENECANVAS_H
复制代码 3.和Qt结合的代码 SceneCanvas.cpp- #include "QSceneCanvas.h"
- QSceneCanvas::QSceneCanvas(QWidget *parent) :
- QGLWidget(parent) {
- ui.setupUi(this);
- _gw = new GraphicsWindowEmbedded(0, 0, width(), height());
- getCamera()->setGraphicsContext(_gw);
- getCamera()->setProjectionMatrixAsPerspective(45.0f,
- static_cast<double> (width()) / static_cast<double> (height()),
- 1.0, 1000.0);
- getCamera()->setViewport(new osg::Viewport(0, 0, width(), height()));
- setCameraManipulator(new osgGA::TrackballManipulator);
- setThreadingModel(osgViewer::Viewer::SingleThreaded);
- setFocusPolicy(Qt::ClickFocus);
- }
- QSceneCanvas::~QSceneCanvas() {
- }
- void QSceneCanvas::paintGL() {
- frame();
- }
- void QSceneCanvas::resizeGL(int width, int height) {
- _gw->getEventQueue()->windowResize(0, 0, width, height);
- _gw->resized(0, 0, width, height);
- }
- void QSceneCanvas::keyPressEvent(QKeyEvent* event) {
- _gw->getEventQueue()->keyPress(getSceneKeyCode(*event));
- }
- void QSceneCanvas::keyReleaseEvent(QKeyEvent* event) {
- _gw->getEventQueue()->keyRelease(getSceneKeyCode(*event));
- }
- void QSceneCanvas::mousePressEvent(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;
- }
- _gw->getEventQueue()->mouseButtonPress(event->x(), event->y(), button);
- }
- void QSceneCanvas::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;
- }
- _gw->getEventQueue()->mouseButtonRelease(event->x(), event->y(), button);
- }
- void QSceneCanvas::mouseMoveEvent(QMouseEvent* event) {
- _gw->getEventQueue()->mouseMotion(event->x(), event->y());
- }
- GUIEventAdapter::KeySymbol QSceneCanvas::getSceneKeyCode(const QKeyEvent& event) {
- return (GUIEventAdapter::KeySymbol) *(event.text().toAscii().data());
- }
复制代码 4.我是如何使用的:- void QMainFrame::onLoadData(){
- QFileDialog fd(this);
- fd.setFilter(QString::fromUtf8("osg File(*.osg)"));
- if(fd.exec()==QDialog::Accepted)
- {
- QString file=fd.selectedFiles().at(0);
- Node* dataNode=osgDB::readNodeFile(string("lz.osg"));
- //SceneNodeFactory fact;
- //Node* dataNode=fact.createSceneNode();
- _masterCanvas.setSceneData(dataNode);
- }
- }
复制代码 本来是要根据选择的文件读取的 我在这里直接写死了。
_masterCanvas就是SceneCanvas的一个对象,是顶层窗口的成员。
如果dataNode使用从文件读取的 程序直接就崩溃了 如果是用createSceneNode函数构造的 是可以运行
createSceneNode的内容是一本交做osgTutorial.chm的书上的第一个例子.
感觉有点郁闷了...
另外:自带的osgViewerQt好像也不能读取文件
高手们都出来救救我吧!
这里是我的整个工程
QGIS.zip
(212.66 KB, 下载次数: 146)
|
|