2007-06-12 22:20:16 +08:00
|
|
|
/* OpenSceneGraph example, osganimate.
|
|
|
|
*
|
|
|
|
* 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-01 00:26:53 +08:00
|
|
|
|
2007-06-03 00:01:56 +08:00
|
|
|
#include <osgViewer/Viewer>
|
2007-06-06 03:10:18 +08:00
|
|
|
#include <osgViewer/CompositeViewer>
|
2007-06-07 00:23:20 +08:00
|
|
|
#include <osgViewer/ViewerEventHandlers>
|
2006-10-01 00:26:53 +08:00
|
|
|
#include <osgGA/TrackballManipulator>
|
|
|
|
#include <osgDB/ReadFile>
|
|
|
|
|
2007-06-03 17:09:09 +08:00
|
|
|
#if USE_QT4
|
2007-06-03 00:01:56 +08:00
|
|
|
|
2007-06-12 18:19:27 +08:00
|
|
|
#include <QtCore/QString>
|
2007-06-03 00:01:56 +08:00
|
|
|
#include <QtCore/QTimer>
|
|
|
|
#include <QtGui/QKeyEvent>
|
|
|
|
#include <QtGui/QApplication>
|
|
|
|
#include <QtOpenGL/QGLWidget>
|
2007-06-12 18:19:27 +08:00
|
|
|
|
|
|
|
using Qt::WindowFlags;
|
2007-06-03 17:09:09 +08:00
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
class QWidget;
|
|
|
|
#include <qtimer.h>
|
|
|
|
#include <qgl.h>
|
|
|
|
#include <qapplication.h>
|
|
|
|
|
2007-06-12 18:19:27 +08:00
|
|
|
#define WindowFlags WFlags
|
|
|
|
|
2007-06-03 00:01:56 +08:00
|
|
|
#endif
|
2006-10-01 00:26:53 +08:00
|
|
|
|
|
|
|
#include <iostream>
|
|
|
|
|
2007-06-03 00:01:56 +08:00
|
|
|
class AdapterWidget : public QGLWidget
|
2006-10-01 00:26:53 +08:00
|
|
|
{
|
2007-06-04 22:47:16 +08:00
|
|
|
public:
|
2006-10-01 00:26:53 +08:00
|
|
|
|
2007-06-12 18:19:27 +08:00
|
|
|
AdapterWidget( QWidget * parent = 0, const char * name = 0, const QGLWidget * shareWidget = 0, WindowFlags f = 0 );
|
2007-06-03 00:01:56 +08:00
|
|
|
|
2007-06-04 22:47:16 +08:00
|
|
|
virtual ~AdapterWidget() {}
|
2006-10-01 00:26:53 +08:00
|
|
|
|
2007-06-04 22:47:16 +08:00
|
|
|
osgViewer::GraphicsWindow* getGraphicsWindow() { return _gw.get(); }
|
|
|
|
const osgViewer::GraphicsWindow* getGraphicsWindow() const { return _gw.get(); }
|
2006-10-01 00:26:53 +08:00
|
|
|
|
2007-06-04 22:47:16 +08:00
|
|
|
protected:
|
2007-06-03 00:01:56 +08:00
|
|
|
|
2007-06-04 22:47:16 +08:00
|
|
|
void init();
|
2006-10-01 00:26:53 +08:00
|
|
|
|
2007-06-04 22:47:16 +08:00
|
|
|
virtual void resizeGL( int width, int height );
|
|
|
|
virtual void keyPressEvent( QKeyEvent* event );
|
|
|
|
virtual void keyReleaseEvent( QKeyEvent* event );
|
|
|
|
virtual void mousePressEvent( QMouseEvent* event );
|
|
|
|
virtual void mouseReleaseEvent( QMouseEvent* event );
|
|
|
|
virtual void mouseMoveEvent( QMouseEvent* event );
|
|
|
|
|
|
|
|
osg::ref_ptr<osgViewer::GraphicsWindowEmbedded> _gw;
|
2006-10-01 00:26:53 +08:00
|
|
|
};
|
|
|
|
|
2007-06-12 18:19:27 +08:00
|
|
|
AdapterWidget::AdapterWidget( QWidget * parent, const char * name, const QGLWidget * shareWidget, WindowFlags f):
|
|
|
|
#if USE_QT4
|
|
|
|
QGLWidget(parent, shareWidget, f)
|
|
|
|
#else
|
2006-10-01 00:26:53 +08:00
|
|
|
QGLWidget(parent, name, shareWidget, f)
|
2007-06-12 18:19:27 +08:00
|
|
|
#endif
|
2006-10-01 00:26:53 +08:00
|
|
|
{
|
2007-06-03 00:01:56 +08:00
|
|
|
_gw = new osgViewer::GraphicsWindowEmbedded(0,0,width(),height());
|
2006-10-01 00:26:53 +08:00
|
|
|
}
|
|
|
|
|
2007-06-03 00:01:56 +08:00
|
|
|
void AdapterWidget::resizeGL( int width, int height )
|
2006-10-01 00:26:53 +08:00
|
|
|
{
|
2007-06-03 00:01:56 +08:00
|
|
|
_gw->getEventQueue()->windowResize(0, 0, width, height );
|
|
|
|
_gw->resized(0,0,width,height);
|
2006-10-01 00:26:53 +08:00
|
|
|
}
|
|
|
|
|
2007-06-03 00:01:56 +08:00
|
|
|
void AdapterWidget::keyPressEvent( QKeyEvent* event )
|
2006-10-01 00:26:53 +08:00
|
|
|
{
|
2007-06-12 23:57:26 +08:00
|
|
|
#if USE_QT4
|
|
|
|
_gw->getEventQueue()->keyPress( (osgGA::GUIEventAdapter::KeySymbol) *(event->text().toAscii().data() ) );
|
|
|
|
#else
|
2007-06-03 00:01:56 +08:00
|
|
|
_gw->getEventQueue()->keyPress( (osgGA::GUIEventAdapter::KeySymbol) event->ascii() );
|
2007-06-12 23:57:26 +08:00
|
|
|
#endif
|
2006-10-01 00:26:53 +08:00
|
|
|
}
|
|
|
|
|
2007-06-03 00:01:56 +08:00
|
|
|
void AdapterWidget::keyReleaseEvent( QKeyEvent* event )
|
2006-10-01 00:26:53 +08:00
|
|
|
{
|
2007-06-12 23:57:26 +08:00
|
|
|
#if USE_QT4
|
|
|
|
_gw->getEventQueue()->keyRelease( (osgGA::GUIEventAdapter::KeySymbol) *(event->text().toAscii().data() ) );
|
|
|
|
#else
|
2007-06-03 00:01:56 +08:00
|
|
|
_gw->getEventQueue()->keyRelease( (osgGA::GUIEventAdapter::KeySymbol) event->ascii() );
|
2007-06-12 23:57:26 +08:00
|
|
|
#endif
|
2006-10-01 00:26:53 +08:00
|
|
|
}
|
|
|
|
|
2007-06-03 00:01:56 +08:00
|
|
|
void AdapterWidget::mousePressEvent( QMouseEvent* event )
|
2006-10-01 00:26:53 +08:00
|
|
|
{
|
|
|
|
int button = 0;
|
|
|
|
switch(event->button())
|
|
|
|
{
|
|
|
|
case(Qt::LeftButton): button = 1; break;
|
|
|
|
case(Qt::MidButton): button = 2; break;
|
|
|
|
case(Qt::RightButton): button = 3; break;
|
|
|
|
case(Qt::NoButton): button = 0; break;
|
|
|
|
default: button = 0; break;
|
|
|
|
}
|
2007-06-03 00:01:56 +08:00
|
|
|
_gw->getEventQueue()->mouseButtonPress(event->x(), event->y(), button);
|
2006-10-01 00:26:53 +08:00
|
|
|
}
|
|
|
|
|
2007-06-03 00:01:56 +08:00
|
|
|
void AdapterWidget::mouseReleaseEvent( QMouseEvent* event )
|
2006-10-01 00:26:53 +08:00
|
|
|
{
|
|
|
|
int button = 0;
|
|
|
|
switch(event->button())
|
|
|
|
{
|
|
|
|
case(Qt::LeftButton): button = 1; break;
|
|
|
|
case(Qt::MidButton): button = 2; break;
|
|
|
|
case(Qt::RightButton): button = 3; break;
|
|
|
|
case(Qt::NoButton): button = 0; break;
|
|
|
|
default: button = 0; break;
|
|
|
|
}
|
2007-06-03 00:01:56 +08:00
|
|
|
_gw->getEventQueue()->mouseButtonRelease(event->x(), event->y(), button);
|
2006-10-01 00:26:53 +08:00
|
|
|
}
|
|
|
|
|
2007-06-03 00:01:56 +08:00
|
|
|
void AdapterWidget::mouseMoveEvent( QMouseEvent* event )
|
2006-10-01 00:26:53 +08:00
|
|
|
{
|
2007-06-03 00:01:56 +08:00
|
|
|
_gw->getEventQueue()->mouseMotion(event->x(), event->y());
|
2006-10-01 00:26:53 +08:00
|
|
|
}
|
|
|
|
|
2006-10-15 19:39:35 +08:00
|
|
|
|
2007-06-04 22:47:16 +08:00
|
|
|
class ViewerQT : public osgViewer::Viewer, public AdapterWidget
|
2007-06-04 23:11:46 +08:00
|
|
|
{
|
2006-10-15 19:39:35 +08:00
|
|
|
public:
|
|
|
|
|
2007-06-12 18:19:27 +08:00
|
|
|
ViewerQT(QWidget * parent = 0, const char * name = 0, const QGLWidget * shareWidget = 0, WindowFlags f = 0):
|
2007-06-03 00:01:56 +08:00
|
|
|
AdapterWidget( parent, name, shareWidget, f )
|
|
|
|
{
|
|
|
|
getCamera()->setViewport(new osg::Viewport(0,0,width(),height()));
|
2007-06-23 19:21:54 +08:00
|
|
|
getCamera()->setProjectionMatrixAsPerspective(30.0f, static_cast<double>(width())/static_cast<double>(height()), 1.0f, 10000.0f);
|
2007-06-23 19:24:00 +08:00
|
|
|
getCamera()->setGraphicsContext(getGraphicsWindow());
|
2007-06-23 19:21:54 +08:00
|
|
|
|
2007-06-03 00:01:56 +08:00
|
|
|
setThreadingModel(osgViewer::Viewer::SingleThreaded);
|
2006-10-15 19:39:35 +08:00
|
|
|
|
2007-06-04 22:47:16 +08:00
|
|
|
connect(&_timer, SIGNAL(timeout()), this, SLOT(updateGL()));
|
|
|
|
_timer.start(10);
|
2006-10-15 19:39:35 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
virtual void paintGL()
|
|
|
|
{
|
|
|
|
frame();
|
|
|
|
}
|
2007-06-04 22:47:16 +08:00
|
|
|
|
|
|
|
protected:
|
2006-10-15 19:39:35 +08:00
|
|
|
|
2007-06-04 22:47:16 +08:00
|
|
|
QTimer _timer;
|
2007-06-06 03:10:18 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
class CompositeViewerQT : public osgViewer::CompositeViewer, public AdapterWidget
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
2007-06-12 18:19:27 +08:00
|
|
|
CompositeViewerQT(QWidget * parent = 0, const char * name = 0, const QGLWidget * shareWidget = 0, WindowFlags f = 0):
|
2007-06-06 03:10:18 +08:00
|
|
|
AdapterWidget( parent, name, shareWidget, f )
|
|
|
|
{
|
|
|
|
setThreadingModel(osgViewer::CompositeViewer::SingleThreaded);
|
|
|
|
|
|
|
|
connect(&_timer, SIGNAL(timeout()), this, SLOT(updateGL()));
|
|
|
|
_timer.start(10);
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void paintGL()
|
|
|
|
{
|
|
|
|
frame();
|
|
|
|
}
|
2007-06-04 22:47:16 +08:00
|
|
|
|
2007-06-06 03:10:18 +08:00
|
|
|
protected:
|
|
|
|
|
|
|
|
QTimer _timer;
|
2006-10-15 19:39:35 +08:00
|
|
|
};
|
|
|
|
|
2006-10-01 00:26:53 +08:00
|
|
|
int main( int argc, char **argv )
|
|
|
|
{
|
|
|
|
QApplication a( argc, argv );
|
|
|
|
|
2006-10-01 04:05:36 +08:00
|
|
|
if (argc<2)
|
2006-10-01 00:26:53 +08:00
|
|
|
{
|
|
|
|
std::cout << argv[0] <<": requires filename argument." << std::endl;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2007-06-06 03:10:18 +08:00
|
|
|
osg::ArgumentParser arguments(&argc, argv);
|
|
|
|
|
2006-10-01 00:26:53 +08:00
|
|
|
// load the scene.
|
2007-06-06 03:10:18 +08:00
|
|
|
osg::ref_ptr<osg::Node> loadedModel = osgDB::readNodeFiles(arguments);
|
2006-10-01 00:26:53 +08:00
|
|
|
if (!loadedModel)
|
|
|
|
{
|
|
|
|
std::cout << argv[0] <<": No data loaded." << std::endl;
|
|
|
|
return 1;
|
|
|
|
}
|
2007-06-06 03:10:18 +08:00
|
|
|
|
|
|
|
|
|
|
|
if (arguments.read("--CompositeViewer"))
|
|
|
|
{
|
|
|
|
CompositeViewerQT* viewerWindow = new CompositeViewerQT;
|
2006-10-01 00:26:53 +08:00
|
|
|
|
2007-06-06 03:10:18 +08:00
|
|
|
unsigned int width = viewerWindow->width();
|
|
|
|
unsigned int height = viewerWindow->height();
|
|
|
|
|
|
|
|
{
|
|
|
|
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));
|
|
|
|
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));
|
|
|
|
view2->setCameraManipulator(new osgGA::TrackballManipulator);
|
|
|
|
view2->setSceneData(loadedModel.get());
|
|
|
|
|
|
|
|
viewerWindow->addView(view2);
|
|
|
|
}
|
2006-10-01 00:26:53 +08:00
|
|
|
|
2007-06-06 03:10:18 +08:00
|
|
|
viewerWindow->show();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ViewerQT* viewerWindow = new ViewerQT;
|
2006-10-01 00:26:53 +08:00
|
|
|
|
2007-06-06 03:10:18 +08:00
|
|
|
viewerWindow->setCameraManipulator(new osgGA::TrackballManipulator);
|
|
|
|
viewerWindow->setSceneData(loadedModel.get());
|
2006-10-01 00:26:53 +08:00
|
|
|
|
2007-06-06 03:10:18 +08:00
|
|
|
viewerWindow->show();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-10-01 00:26:53 +08:00
|
|
|
a.connect( &a, SIGNAL(lastWindowClosed()), &a, SLOT(quit()) );
|
|
|
|
|
|
|
|
return a.exec();
|
|
|
|
}
|
|
|
|
|
|
|
|
/*EOF*/
|