root/OpenSceneGraph/trunk/examples/osgviewerFLTK/osgviewerFLTK.cpp @ 13223

Revision 7019, 6.3 kB (checked in by robert, 7 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*  C++ source file - (C) 2003 Robert Osfield.
4*  (C) 2005 Mike Weiblen http://mew.cx/
5*
6*  Permission is hereby granted, free of charge, to any person obtaining a copy
7*  of this software and associated documentation files (the "Software"), to deal
8*  in the Software without restriction, including without limitation the rights
9*  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10*  copies of the Software, and to permit persons to whom the Software is
11*  furnished to do so, subject to the following conditions:
12*
13*  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14*  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15*  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16*  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17*  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18*  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19*  THE SOFTWARE.
20*/
21
22
23#include <osgViewer/Viewer>
24#include <osgViewer/CompositeViewer>
25#include <osgViewer/ViewerEventHandlers>
26#include <osgGA/TrackballManipulator>
27#include <osgDB/ReadFile>
28
29#include <FL/Fl.H>
30#include <FL/Fl_Gl_Window.H>
31
32#include <iostream>
33
34class AdapterWidget : public Fl_Gl_Window
35{
36public:
37
38    AdapterWidget(int x, int y, int w, int h, const char *label=0);
39    virtual ~AdapterWidget() {}
40
41    osgViewer::GraphicsWindow* getGraphicsWindow() { return _gw.get(); }
42    const osgViewer::GraphicsWindow* getGraphicsWindow() const { return _gw.get(); }
43
44    virtual void resize(int x, int y, int w, int h);
45
46protected:
47
48    virtual int handle(int event);
49   
50    osg::ref_ptr<osgViewer::GraphicsWindowEmbedded> _gw;
51};
52
53AdapterWidget::AdapterWidget(int x, int y, int w, int h, const char *label):
54    Fl_Gl_Window(x, y, w, h, label)
55{
56    _gw = new osgViewer::GraphicsWindowEmbedded(x,y,w,h);
57}
58
59void AdapterWidget::resize(int x, int y, int w, int h)
60{
61    _gw->getEventQueue()->windowResize(x, y, w, h );
62    _gw->resized(x,y,w,h);
63
64    Fl_Gl_Window::resize(x,y,w,h);
65
66}
67
68int AdapterWidget::handle(int event)
69{
70    switch(event){
71        case FL_PUSH:
72            _gw->getEventQueue()->mouseButtonPress(Fl::event_x(), Fl::event_y(), Fl::event_button());
73            return 1;
74        case FL_MOVE:
75        case FL_DRAG:
76            _gw->getEventQueue()->mouseMotion(Fl::event_x(), Fl::event_y());
77            return 1;
78        case FL_RELEASE:
79            _gw->getEventQueue()->mouseButtonRelease(Fl::event_x(), Fl::event_y(), Fl::event_button());
80            return 1;
81        case FL_KEYDOWN:
82            _gw->getEventQueue()->keyPress((osgGA::GUIEventAdapter::KeySymbol)Fl::event_key());
83            return 1;
84        case FL_KEYUP:
85            _gw->getEventQueue()->keyRelease((osgGA::GUIEventAdapter::KeySymbol)Fl::event_key());
86            return 1;
87        default:
88            // pass other events to the base class
89            return Fl_Gl_Window::handle(event);
90    }
91}
92
93void idle_cb()
94{
95    Fl::redraw();
96}
97
98class ViewerFLTK : public osgViewer::Viewer, public AdapterWidget
99{
100    public:
101        ViewerFLTK(int x, int y, int w, int h, const char *label=0):
102            AdapterWidget(x,y,w,h,label)
103            {
104                getCamera()->setViewport(new osg::Viewport(0,0,w,h));
105                getCamera()->setProjectionMatrixAsPerspective(30.0f, static_cast<double>(w)/static_cast<double>(h), 1.0f, 10000.0f);
106                getCamera()->setGraphicsContext(getGraphicsWindow());
107                setThreadingModel(osgViewer::Viewer::SingleThreaded);
108            }
109
110    protected:
111        virtual void draw() { frame(); }
112
113};
114
115class CompositeViewerFLTK : public osgViewer::CompositeViewer, public AdapterWidget
116{
117    public:
118
119        CompositeViewerFLTK(int x, int y, int w, int h, const char *label=0):
120            AdapterWidget(x,y,w,h,label)
121        {
122            setThreadingModel(osgViewer::CompositeViewer::SingleThreaded);
123        }
124
125    protected:
126        virtual void draw() { frame(); }
127
128};
129
130
131int main( int argc, char **argv )
132{
133   
134    if (argc<2)
135    {
136        std::cout << argv[0] <<": requires filename argument." << std::endl;
137        return 1;
138    }
139
140    osg::ArgumentParser arguments(&argc, argv);
141
142    // load the scene.
143    osg::ref_ptr<osg::Node> loadedModel = osgDB::readNodeFiles(arguments);
144    if (!loadedModel)
145    {
146        std::cout << argv[0] <<": No data loaded." << std::endl;
147        return 1;
148    }
149
150
151    if (arguments.read("--CompositeViewer"))
152    {
153        unsigned int width = 1024;
154        unsigned int height = 800;
155
156        CompositeViewerFLTK viewerWindow(100,100,width,height);
157        viewerWindow.resizable(&viewerWindow);
158       
159        {
160            osgViewer::View* view1 = new osgViewer::View;
161            view1->getCamera()->setGraphicsContext(viewerWindow.getGraphicsWindow());
162            view1->getCamera()->setProjectionMatrixAsPerspective(30.0f, static_cast<double>(width)/static_cast<double>(height/2), 1.0, 1000.0);
163            view1->getCamera()->setViewport(new osg::Viewport(0,0,width,height/2));
164            view1->setCameraManipulator(new osgGA::TrackballManipulator);
165            view1->setSceneData(loadedModel.get());
166           
167            viewerWindow.addView(view1);
168        }
169       
170        {
171            osgViewer::View* view2 = new osgViewer::View;
172            view2->getCamera()->setGraphicsContext(viewerWindow.getGraphicsWindow());
173            view2->getCamera()->setProjectionMatrixAsPerspective(30.0f, static_cast<double>(width)/static_cast<double>(height/2), 1.0, 1000.0);
174            view2->getCamera()->setViewport(new osg::Viewport(0,height/2,width,height/2));
175            view2->setCameraManipulator(new osgGA::TrackballManipulator);
176            view2->setSceneData(loadedModel.get());
177           
178            viewerWindow.addView(view2);
179        }
180
181        viewerWindow.show();
182
183        Fl::set_idle(idle_cb);
184
185        return Fl::run();
186    }
187    else
188    {
189
190        ViewerFLTK viewerWindow(100,100,800,600);
191        viewerWindow.resizable(&viewerWindow);
192
193        viewerWindow.setSceneData(loadedModel.get());
194        viewerWindow.setCameraManipulator(new osgGA::TrackballManipulator);
195        viewerWindow.addEventHandler(new osgViewer::StatsHandler);
196
197        viewerWindow.show();
198
199        Fl::set_idle(idle_cb);
200
201        return Fl::run();
202    }
203}
Note: See TracBrowser for help on using the browser.