root/OpenSceneGraph/trunk/examples/osgviewerQT/osgviewerQT.cpp @ 7019

Revision 7019, 7.9 kB (checked in by robert, 8 years ago)

Added setting of the Camera's projection matrix to fit the windowing aspect ratio.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1/* OpenSceneGraph example, osganimate.
2*
3*  Permission is hereby granted, free of charge, to any person obtaining a copy
4*  of this software and associated documentation files (the "Software"), to deal
5*  in the Software without restriction, including without limitation the rights
6*  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7*  copies of the Software, and to permit persons to whom the Software is
8*  furnished to do so, subject to the following conditions:
9*
10*  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
11*  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
12*  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
13*  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
14*  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
15*  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
16*  THE SOFTWARE.
17*/
18
19#include <osgViewer/Viewer>
20#include <osgViewer/CompositeViewer>
21#include <osgViewer/ViewerEventHandlers>
22#include <osgGA/TrackballManipulator>
23#include <osgDB/ReadFile>
24
25#if USE_QT4
26
27    #include <QtCore/QString>
28    #include <QtCore/QTimer>
29    #include <QtGui/QKeyEvent>
30    #include <QtGui/QApplication>
31    #include <QtOpenGL/QGLWidget>
32   
33    using Qt::WindowFlags;
34
35#else
36
37    class QWidget;
38    #include <qtimer.h>
39    #include <qgl.h>
40    #include <qapplication.h>
41
42    #define WindowFlags WFlags
43
44#endif
45
46#include <iostream>
47
48class AdapterWidget : public QGLWidget
49{
50    public:
51
52        AdapterWidget( QWidget * parent = 0, const char * name = 0, const QGLWidget * shareWidget = 0, WindowFlags f = 0 );
53
54        virtual ~AdapterWidget() {}
55
56        osgViewer::GraphicsWindow* getGraphicsWindow() { return _gw.get(); }
57        const osgViewer::GraphicsWindow* getGraphicsWindow() const { return _gw.get(); }
58
59    protected:
60
61        void init();
62
63        virtual void resizeGL( int width, int height );
64        virtual void keyPressEvent( QKeyEvent* event );
65        virtual void keyReleaseEvent( QKeyEvent* event );
66        virtual void mousePressEvent( QMouseEvent* event );
67        virtual void mouseReleaseEvent( QMouseEvent* event );
68        virtual void mouseMoveEvent( QMouseEvent* event );
69
70        osg::ref_ptr<osgViewer::GraphicsWindowEmbedded> _gw;
71};
72
73AdapterWidget::AdapterWidget( QWidget * parent, const char * name, const QGLWidget * shareWidget, WindowFlags f):
74#if USE_QT4
75    QGLWidget(parent, shareWidget, f)
76#else
77    QGLWidget(parent, name, shareWidget, f)
78#endif
79{
80    _gw = new osgViewer::GraphicsWindowEmbedded(0,0,width(),height());
81}
82
83void AdapterWidget::resizeGL( int width, int height )
84{
85    _gw->getEventQueue()->windowResize(0, 0, width, height );
86    _gw->resized(0,0,width,height);
87}
88
89void AdapterWidget::keyPressEvent( QKeyEvent* event )
90{
91#if USE_QT4
92    _gw->getEventQueue()->keyPress( (osgGA::GUIEventAdapter::KeySymbol) *(event->text().toAscii().data() ) );
93#else
94    _gw->getEventQueue()->keyPress( (osgGA::GUIEventAdapter::KeySymbol) event->ascii() );
95#endif
96}
97
98void AdapterWidget::keyReleaseEvent( QKeyEvent* event )
99{
100#if USE_QT4
101    _gw->getEventQueue()->keyRelease( (osgGA::GUIEventAdapter::KeySymbol) *(event->text().toAscii().data() ) );
102#else
103    _gw->getEventQueue()->keyRelease( (osgGA::GUIEventAdapter::KeySymbol) event->ascii() );
104#endif
105}
106
107void AdapterWidget::mousePressEvent( QMouseEvent* event )
108{
109    int button = 0;
110    switch(event->button())
111    {
112        case(Qt::LeftButton): button = 1; break;
113        case(Qt::MidButton): button = 2; break;
114        case(Qt::RightButton): button = 3; break;
115        case(Qt::NoButton): button = 0; break;
116        default: button = 0; break;
117    }
118    _gw->getEventQueue()->mouseButtonPress(event->x(), event->y(), button);
119}
120
121void AdapterWidget::mouseReleaseEvent( QMouseEvent* event )
122{
123    int button = 0;
124    switch(event->button())
125    {
126        case(Qt::LeftButton): button = 1; break;
127        case(Qt::MidButton): button = 2; break;
128        case(Qt::RightButton): button = 3; break;
129        case(Qt::NoButton): button = 0; break;
130        default: button = 0; break;
131    }
132    _gw->getEventQueue()->mouseButtonRelease(event->x(), event->y(), button);
133}
134
135void AdapterWidget::mouseMoveEvent( QMouseEvent* event )
136{
137    _gw->getEventQueue()->mouseMotion(event->x(), event->y());
138}
139
140
141class ViewerQT : public osgViewer::Viewer, public AdapterWidget
142{
143    public:
144
145        ViewerQT(QWidget * parent = 0, const char * name = 0, const QGLWidget * shareWidget = 0, WindowFlags f = 0):
146            AdapterWidget( parent, name, shareWidget, f )
147        {
148            getCamera()->setViewport(new osg::Viewport(0,0,width(),height()));
149            getCamera()->setGraphicsContext(getGraphicsWindow());
150            getCamera()->setProjectionMatrixAsPerspective(30.0f, static_cast<double>(width())/static_cast<double>(height()), 1.0f, 10000.0f);
151
152            setThreadingModel(osgViewer::Viewer::SingleThreaded);
153
154            connect(&_timer, SIGNAL(timeout()), this, SLOT(updateGL()));
155            _timer.start(10);
156        }
157
158        virtual void paintGL()
159        {
160            frame();
161        }
162   
163    protected:
164
165        QTimer _timer;
166};
167
168class CompositeViewerQT : public osgViewer::CompositeViewer, public AdapterWidget
169{
170    public:
171
172        CompositeViewerQT(QWidget * parent = 0, const char * name = 0, const QGLWidget * shareWidget = 0, WindowFlags f = 0):
173            AdapterWidget( parent, name, shareWidget, f )
174        {
175            setThreadingModel(osgViewer::CompositeViewer::SingleThreaded);
176
177            connect(&_timer, SIGNAL(timeout()), this, SLOT(updateGL()));
178            _timer.start(10);
179        }
180
181        virtual void paintGL()
182        {
183            frame();
184        }
185   
186    protected:
187
188        QTimer _timer;
189};
190
191int main( int argc, char **argv )
192{
193    QApplication a( argc, argv );
194   
195    if (argc<2)
196    {
197        std::cout << argv[0] <<": requires filename argument." << std::endl;
198        return 1;
199    }
200
201    osg::ArgumentParser arguments(&argc, argv);
202
203    // load the scene.
204    osg::ref_ptr<osg::Node> loadedModel = osgDB::readNodeFiles(arguments);
205    if (!loadedModel)
206    {
207        std::cout << argv[0] <<": No data loaded." << std::endl;
208        return 1;
209    }
210   
211   
212    if (arguments.read("--CompositeViewer"))
213    {
214        CompositeViewerQT* viewerWindow = new CompositeViewerQT;
215
216        unsigned int width = viewerWindow->width();
217        unsigned int height = viewerWindow->height();
218       
219        {
220            osgViewer::View* view1 = new osgViewer::View;
221            view1->getCamera()->setGraphicsContext(viewerWindow->getGraphicsWindow());
222            view1->getCamera()->setProjectionMatrixAsPerspective(30.0f, static_cast<double>(width)/static_cast<double>(height/2), 1.0, 1000.0);
223            view1->getCamera()->setViewport(new osg::Viewport(0,0,width,height/2));
224            view1->setCameraManipulator(new osgGA::TrackballManipulator);
225            view1->setSceneData(loadedModel.get());
226           
227            viewerWindow->addView(view1);
228        }
229       
230        {
231            osgViewer::View* view2 = new osgViewer::View;
232            view2->getCamera()->setGraphicsContext(viewerWindow->getGraphicsWindow());
233            view2->getCamera()->setProjectionMatrixAsPerspective(30.0f, static_cast<double>(width)/static_cast<double>(height/2), 1.0, 1000.0);
234            view2->getCamera()->setViewport(new osg::Viewport(0,height/2,width,height/2));
235            view2->setCameraManipulator(new osgGA::TrackballManipulator);
236            view2->setSceneData(loadedModel.get());
237           
238            viewerWindow->addView(view2);
239        }
240
241        viewerWindow->show();
242    }
243    else
244    {
245        ViewerQT* viewerWindow = new ViewerQT;
246
247        viewerWindow->setCameraManipulator(new osgGA::TrackballManipulator);
248        viewerWindow->setSceneData(loadedModel.get());
249
250        viewerWindow->show();
251    }   
252   
253   
254    a.connect( &a, SIGNAL(lastWindowClosed()), &a, SLOT(quit()) );
255   
256    return a.exec();
257}
258
259/*EOF*/
Note: See TracBrowser for help on using the browser.