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

Revision 6829, 3.4 kB (checked in by robert, 7 years ago)

From Martin Lavery and Robert Osfield, ported fltk example to osgViewer::Viewer

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1// C++ source file - (C) 2003 Robert Osfield, released under the OSGPL.
2// (C) 2005 Mike Weiblen http://mew.cx/ released under the OSGPL.
3// Simple example using GLUT to create an OpenGL window and OSG for rendering.
4// Derived from osgGLUTsimple.cpp and osgkeyboardmouse.cpp
5
6#include <osgViewer/Viewer>
7#include <osgViewer/StatsHandler>
8#include <osgGA/TrackballManipulator>
9#include <osgDB/ReadFile>
10
11#include <FL/Fl.H>
12#include <FL/Fl_Gl_Window.H>
13
14#include <iostream>
15
16class AdapterWidget : public Fl_Gl_Window
17{
18public:
19
20    AdapterWidget(int x, int y, int w, int h, const char *label=0);
21    virtual ~AdapterWidget() {}
22
23    osgViewer::GraphicsWindow* getGraphicsWindow() { return _gw.get(); }
24    const osgViewer::GraphicsWindow* getGraphicsWindow() const { return _gw.get(); }
25
26    virtual void resize(int x, int y, int w, int h);
27
28protected:
29
30    virtual int handle(int event);
31   
32    osg::ref_ptr<osgViewer::GraphicsWindowEmbedded> _gw;
33};
34
35AdapterWidget::AdapterWidget(int x, int y, int w, int h, const char *label):
36    Fl_Gl_Window(x, y, w, h, label)
37{
38    _gw = new osgViewer::GraphicsWindowEmbedded(x,y,w,h);
39}
40
41void AdapterWidget::resize(int x, int y, int w, int h)
42{
43    _gw->getEventQueue()->windowResize(x, y, w, h );
44    _gw->resized(x,y,w,h);
45
46    Fl_Gl_Window::resize(x,y,w,h);
47
48}
49
50int AdapterWidget::handle(int event)
51{
52    switch(event){
53        case FL_PUSH:
54            _gw->getEventQueue()->mouseButtonPress(Fl::event_x(), Fl::event_y(), Fl::event_button());
55            return 1;
56        case FL_MOVE:
57        case FL_DRAG:
58            _gw->getEventQueue()->mouseMotion(Fl::event_x(), Fl::event_y());
59            return 1;
60        case FL_RELEASE:
61            _gw->getEventQueue()->mouseButtonRelease(Fl::event_x(), Fl::event_y(), Fl::event_button());
62            return 1;
63        case FL_KEYDOWN:
64            _gw->getEventQueue()->keyPress((osgGA::GUIEventAdapter::KeySymbol)Fl::event_key());
65            return 1;
66        case FL_KEYUP:
67            _gw->getEventQueue()->keyRelease((osgGA::GUIEventAdapter::KeySymbol)Fl::event_key());
68            return 1;
69        default:
70            // pass other events to the base class
71            return Fl_Gl_Window::handle(event);
72    }
73}
74
75void idle_cb()
76{
77    Fl::redraw();
78}
79
80
81class ViewerFLTK : public osgViewer::Viewer, public AdapterWidget
82{
83    public:
84        ViewerFLTK(int x, int y, int w, int h, const char *label=0):
85            AdapterWidget(x,y,w,h,label)
86            {
87                getCamera()->setViewport(new osg::Viewport(0,0,w,h));
88                getCamera()->setGraphicsContext(getGraphicsWindow());
89                setThreadingModel(osgViewer::Viewer::SingleThreaded);
90            }
91
92    protected:
93        virtual void draw() { frame(); }
94
95};
96
97int main( int argc, char **argv )
98{
99   
100    if (argc<2)
101    {
102        std::cout << argv[0] <<": requires filename argument." << std::endl;
103        return 1;
104    }
105
106    // load the scene.
107    osg::ref_ptr<osg::Node> loadedModel = osgDB::readNodeFile(argv[1]);
108    if (!loadedModel)
109    {
110        std::cout << argv[0] <<": No data loaded." << std::endl;
111        return 1;
112    }
113
114
115    ViewerFLTK viewerWindow(100,100,800,600);
116    viewerWindow.resizable(&viewerWindow);
117
118    viewerWindow.setSceneData(loadedModel.get());
119    viewerWindow.setCameraManipulator(new osgGA::TrackballManipulator);
120    viewerWindow.addEventHandler(new osgViewer::StatsHandler);
121   
122    viewerWindow.show();
123   
124    Fl::set_idle(idle_cb);
125   
126    return Fl::run();
127}
Note: See TracBrowser for help on using the browser.