changed: auto add updateData callback to stages so that stagedata can be updated...
[opensg.git] / Source / WindowSystem / QT4 / testWindowQT4_qt.cpp
blob36985dd8eff384d9d2f9c952da721ce1b05ace1a
1 // Single-threaded QT example
3 #include "OSGConfig.h"
5 #include <iostream>
7 #ifdef __sgi
8 #pragma set woff 1375
9 #endif
11 #include <Qt/qapplication.h>
12 #include <QtGui/QMouseEvent>
13 #include <QtGui/QKeyEvent>
14 #include <QtOpenGL/QGLContext>
16 #ifdef __sgi
17 #pragma set woff 1375
18 #endif
20 #include "OSGFieldContainerFactory.h"
21 #include "OSGVector.h"
22 #include "OSGQuaternion.h"
23 #include "OSGMatrix.h"
24 #include "OSGMatrixUtility.h"
25 #include "OSGBoxVolume.h"
26 #include "OSGNode.h"
27 #include "OSGGroup.h"
28 #include "OSGTransform.h"
29 #include "OSGAttachment.h"
30 #include "OSGAction.h"
31 #include "OSGRenderAction.h"
32 #include "OSGSceneFileHandler.h"
33 #include "OSGDirectionalLight.h"
34 #include "OSGSimpleGeometry.h"
36 #include "OSGViewport.h"
37 #include "OSGCamera.h"
38 #include "OSGWindow.h"
39 #include "OSGQT4Window.h"
40 #include "OSGCamera.h"
41 #include "OSGPerspectiveCamera.h"
42 #include "OSGSolidBackground.h"
43 #include "OSGGradientBackground.h"
45 #if defined(__linux) || ( defined(WIN32) && ! defined(OSG_BUILD_DLL) )
46 #include "OSGRAWSceneFileType.h"
47 #endif
49 #include "OSGTrackball.h"
51 // We need our own context to prevent Qt to set its own context current
52 class MyQGLContext : public QGLContext
54 public:
55 MyQGLContext (const QGLFormat & format) :
56 QGLContext(format)
57 {};
58 virtual void makeCurrent (){}; // on makeCurrent() just do pretty nothing
61 class MyOSGQGLWidget : public OSG::OSGQGLWidget
63 public:
64 MyOSGQGLWidget( QWidget *parent=0, const char *name=0 );
65 MyOSGQGLWidget( MyQGLContext * context,
66 QWidget * parent = 0,
67 const QGLWidget * shareWidget = 0,
68 Qt::WindowFlags f = 0);
70 OSG::Trackball tball;
71 OSG::QT4WindowRecPtr osgWin;
73 protected:
74 virtual void initializeGL (void);
75 virtual void paintGL (void);
76 virtual void resizeGL (int w, int h);
77 virtual void mousePressEvent ( QMouseEvent* );
78 virtual void mouseReleaseEvent ( QMouseEvent* );
79 virtual void mouseMoveEvent ( QMouseEvent* );
80 virtual void keyPressEvent ( QKeyEvent* );
82 static void initOpenGL(void);
84 OSG::UInt32 mouseb;
85 OSG::Int32 lastx;
86 OSG::Int32 lasty;
89 MyOSGQGLWidget *glWidget;
90 QApplication *a;
92 OSG::RenderActionRefPtr ract;
93 OSG::NodeRecPtr root;
94 OSG::NodeRecPtr file;
95 OSG::ViewportRecPtr vp;
96 OSG::TransformRecPtr cam_trans;
97 OSG::PerspectiveCameraRecPtr cam;
99 OSG::QT4WindowRecPtr osgTWin;
101 MyOSGQGLWidget::MyOSGQGLWidget ( QWidget *parent, const char *name ) :
102 OSG::OSGQGLWidget( parent, name )
106 MyOSGQGLWidget::MyOSGQGLWidget( MyQGLContext * context,
107 QWidget * parent,
108 const QGLWidget * shareWidget,
109 Qt::WindowFlags f) :
110 OSG::OSGQGLWidget(context, parent, shareWidget, f)
113 void MyOSGQGLWidget::initOpenGL ( void )
115 // some manual init
116 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
117 glPixelStorei(GL_PACK_ALIGNMENT, 1);
119 glDepthFunc(GL_LEQUAL );
120 glEnable (GL_DEPTH_TEST);
122 glEnable (GL_NORMALIZE );
124 // switch off default light
125 OSG::Real32 nul[4]={0.f,0.f,0.f,0.f};
127 glLightfv(GL_LIGHT0, GL_DIFFUSE, nul);
128 glLightfv(GL_LIGHT0, GL_SPECULAR, nul);
131 void MyOSGQGLWidget::initializeGL ( void )
133 osgWin->init(&MyOSGQGLWidget::initOpenGL); // create the context
135 #if 0
136 osgWin->activate(); // and activate it
138 // some manual init
139 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
140 glPixelStorei(GL_PACK_ALIGNMENT, 1);
142 glDepthFunc(GL_LEQUAL );
143 glEnable (GL_DEPTH_TEST);
145 glEnable (GL_NORMALIZE );
147 // switch off default light
148 Real32 nul[4]={0.f,0.f,0.f,0.f};
150 glLightfv(GL_LIGHT0, GL_DIFFUSE, nul);
151 glLightfv(GL_LIGHT0, GL_SPECULAR, nul);
152 #endif
154 OSG::commitChanges();
156 #if 0
157 osgWin->frameInit(); // call it to setup extensions
158 osgWin->frameExit(); // for symmetry
159 #endif
162 void MyOSGQGLWidget::paintGL ( void )
164 OSG::Matrix m1, m2, m3;
165 OSG::Quaternion q1;
167 #if 0
168 osgWin->frameInit(); // frame-init
169 #endif
171 tball.getRotation().getValue(m3);
172 q1.setValue(m3);
173 m1.setRotate(q1);
174 m2.setTranslate( tball.getPosition() );
175 m1.mult( m2 );
176 cam_trans->setMatrix( m1 );
178 OSG::commitChanges();
180 osgWin->render(ract); // draw the viewports
182 #if 0
183 osgWin->renderAllViewports( ract ); // draw the viewports
184 osgWin->swap();
186 osgWin->frameExit(); // frame-cleanup
187 #endif
190 void MyOSGQGLWidget::resizeGL ( int w, int h )
192 osgWin->resize( w, h );
195 void MyOSGQGLWidget::mousePressEvent ( QMouseEvent *me )
197 switch ( me->button() )
199 case Qt::MidButton:
200 tball.setAutoPosition(true);
201 break;
202 case Qt::RightButton:
203 tball.setAutoPositionNeg(true);
204 break;
205 default:
206 break;
208 mouseb |= me->button();
209 lastx = me->x();
210 lasty = me->y();
213 void MyOSGQGLWidget::mouseReleaseEvent ( QMouseEvent *me )
215 switch ( me->button() )
217 case Qt::MidButton:
218 tball.setAutoPosition(false);
219 break;
220 case Qt::RightButton:
221 tball.setAutoPositionNeg(false);
222 break;
223 default:
224 break;
226 mouseb &= ~me->button();
228 lastx = me->x();
229 lasty = me->y();
232 void MyOSGQGLWidget::mouseMoveEvent ( QMouseEvent *me )
234 OSG::Real32 w = osgWin->getWidth(); // force the calc to Real32
235 OSG::Real32 h = osgWin->getHeight();
237 OSG::Real32 a = -2. * ( lastx / w - .5 );
238 OSG::Real32 b = -2. * ( .5 - lasty / h );
239 OSG::Real32 c = -2. * ( me->pos().x() / w - .5 );
240 OSG::Real32 d = -2. * ( .5 - me->pos().y() / h );
242 if ( mouseb & Qt::LeftButton )
244 tball.updateRotation( a, b, c, d );
246 else if ( mouseb & Qt::MidButton )
248 tball.updatePosition( a, b, c, d );
250 else if ( mouseb & Qt::RightButton )
252 tball.updatePositionNeg( a, b, c, d );
255 lastx = me->pos().x();
256 lasty = me->pos().y();
258 paintGL();
262 void MyOSGQGLWidget::keyPressEvent ( QKeyEvent *ke )
264 if ( ke->key() == Qt::Key_Escape )
266 a->quit();
271 int main( int argc, char **argv )
273 // OSG init
274 OSG::osgInit(argc, argv);
276 OSG::SceneFileHandler::the()->print();
278 // create the graph
280 // beacon for camera and light
281 OSG::NodeRecPtr b1n = OSG::Node::create();
282 OSG::GroupRecPtr b1 = OSG::Group::create();
283 b1n->setCore( b1 );
285 // transformation
286 OSG::NodeRecPtr t1n = OSG::Node::create();
287 OSG::TransformRecPtr t1 = OSG::Transform::create();
288 t1n->setCore( t1 );
289 t1n->addChild( b1n );
291 cam_trans = t1;
293 // light
294 OSG::NodeRecPtr dlight = OSG::Node::create();
295 OSG::DirectionalLightRecPtr dl = OSG::DirectionalLight::create();
297 dlight->setCore( dl );
299 dl->setAmbient( .3, .3, .3, 1 );
300 dl->setDiffuse( 1, 1, 1, 1 );
301 dl->setDirection(0,0,1);
302 dl->setBeacon( b1n);
304 // root
305 root = OSG::Node::create();
306 OSG::GroupRecPtr gr1 = OSG::Group::create();
307 root->setCore( gr1 );
308 root->addChild( t1n );
309 root->addChild( dlight );
311 // Load the file
312 OSG::NodeRecPtr file = NULL;
314 if ( argc > 1 )
315 file = OSG::SceneFileHandler::the()->read(argv[1]);
317 if ( file == NULL )
319 std::cerr << "Couldn't load file, ignoring" << std::endl;
320 file = OSG::makeTorus( .5, 2, 16, 16 );
323 OSG::commitChanges();
325 file->updateVolume();
327 OSG::Vec3f min,max;
328 file->getVolume().getBounds( min, max );
330 std::cout << "Volume: from " << min << " to " << max << std::endl;
332 dlight->addChild( file );
334 std::cerr << "Tree: " << std::endl;
335 root->dump();
337 // Camera
338 OSG::PerspectiveCameraRecPtr cam = OSG::PerspectiveCamera::create();
340 cam->setBeacon( b1n );
341 cam->setFov( OSG::osgDegree2Rad( 60 ) );
342 cam->setNear( 1 );
343 cam->setFar( 4000 );
345 // Background
346 OSG::SolidBackgroundRecPtr bkgnd = OSG::SolidBackground::create();
347 bkgnd->setColor( OSG::Color3f( 0,0,1 ) );
349 // Viewport
350 vp = OSG::Viewport::create();
351 vp->setCamera( cam );
352 vp->setBackground( bkgnd );
353 vp->setRoot( root );
354 vp->setSize( 0,0, 1,1 );
356 // Action
357 ract = OSG::RenderAction::create();
359 // QT init
360 QApplication::setColorSpec( QApplication::CustomColor );
361 a = new QApplication( argc, argv );
363 if ( !QGLFormat::hasOpenGL() )
365 qWarning( "This system has no OpenGL support. Exiting." );
366 return -1;
369 OSG::Vec3f pos( 0, 0, max[2] + ( max[2] - min[2] ) * 1.5 );
371 // Create Window
372 glWidget = new MyOSGQGLWidget(new MyQGLContext(QGLFormat::defaultFormat()), 0, 0, 0);
374 glWidget->tball.setMode( OSG::Trackball::OSGObject );
375 glWidget->tball.setStartPosition( pos, true );
376 glWidget->tball.setSum( true );
377 glWidget->tball.setTranslationMode( OSG::Trackball::OSGFree );
379 glWidget->osgWin = OSG::QT4Window::create();
380 glWidget->osgWin->setGlWidget( glWidget );
381 glWidget->osgWin->addPort( vp );
383 a->processEvents();
385 glWidget->show();
387 a->exec(); // execute QT main loop
389 root = NULL;
390 file = NULL;
391 vp = NULL;
392 cam_trans = NULL;
393 cam = NULL;
394 osgTWin = NULL;
396 delete glWidget;
397 glWidget = NULL;
399 return 0;