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

This commit is contained in:
Robert Osfield 2007-06-04 14:46:38 +00:00
parent ac739a2e6a
commit f3ec476d9f

View File

@ -3,7 +3,8 @@
// Simple example using GLUT to create an OpenGL window and OSG for rendering. // Simple example using GLUT to create an OpenGL window and OSG for rendering.
// Derived from osgGLUTsimple.cpp and osgkeyboardmouse.cpp // Derived from osgGLUTsimple.cpp and osgkeyboardmouse.cpp
#include <osgViewer/SimpleViewer> #include <osgViewer/Viewer>
#include <osgViewer/StatsHandler>
#include <osgGA/TrackballManipulator> #include <osgGA/TrackballManipulator>
#include <osgDB/ReadFile> #include <osgDB/ReadFile>
@ -12,12 +13,15 @@
#include <iostream> #include <iostream>
class GraphicsWindowFLTK : public Fl_Gl_Window, virtual osgViewer::GraphicsWindow class AdapterWidget : public Fl_Gl_Window
{ {
public: public:
GraphicsWindowFLTK(int x, int y, int w, int h, const char *label=0); AdapterWidget(int x, int y, int w, int h, const char *label=0);
virtual ~GraphicsWindowFLTK() {} virtual ~AdapterWidget() {}
osgViewer::GraphicsWindow* getGraphicsWindow() { return _gw.get(); }
const osgViewer::GraphicsWindow* getGraphicsWindow() const { return _gw.get(); }
virtual void resize(int x, int y, int w, int h); virtual void resize(int x, int y, int w, int h);
@ -25,38 +29,42 @@ protected:
virtual int handle(int event); virtual int handle(int event);
osg::ref_ptr<osgViewer::GraphicsWindowEmbedded> _gw;
}; };
GraphicsWindowFLTK::GraphicsWindowFLTK(int x, int y, int w, int h, const char *label): AdapterWidget::AdapterWidget(int x, int y, int w, int h, const char *label):
Fl_Gl_Window(x, y, w, h, label) Fl_Gl_Window(x, y, w, h, label)
{ {
getEventQueue()->windowResize(x, y, w, h ); _gw = new osgViewer::GraphicsWindowEmbedded(x,y,w,h);
} }
void GraphicsWindowFLTK::resize(int x, int y, int w, int h) void AdapterWidget::resize(int x, int y, int w, int h)
{ {
getEventQueue()->windowResize(x, y, w, h ); _gw->getEventQueue()->windowResize(x, y, w, h );
_gw->resized(x,y,w,h);
Fl_Gl_Window::resize(x,y,w,h); Fl_Gl_Window::resize(x,y,w,h);
} }
int GraphicsWindowFLTK::handle(int event) int AdapterWidget::handle(int event)
{ {
switch(event){ switch(event){
case FL_PUSH: case FL_PUSH:
getEventQueue()->mouseButtonPress(Fl::event_x(), Fl::event_y(), Fl::event_button()); _gw->getEventQueue()->mouseButtonPress(Fl::event_x(), Fl::event_y(), Fl::event_button());
return 1; return 1;
case FL_MOVE: case FL_MOVE:
case FL_DRAG: case FL_DRAG:
getEventQueue()->mouseMotion(Fl::event_x(), Fl::event_y()); _gw->getEventQueue()->mouseMotion(Fl::event_x(), Fl::event_y());
return 1; return 1;
case FL_RELEASE: case FL_RELEASE:
getEventQueue()->mouseButtonRelease(Fl::event_x(), Fl::event_y(), Fl::event_button()); _gw->getEventQueue()->mouseButtonRelease(Fl::event_x(), Fl::event_y(), Fl::event_button());
return 1; return 1;
case FL_KEYDOWN: case FL_KEYDOWN:
getEventQueue()->keyPress((osgGA::GUIEventAdapter::KeySymbol)Fl::event_key()); _gw->getEventQueue()->keyPress((osgGA::GUIEventAdapter::KeySymbol)Fl::event_key());
return 1; return 1;
case FL_KEYUP: case FL_KEYUP:
getEventQueue()->keyRelease((osgGA::GUIEventAdapter::KeySymbol)Fl::event_key()); _gw->getEventQueue()->keyRelease((osgGA::GUIEventAdapter::KeySymbol)Fl::event_key());
return 1; return 1;
default: default:
// pass other events to the base class // pass other events to the base class
@ -70,13 +78,18 @@ void idle_cb()
} }
class SimpleViewerFLTK : public osgViewer::SimpleViewer, public GraphicsWindowFLTK class ViewerFLTK : public osgViewer::Viewer, public AdapterWidget
{ {
public: public:
SimpleViewerFLTK(int x, int y, int w, int h, const char *label=0): ViewerFLTK(int x, int y, int w, int h, const char *label=0):
GraphicsWindowFLTK(x,y,w,h,label) {} AdapterWidget(x,y,w,h,label)
{
getCamera()->setViewport(new osg::Viewport(0,0,w,h));
getCamera()->setGraphicsContext(getGraphicsWindow());
setThreadingModel(osgViewer::Viewer::SingleThreaded);
}
protected: protected:
virtual void draw() { frame(); } virtual void draw() { frame(); }
}; };
@ -99,11 +112,12 @@ int main( int argc, char **argv )
} }
SimpleViewerFLTK viewerWindow(100,100,800,600); ViewerFLTK viewerWindow(100,100,800,600);
viewerWindow.resizable(&viewerWindow); viewerWindow.resizable(&viewerWindow);
viewerWindow.setSceneData(loadedModel.get()); viewerWindow.setSceneData(loadedModel.get());
viewerWindow.setCameraManipulator(new osgGA::TrackballManipulator); viewerWindow.setCameraManipulator(new osgGA::TrackballManipulator);
viewerWindow.addEventHandler(new osgViewer::StatsHandler);
viewerWindow.show(); viewerWindow.show();