/
osgviewerQT.cpp
166 lines (126 loc) · 4.39 KB
/
osgviewerQT.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
// C++ source file - (C) 2003 Robert Osfield, released under the OSGPL.
#include <osgViewer/Viewer>
#include <osgViewer/StatsHandler>
#include <osgGA/TrackballManipulator>
#include <osgDB/ReadFile>
#if USE_QT4
#include <QtCore/QTimer>
#include <QtGui/QKeyEvent>
#include <QtGui/QApplication>
#include <QtOpenGL/QGLWidget>
using Qt::WFlags;
#else
class QWidget;
#include <qtimer.h>
#include <qgl.h>
#include <qapplication.h>
#endif
#include <iostream>
class AdapterWidget : public QGLWidget
{
public:
AdapterWidget( QWidget * parent = 0, const char * name = 0, const QGLWidget * shareWidget = 0, WFlags f = 0 );
virtual ~AdapterWidget() {}
osgViewer::GraphicsWindow* getGraphicsWindow() { return _gw.get(); }
const osgViewer::GraphicsWindow* getGraphicsWindow() const { return _gw.get(); }
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 );
osg::ref_ptr<osgViewer::GraphicsWindowEmbedded> _gw;
};
AdapterWidget::AdapterWidget( QWidget * parent, const char * name, const QGLWidget * shareWidget, WFlags f):
QGLWidget(parent, name, shareWidget, f)
{
_gw = new osgViewer::GraphicsWindowEmbedded(0,0,width(),height());
}
void AdapterWidget::resizeGL( int width, int height )
{
_gw->getEventQueue()->windowResize(0, 0, width, height );
_gw->resized(0,0,width,height);
}
void AdapterWidget::keyPressEvent( QKeyEvent* event )
{
_gw->getEventQueue()->keyPress( (osgGA::GUIEventAdapter::KeySymbol) event->ascii() );
}
void AdapterWidget::keyReleaseEvent( QKeyEvent* event )
{
_gw->getEventQueue()->keyRelease( (osgGA::GUIEventAdapter::KeySymbol) event->ascii() );
}
void AdapterWidget::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 AdapterWidget::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 AdapterWidget::mouseMoveEvent( QMouseEvent* event )
{
_gw->getEventQueue()->mouseMotion(event->x(), event->y());
}
class ViewerQT : public osgViewer::Viewer, public AdapterWidget
{
public:
ViewerQT(QWidget * parent = 0, const char * name = 0, const QGLWidget * shareWidget = 0, WFlags f = 0):
AdapterWidget( parent, name, shareWidget, f )
{
getCamera()->setViewport(new osg::Viewport(0,0,width(),height()));
getCamera()->setGraphicsContext(getGraphicsWindow());
setThreadingModel(osgViewer::Viewer::SingleThreaded);
connect(&_timer, SIGNAL(timeout()), this, SLOT(updateGL()));
_timer.start(10);
}
virtual void paintGL()
{
frame();
}
protected:
QTimer _timer;
};
int main( int argc, char **argv )
{
QApplication a( argc, argv );
if (argc<2)
{
std::cout << argv[0] <<": requires filename argument." << std::endl;
return 1;
}
// load the scene.
osg::ref_ptr<osg::Node> loadedModel = osgDB::readNodeFile(argv[1]);
if (!loadedModel)
{
std::cout << argv[0] <<": No data loaded." << std::endl;
return 1;
}
ViewerQT* viewerWindow = new ViewerQT;
viewerWindow->setSceneData(loadedModel.get());
viewerWindow->setCameraManipulator(new osgGA::TrackballManipulator);
viewerWindow->addEventHandler(new osgViewer::StatsHandler);
viewerWindow->show();
a.connect( &a, SIGNAL(lastWindowClosed()), &a, SLOT(quit()) );
return a.exec();
}
/*EOF*/