2006-10-01 21:26:02 +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
|
|
|
|
|
2006-11-02 20:27:15 +08:00
|
|
|
#include <osgViewer/SimpleViewer>
|
2006-10-01 21:26:02 +08:00
|
|
|
#include <osgGA/TrackballManipulator>
|
|
|
|
#include <osgDB/ReadFile>
|
|
|
|
|
|
|
|
#include <QtCore/QTimer>
|
|
|
|
#include <QtGui/QKeyEvent>
|
|
|
|
#include <QtGui/QApplication>
|
|
|
|
#include <QtOpenGL/QGLWidget>
|
|
|
|
|
|
|
|
#include <iostream>
|
|
|
|
|
2006-11-02 20:27:15 +08:00
|
|
|
class GraphicsWindowQT : public QGLWidget, virtual osgViewer::GraphicsWindow
|
2006-10-01 21:26:02 +08:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
2006-10-05 18:59:04 +08:00
|
|
|
GraphicsWindowQT( QWidget * parent = 0, const char * name = 0, const QGLWidget * shareWidget = 0, Qt::WFlags f = 0 );
|
|
|
|
virtual ~GraphicsWindowQT() {}
|
2006-10-01 21:26:02 +08:00
|
|
|
|
|
|
|
protected:
|
|
|
|
|
|
|
|
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 );
|
|
|
|
|
|
|
|
QTimer _timer;
|
|
|
|
};
|
|
|
|
|
2006-10-05 18:59:04 +08:00
|
|
|
GraphicsWindowQT::GraphicsWindowQT( QWidget * parent, const char * /*name*/, const QGLWidget * shareWidget, Qt::WFlags f):
|
2006-10-01 21:26:02 +08:00
|
|
|
QGLWidget(parent, shareWidget, f)
|
|
|
|
{
|
|
|
|
connect(&_timer, SIGNAL(timeout()), this, SLOT(updateGL()));
|
|
|
|
_timer.start(10);
|
|
|
|
}
|
|
|
|
|
2006-10-05 18:59:04 +08:00
|
|
|
void GraphicsWindowQT::resizeGL( int width, int height )
|
2006-10-01 21:26:02 +08:00
|
|
|
{
|
|
|
|
getEventQueue()->windowResize(0, 0, width, height );
|
|
|
|
}
|
|
|
|
|
2006-10-05 18:59:04 +08:00
|
|
|
void GraphicsWindowQT::keyPressEvent( QKeyEvent* event )
|
2006-10-01 21:26:02 +08:00
|
|
|
{
|
|
|
|
getEventQueue()->keyPress( (osgGA::GUIEventAdapter::KeySymbol) event->key() );
|
|
|
|
}
|
|
|
|
|
2006-10-05 18:59:04 +08:00
|
|
|
void GraphicsWindowQT::keyReleaseEvent( QKeyEvent* event )
|
2006-10-01 21:26:02 +08:00
|
|
|
{
|
|
|
|
getEventQueue()->keyRelease( (osgGA::GUIEventAdapter::KeySymbol) event->key() );
|
|
|
|
}
|
|
|
|
|
2006-10-05 18:59:04 +08:00
|
|
|
void GraphicsWindowQT::mousePressEvent( QMouseEvent* event )
|
2006-10-01 21:26:02 +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;
|
|
|
|
}
|
|
|
|
getEventQueue()->mouseButtonPress(event->x(), event->y(), button);
|
|
|
|
}
|
|
|
|
|
2006-10-05 18:59:04 +08:00
|
|
|
void GraphicsWindowQT::mouseReleaseEvent( QMouseEvent* event )
|
2006-10-01 21:26:02 +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;
|
|
|
|
}
|
|
|
|
getEventQueue()->mouseButtonRelease(event->x(), event->y(), button);
|
|
|
|
}
|
|
|
|
|
2006-10-05 18:59:04 +08:00
|
|
|
void GraphicsWindowQT::mouseMoveEvent( QMouseEvent* event )
|
2006-10-01 21:26:02 +08:00
|
|
|
{
|
|
|
|
getEventQueue()->mouseMotion(event->x(), event->y());
|
|
|
|
}
|
|
|
|
|
2006-10-05 18:59:04 +08:00
|
|
|
|
2006-11-02 20:27:15 +08:00
|
|
|
class SimpleViewerQT : public osgViewer::SimpleViewer, public GraphicsWindowQT
|
2006-10-05 18:59:04 +08:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
SimpleViewerQT() {}
|
|
|
|
|
|
|
|
|
|
|
|
virtual void initializeGL()
|
|
|
|
{
|
|
|
|
QGLWidget::initializeGL();
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void paintGL()
|
|
|
|
{
|
|
|
|
frame();
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2006-10-01 21:26:02 +08:00
|
|
|
int main( int argc, char **argv )
|
|
|
|
{
|
|
|
|
QApplication a( argc, 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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-10-05 18:59:04 +08:00
|
|
|
SimpleViewerQT* viewerWindow = new SimpleViewerQT;
|
2006-10-01 21:26:02 +08:00
|
|
|
|
|
|
|
viewerWindow->setSceneData(loadedModel.get());
|
|
|
|
viewerWindow->setCameraManipulator(new osgGA::TrackballManipulator);
|
|
|
|
|
|
|
|
viewerWindow->show();
|
|
|
|
a.connect( &a, SIGNAL(lastWindowClosed()), &a, SLOT(quit()) );
|
|
|
|
|
|
|
|
return a.exec();
|
|
|
|
}
|
|
|
|
|
|
|
|
/*EOF*/
|