2006-10-02 00:10:33 +08:00
|
|
|
// C++ source file - (C) 2003 Robert Osfield, released under the OSGPL.
|
|
|
|
// (C) 2005 Mike Weiblen http://mew.cx/ released under the OSGPL.
|
|
|
|
// Simple example using GLUT to create an OpenGL window and OSG for rendering.
|
|
|
|
// Derived from osgGLUTsimple.cpp and osgkeyboardmouse.cpp
|
|
|
|
|
2007-06-04 22:46:38 +08:00
|
|
|
#include <osgViewer/Viewer>
|
|
|
|
#include <osgViewer/StatsHandler>
|
2006-10-02 00:10:33 +08:00
|
|
|
#include <osgGA/TrackballManipulator>
|
|
|
|
#include <osgDB/ReadFile>
|
|
|
|
|
|
|
|
#include <FL/Fl.H>
|
|
|
|
#include <FL/Fl_Gl_Window.H>
|
|
|
|
|
|
|
|
#include <iostream>
|
|
|
|
|
2007-06-04 22:46:38 +08:00
|
|
|
class AdapterWidget : public Fl_Gl_Window
|
2006-10-02 00:10:33 +08:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
2007-06-04 22:46:38 +08:00
|
|
|
AdapterWidget(int x, int y, int w, int h, const char *label=0);
|
|
|
|
virtual ~AdapterWidget() {}
|
|
|
|
|
|
|
|
osgViewer::GraphicsWindow* getGraphicsWindow() { return _gw.get(); }
|
|
|
|
const osgViewer::GraphicsWindow* getGraphicsWindow() const { return _gw.get(); }
|
2006-10-02 00:10:33 +08:00
|
|
|
|
|
|
|
virtual void resize(int x, int y, int w, int h);
|
|
|
|
|
|
|
|
protected:
|
|
|
|
|
|
|
|
virtual int handle(int event);
|
2007-06-04 22:46:38 +08:00
|
|
|
|
|
|
|
osg::ref_ptr<osgViewer::GraphicsWindowEmbedded> _gw;
|
2006-10-02 00:10:33 +08:00
|
|
|
};
|
|
|
|
|
2007-06-04 22:46:38 +08:00
|
|
|
AdapterWidget::AdapterWidget(int x, int y, int w, int h, const char *label):
|
2006-10-02 00:10:33 +08:00
|
|
|
Fl_Gl_Window(x, y, w, h, label)
|
|
|
|
{
|
2007-06-04 22:46:38 +08:00
|
|
|
_gw = new osgViewer::GraphicsWindowEmbedded(x,y,w,h);
|
2006-10-02 00:10:33 +08:00
|
|
|
}
|
|
|
|
|
2007-06-04 22:46:38 +08:00
|
|
|
void AdapterWidget::resize(int x, int y, int w, int h)
|
2006-10-02 00:10:33 +08:00
|
|
|
{
|
2007-06-04 22:46:38 +08:00
|
|
|
_gw->getEventQueue()->windowResize(x, y, w, h );
|
|
|
|
_gw->resized(x,y,w,h);
|
|
|
|
|
2006-10-02 00:10:33 +08:00
|
|
|
Fl_Gl_Window::resize(x,y,w,h);
|
2007-06-04 22:46:38 +08:00
|
|
|
|
2006-10-02 00:10:33 +08:00
|
|
|
}
|
|
|
|
|
2007-06-04 22:46:38 +08:00
|
|
|
int AdapterWidget::handle(int event)
|
2006-10-02 00:10:33 +08:00
|
|
|
{
|
|
|
|
switch(event){
|
|
|
|
case FL_PUSH:
|
2007-06-04 22:46:38 +08:00
|
|
|
_gw->getEventQueue()->mouseButtonPress(Fl::event_x(), Fl::event_y(), Fl::event_button());
|
2006-10-02 00:10:33 +08:00
|
|
|
return 1;
|
|
|
|
case FL_MOVE:
|
|
|
|
case FL_DRAG:
|
2007-06-04 22:46:38 +08:00
|
|
|
_gw->getEventQueue()->mouseMotion(Fl::event_x(), Fl::event_y());
|
2006-10-02 00:10:33 +08:00
|
|
|
return 1;
|
|
|
|
case FL_RELEASE:
|
2007-06-04 22:46:38 +08:00
|
|
|
_gw->getEventQueue()->mouseButtonRelease(Fl::event_x(), Fl::event_y(), Fl::event_button());
|
2006-10-02 00:10:33 +08:00
|
|
|
return 1;
|
|
|
|
case FL_KEYDOWN:
|
2007-06-04 22:46:38 +08:00
|
|
|
_gw->getEventQueue()->keyPress((osgGA::GUIEventAdapter::KeySymbol)Fl::event_key());
|
2006-10-02 00:10:33 +08:00
|
|
|
return 1;
|
|
|
|
case FL_KEYUP:
|
2007-06-04 22:46:38 +08:00
|
|
|
_gw->getEventQueue()->keyRelease((osgGA::GUIEventAdapter::KeySymbol)Fl::event_key());
|
2006-10-02 00:10:33 +08:00
|
|
|
return 1;
|
|
|
|
default:
|
|
|
|
// pass other events to the base class
|
|
|
|
return Fl_Gl_Window::handle(event);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void idle_cb()
|
|
|
|
{
|
|
|
|
Fl::redraw();
|
|
|
|
}
|
|
|
|
|
2006-10-15 19:56:52 +08:00
|
|
|
|
2007-06-04 22:46:38 +08:00
|
|
|
class ViewerFLTK : public osgViewer::Viewer, public AdapterWidget
|
2006-10-15 19:56:52 +08:00
|
|
|
{
|
2007-06-04 22:46:38 +08:00
|
|
|
public:
|
|
|
|
ViewerFLTK(int x, int y, int w, int h, const char *label=0):
|
|
|
|
AdapterWidget(x,y,w,h,label)
|
|
|
|
{
|
|
|
|
getCamera()->setViewport(new osg::Viewport(0,0,w,h));
|
|
|
|
getCamera()->setGraphicsContext(getGraphicsWindow());
|
|
|
|
setThreadingModel(osgViewer::Viewer::SingleThreaded);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
virtual void draw() { frame(); }
|
2006-10-15 19:56:52 +08:00
|
|
|
|
|
|
|
};
|
|
|
|
|
2006-10-02 00:10:33 +08:00
|
|
|
int main( int argc, char **argv )
|
|
|
|
{
|
|
|
|
|
|
|
|
if (argc<2)
|
|
|
|
{
|
|
|
|
std::cout << argv[0] <<": requires filename argument." << std::endl;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// load the scene.
|
|
|
|
osg::ref_ptr<osg::Node> loadedModel = osgDB::readNodeFile(argv[1]);
|
|
|
|
if (!loadedModel)
|
|
|
|
{
|
|
|
|
std::cout << argv[0] <<": No data loaded." << std::endl;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-06-04 22:46:38 +08:00
|
|
|
ViewerFLTK viewerWindow(100,100,800,600);
|
2006-10-02 00:10:33 +08:00
|
|
|
viewerWindow.resizable(&viewerWindow);
|
|
|
|
|
|
|
|
viewerWindow.setSceneData(loadedModel.get());
|
|
|
|
viewerWindow.setCameraManipulator(new osgGA::TrackballManipulator);
|
2007-06-04 22:46:38 +08:00
|
|
|
viewerWindow.addEventHandler(new osgViewer::StatsHandler);
|
|
|
|
|
2006-10-02 00:10:33 +08:00
|
|
|
viewerWindow.show();
|
|
|
|
|
|
|
|
Fl::set_idle(idle_cb);
|
|
|
|
|
|
|
|
return Fl::run();
|
|
|
|
}
|