2007-06-12 22:20:16 +08:00
|
|
|
/* OpenSceneGraph example, osganimate.
|
|
|
|
*
|
|
|
|
* C++ source file - (C) 2003 Robert Osfield.
|
|
|
|
* (C) 2005 Mike Weiblen http://mew.cx/
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
2006-10-02 00:10:33 +08:00
|
|
|
|
2007-06-04 22:46:38 +08:00
|
|
|
#include <osgViewer/Viewer>
|
2007-06-06 03:40:07 +08:00
|
|
|
#include <osgViewer/CompositeViewer>
|
2007-06-07 00:23:20 +08:00
|
|
|
#include <osgViewer/ViewerEventHandlers>
|
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();
|
|
|
|
}
|
|
|
|
|
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));
|
2007-06-23 19:21:54 +08:00
|
|
|
getCamera()->setProjectionMatrixAsPerspective(30.0f, static_cast<double>(w)/static_cast<double>(h), 1.0f, 10000.0f);
|
2007-06-04 22:46:38 +08:00
|
|
|
getCamera()->setGraphicsContext(getGraphicsWindow());
|
2018-03-07 17:37:11 +08:00
|
|
|
|
|
|
|
// set the draw and read buffers up for a double buffered window with rendering going to back buffer
|
|
|
|
getCamera()->setDrawBuffer(GL_BACK);
|
|
|
|
getCamera()->setReadBuffer(GL_BACK);
|
|
|
|
|
2007-06-04 22:46:38 +08:00
|
|
|
setThreadingModel(osgViewer::Viewer::SingleThreaded);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
virtual void draw() { frame(); }
|
2006-10-15 19:56:52 +08:00
|
|
|
|
|
|
|
};
|
|
|
|
|
2007-06-06 03:40:07 +08:00
|
|
|
class CompositeViewerFLTK : public osgViewer::CompositeViewer, public AdapterWidget
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
|
|
|
CompositeViewerFLTK(int x, int y, int w, int h, const char *label=0):
|
|
|
|
AdapterWidget(x,y,w,h,label)
|
|
|
|
{
|
|
|
|
setThreadingModel(osgViewer::CompositeViewer::SingleThreaded);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
virtual void draw() { frame(); }
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2007-06-06 03:40:07 +08:00
|
|
|
osg::ArgumentParser arguments(&argc, argv);
|
|
|
|
|
2006-10-02 00:10:33 +08:00
|
|
|
// load the scene.
|
2007-06-06 03:40:07 +08:00
|
|
|
osg::ref_ptr<osg::Node> loadedModel = osgDB::readNodeFiles(arguments);
|
2006-10-02 00:10:33 +08:00
|
|
|
if (!loadedModel)
|
|
|
|
{
|
|
|
|
std::cout << argv[0] <<": No data loaded." << std::endl;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-06-06 03:40:07 +08:00
|
|
|
if (arguments.read("--CompositeViewer"))
|
|
|
|
{
|
|
|
|
unsigned int width = 1024;
|
|
|
|
unsigned int height = 800;
|
|
|
|
|
|
|
|
CompositeViewerFLTK viewerWindow(100,100,width,height);
|
|
|
|
viewerWindow.resizable(&viewerWindow);
|
|
|
|
|
|
|
|
{
|
|
|
|
osgViewer::View* view1 = new osgViewer::View;
|
|
|
|
view1->getCamera()->setGraphicsContext(viewerWindow.getGraphicsWindow());
|
|
|
|
view1->getCamera()->setProjectionMatrixAsPerspective(30.0f, static_cast<double>(width)/static_cast<double>(height/2), 1.0, 1000.0);
|
|
|
|
view1->getCamera()->setViewport(new osg::Viewport(0,0,width,height/2));
|
2018-03-07 17:37:11 +08:00
|
|
|
|
|
|
|
// set the draw and read buffers up for a double buffered window with rendering going to back buffer
|
|
|
|
view1->getCamera()->setDrawBuffer(GL_BACK);
|
|
|
|
view1->getCamera()->setReadBuffer(GL_BACK);
|
|
|
|
|
2007-06-06 03:40:07 +08:00
|
|
|
view1->setCameraManipulator(new osgGA::TrackballManipulator);
|
|
|
|
view1->setSceneData(loadedModel.get());
|
|
|
|
|
|
|
|
viewerWindow.addView(view1);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
osgViewer::View* view2 = new osgViewer::View;
|
|
|
|
view2->getCamera()->setGraphicsContext(viewerWindow.getGraphicsWindow());
|
|
|
|
view2->getCamera()->setProjectionMatrixAsPerspective(30.0f, static_cast<double>(width)/static_cast<double>(height/2), 1.0, 1000.0);
|
|
|
|
view2->getCamera()->setViewport(new osg::Viewport(0,height/2,width,height/2));
|
2018-03-07 17:37:11 +08:00
|
|
|
|
|
|
|
// set the draw and read buffers up for a double buffered window with rendering going to back buffer
|
|
|
|
view2->getCamera()->setDrawBuffer(GL_BACK);
|
|
|
|
view2->getCamera()->setReadBuffer(GL_BACK);
|
|
|
|
|
2007-06-06 03:40:07 +08:00
|
|
|
view2->setCameraManipulator(new osgGA::TrackballManipulator);
|
|
|
|
view2->setSceneData(loadedModel.get());
|
|
|
|
|
|
|
|
viewerWindow.addView(view2);
|
|
|
|
}
|
|
|
|
|
|
|
|
viewerWindow.show();
|
|
|
|
|
|
|
|
Fl::set_idle(idle_cb);
|
|
|
|
|
|
|
|
return Fl::run();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2006-10-02 00:10:33 +08:00
|
|
|
|
2007-06-06 03:40:07 +08:00
|
|
|
ViewerFLTK viewerWindow(100,100,800,600);
|
|
|
|
viewerWindow.resizable(&viewerWindow);
|
|
|
|
|
|
|
|
viewerWindow.setSceneData(loadedModel.get());
|
|
|
|
viewerWindow.setCameraManipulator(new osgGA::TrackballManipulator);
|
|
|
|
viewerWindow.addEventHandler(new osgViewer::StatsHandler);
|
|
|
|
|
|
|
|
viewerWindow.show();
|
|
|
|
|
|
|
|
Fl::set_idle(idle_cb);
|
|
|
|
|
|
|
|
return Fl::run();
|
|
|
|
}
|
2006-10-02 00:10:33 +08:00
|
|
|
}
|