root/OpenSceneGraph/trunk/examples/osgviewerFLTK/osgsimpleviewerFLTK.cpp @ 6812

Revision 5679, 2.9 kB (checked in by robert, 7 years ago)

Moved SimpleViewer? and GraphicsWindow? into their own osgViewer library, updated simpleviewer examples to reflect this change

  • 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/SimpleViewer>
7#include <osgGA/TrackballManipulator>
8#include <osgDB/ReadFile>
9
10#include <FL/Fl.H>
11#include <FL/Fl_Gl_Window.H>
12
13#include <iostream>
14
15class GraphicsWindowFLTK : public Fl_Gl_Window,  virtual osgViewer::GraphicsWindow
16{
17public:
18
19    GraphicsWindowFLTK(int x, int y, int w, int h, const char *label=0);
20    virtual ~GraphicsWindowFLTK() {}
21
22    virtual void resize(int x, int y, int w, int h);
23
24protected:
25
26    virtual int handle(int event);
27
28};
29
30GraphicsWindowFLTK::GraphicsWindowFLTK(int x, int y, int w, int h, const char *label):
31    Fl_Gl_Window(x, y, w, h, label)
32{
33    getEventQueue()->windowResize(x, y, w, h );
34}
35
36void GraphicsWindowFLTK::resize(int x, int y, int w, int h)
37{
38    getEventQueue()->windowResize(x, y, w, h );
39    Fl_Gl_Window::resize(x,y,w,h);
40}
41
42int GraphicsWindowFLTK::handle(int event)
43{
44    switch(event){
45        case FL_PUSH:
46            getEventQueue()->mouseButtonPress(Fl::event_x(), Fl::event_y(), Fl::event_button());
47            return 1;
48        case FL_MOVE:
49        case FL_DRAG:
50            getEventQueue()->mouseMotion(Fl::event_x(), Fl::event_y());
51            return 1;
52        case FL_RELEASE:
53            getEventQueue()->mouseButtonRelease(Fl::event_x(), Fl::event_y(), Fl::event_button());
54            return 1;
55        case FL_KEYDOWN:
56            getEventQueue()->keyPress((osgGA::GUIEventAdapter::KeySymbol)Fl::event_key());
57            return 1;
58        case FL_KEYUP:
59            getEventQueue()->keyRelease((osgGA::GUIEventAdapter::KeySymbol)Fl::event_key());
60            return 1;
61        default:
62            // pass other events to the base class
63            return Fl_Gl_Window::handle(event);
64    }
65}
66
67void idle_cb()
68{
69    Fl::redraw();
70}
71
72
73class SimpleViewerFLTK : public osgViewer::SimpleViewer, public GraphicsWindowFLTK
74{
75public:
76    SimpleViewerFLTK(int x, int y, int w, int h, const char *label=0):
77         GraphicsWindowFLTK(x,y,w,h,label) {}
78
79protected:
80    virtual void draw() { frame(); }
81
82};
83
84int main( int argc, char **argv )
85{
86   
87    if (argc<2)
88    {
89        std::cout << argv[0] <<": requires filename argument." << std::endl;
90        return 1;
91    }
92
93    // load the scene.
94    osg::ref_ptr<osg::Node> loadedModel = osgDB::readNodeFile(argv[1]);
95    if (!loadedModel)
96    {
97        std::cout << argv[0] <<": No data loaded." << std::endl;
98        return 1;
99    }
100
101
102    SimpleViewerFLTK viewerWindow(100,100,800,600);
103    viewerWindow.resizable(&viewerWindow);
104
105    viewerWindow.setSceneData(loadedModel.get());
106    viewerWindow.setCameraManipulator(new osgGA::TrackballManipulator);
107
108    viewerWindow.show();
109   
110    Fl::set_idle(idle_cb);
111   
112    return Fl::run();
113}
Note: See TracBrowser for help on using the browser.