1 // Single-threaded QT example
11 #include <Qt/qapplication.h>
12 #include <QtGui/QMouseEvent>
13 #include <QtGui/QKeyEvent>
14 #include <QtOpenGL/QGLContext>
20 #include "OSGFieldContainerFactory.h"
21 #include "OSGVector.h"
22 #include "OSGQuaternion.h"
23 #include "OSGMatrix.h"
24 #include "OSGMatrixUtility.h"
25 #include "OSGBoxVolume.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"
49 #include "OSGTrackball.h"
51 // We need our own context to prevent Qt to set its own context current
52 class MyQGLContext
: public QGLContext
55 MyQGLContext (const QGLFormat
& format
) :
58 virtual void makeCurrent (){}; // on makeCurrent() just do pretty nothing
61 class MyOSGQGLWidget
: public OSG::OSGQGLWidget
64 MyOSGQGLWidget( QWidget
*parent
=0, const char *name
=0 );
65 MyOSGQGLWidget( MyQGLContext
* context
,
67 const QGLWidget
* shareWidget
= 0,
68 Qt::WindowFlags f
= 0);
71 OSG::QT4WindowRecPtr osgWin
;
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);
89 MyOSGQGLWidget
*glWidget
;
92 OSG::RenderActionRefPtr ract
;
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
,
108 const QGLWidget
* shareWidget
,
110 OSG::OSGQGLWidget(context
, parent
, shareWidget
, f
)
113 void MyOSGQGLWidget::initOpenGL ( void )
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
136 osgWin
->activate(); // and activate it
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
);
154 OSG::commitChanges();
157 osgWin
->frameInit(); // call it to setup extensions
158 osgWin
->frameExit(); // for symmetry
162 void MyOSGQGLWidget::paintGL ( void )
164 OSG::Matrix m1
, m2
, m3
;
168 osgWin
->frameInit(); // frame-init
171 tball
.getRotation().getValue(m3
);
174 m2
.setTranslate( tball
.getPosition() );
176 cam_trans
->setMatrix( m1
);
178 OSG::commitChanges();
180 osgWin
->render(ract
); // draw the viewports
183 osgWin
->renderAllViewports( ract
); // draw the viewports
186 osgWin
->frameExit(); // frame-cleanup
190 void MyOSGQGLWidget::resizeGL ( int w
, int h
)
192 osgWin
->resize( w
, h
);
195 void MyOSGQGLWidget::mousePressEvent ( QMouseEvent
*me
)
197 switch ( me
->button() )
200 tball
.setAutoPosition(true);
202 case Qt::RightButton
:
203 tball
.setAutoPositionNeg(true);
208 mouseb
|= me
->button();
213 void MyOSGQGLWidget::mouseReleaseEvent ( QMouseEvent
*me
)
215 switch ( me
->button() )
218 tball
.setAutoPosition(false);
220 case Qt::RightButton
:
221 tball
.setAutoPositionNeg(false);
226 mouseb
&= ~me
->button();
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();
262 void MyOSGQGLWidget::keyPressEvent ( QKeyEvent
*ke
)
264 if ( ke
->key() == Qt::Key_Escape
)
271 int main( int argc
, char **argv
)
274 OSG::osgInit(argc
, argv
);
276 OSG::SceneFileHandler::the()->print();
280 // beacon for camera and light
281 OSG::NodeRecPtr b1n
= OSG::Node::create();
282 OSG::GroupRecPtr b1
= OSG::Group::create();
286 OSG::NodeRecPtr t1n
= OSG::Node::create();
287 OSG::TransformRecPtr t1
= OSG::Transform::create();
289 t1n
->addChild( b1n
);
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);
305 root
= OSG::Node::create();
306 OSG::GroupRecPtr gr1
= OSG::Group::create();
307 root
->setCore( gr1
);
308 root
->addChild( t1n
);
309 root
->addChild( dlight
);
312 OSG::NodeRecPtr file
= NULL
;
315 file
= OSG::SceneFileHandler::the()->read(argv
[1]);
319 std::cerr
<< "Couldn't load file, ignoring" << std::endl
;
320 file
= OSG::makeTorus( .5, 2, 16, 16 );
323 OSG::commitChanges();
325 file
->updateVolume();
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
;
338 OSG::PerspectiveCameraRecPtr cam
= OSG::PerspectiveCamera::create();
340 cam
->setBeacon( b1n
);
341 cam
->setFov( OSG::osgDegree2Rad( 60 ) );
346 OSG::SolidBackgroundRecPtr bkgnd
= OSG::SolidBackground::create();
347 bkgnd
->setColor( OSG::Color3f( 0,0,1 ) );
350 vp
= OSG::Viewport::create();
351 vp
->setCamera( cam
);
352 vp
->setBackground( bkgnd
);
354 vp
->setSize( 0,0, 1,1 );
357 ract
= OSG::RenderAction::create();
360 QApplication::setColorSpec( QApplication::CustomColor
);
361 a
= new QApplication( argc
, argv
);
363 if ( !QGLFormat::hasOpenGL() )
365 qWarning( "This system has no OpenGL support. Exiting." );
369 OSG::Vec3f
pos( 0, 0, max
[2] + ( max
[2] - min
[2] ) * 1.5 );
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
);
387 a
->exec(); // execute QT main loop