1 // little example on how to use OpenSG with a standard QT OpenGL widget
2 // should be independent of QT version, and might also be used as an example
3 // on how ot integrate OpenSG into other windowing toolkits
5 // The only thing lost by doing it like this is the ability to separate the
6 // rendering and interface threads. If you need that you will have to
7 // use the real OpenSG QT widget.
9 // It is ASAP (as simple as possible ;), and thus probably not a good example
10 // for style, but I hope it's good enough to see what's going on.
12 // This code is based on the QT opengl/gear example.
16 #include <Qt/qapplication.h>
17 #include <QtOpenGL/QGLWidget>
18 #include <QtGui/QMouseEvent>
20 #if defined(Q_CC_MSVC)
21 #pragma warning(disable:4305) // init: truncation from const double to float
24 #include "OSGConfig.h"
25 #include "OSGSceneFileHandler.h"
26 #include "OSGSimpleGeometry.h"
27 #include "OSGPassiveWindow.h"
28 #include "OSGSimpleSceneManager.h"
30 class OpenSGWidget
: public QGLWidget
33 OpenSGWidget(const QGLFormat
&f
, QWidget
*parent
=0);
35 OSG::SimpleSceneManager
*getManager(void);
38 void resizeGL( int, int );
40 void mousePressEvent( QMouseEvent
*ev
);
41 void mouseMoveEvent( QMouseEvent
*ev
);
42 void mouseReleaseEvent( QMouseEvent
*ev
);
43 void wheelEvent( QWheelEvent
*ev
);
45 OSG::SimpleSceneManagerRefPtr mgr
;
46 OSG::PassiveWindowRecPtr pwin
;
49 OpenSGWidget::OpenSGWidget(const QGLFormat
&f
, QWidget
*parent
)
50 : QGLWidget(f
, parent
)
52 setAutoBufferSwap(false);
53 mgr
= OSG::SimpleSceneManager::create();
54 pwin
= OSG::PassiveWindow::create();
58 OSG::SimpleSceneManager
*OpenSGWidget::getManager(void)
64 void OpenSGWidget::initializeGL()
69 void OpenSGWidget::resizeGL( int width
, int height
)
71 mgr
->resize(width
,height
);
74 void OpenSGWidget::paintGL()
80 void OpenSGWidget::mousePressEvent( QMouseEvent
*ev
)
84 switch ( ev
->button() )
87 button
= OSG::SimpleSceneManager::MouseLeft
;
90 button
= OSG::SimpleSceneManager::MouseMiddle
;
93 button
= OSG::SimpleSceneManager::MouseRight
;
97 mgr
->mouseButtonPress(button
, ev
->x(), ev
->y());
101 void OpenSGWidget::mouseReleaseEvent( QMouseEvent
*ev
)
105 switch ( ev
->button() )
108 button
= OSG::SimpleSceneManager::MouseLeft
;
111 button
= OSG::SimpleSceneManager::MouseMiddle
;
113 case Qt::RightButton
:
114 button
= OSG::SimpleSceneManager::MouseRight
;
118 mgr
->mouseButtonRelease(button
, ev
->x(), ev
->y());
122 void OpenSGWidget::mouseMoveEvent( QMouseEvent
*ev
)
124 mgr
->mouseMove(ev
->x(), ev
->y());
128 void OpenSGWidget::wheelEvent( QWheelEvent
*ev
)
130 mgr
->mouseButtonPress(ev
->delta() > 0 ? OSG::SimpleSceneManager::MouseUp
131 : OSG::SimpleSceneManager::MouseDown
,
139 int main( int argc
, char **argv
)
141 OSG::osgInit(argc
,argv
);
143 QApplication::setColorSpec( QApplication::CustomColor
);
144 QApplication
a( argc
, argv
);
146 if ( !QGLFormat::hasOpenGL() ) {
147 qWarning( "This system has no OpenGL support. Exiting." );
151 OpenSGWidget
w(QGLFormat(QGL::DoubleBuffer
| QGL::DepthBuffer
| QGL::Rgba
|
152 QGL::DirectRendering
));
155 OSG::NodeRecPtr scene
;
159 scene
= OSG::Node::create();
160 OSG::GroupRecPtr g
= OSG::Group::create();
164 for(OSG::UInt16 i
= 1; i
< argc
; ++i
)
165 scene
->addChild(OSG::SceneFileHandler::the()->read(argv
[i
]));
169 scene
= OSG::makeTorus(.5, 3, 16, 16);
172 w
.getManager()->setRoot(scene
);
173 w
.getManager()->showAll();