00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013 #include <GL/glut.h>
00014 #include <iostream>
00015 #include <cmath>
00016 #include <sstream>
00017 #include <fstream>
00018
00019 #include "SensorDataGLWidget.h"
00020
00021
00022
00023
00024
00025
00026
00027 #define THIS SensorDataGLWidget
00028
00029 using namespace std;
00030
00031 int THIS::WidgetCounter = 0;
00032
00033
00034 const float ZOOM_STEP = 250.0;
00035
00036 THIS::THIS ( QWidget *parent ) : QGLWidget ( parent )
00037 {
00038 m_CameraTurning = false;
00039 m_CameraTurnStartTime = 0;
00040
00041
00042 setCursor ( Qt::SizeAllCursor );
00043
00044 m_ViewportWidth = 100;
00045 m_ViewportHeight = 100;
00046
00047 followNode( "RobotView" );
00048
00049 ostringstream s;
00050 s << "3D Sensor Display " << WidgetCounter;
00051 WidgetCounter++;
00052
00053
00054 setBgColor ( 0,0,0,0 );
00055
00056 m_RedrawTimer = new QTimer( this );
00057 connect( m_RedrawTimer, SIGNAL(timeout()), SLOT( updateGL() ) );
00058 m_RedrawTimer->start( 1000/25 );
00059
00060 }
00061
00062
00063 THIS::~THIS()
00064 {
00065 }
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101 void THIS::followNode( const QString& nodeName )
00102 {
00103 m_LookAtNode = nodeName.toStdString();
00104
00105
00106 list<PainterPlugin*>::iterator it = m_Painters.begin();
00107 while ( it != m_Painters.end() )
00108 {
00109 (*it)->nodeSelected( m_LookAtNode );
00110 it++;
00111 }
00112
00113 if ( m_LookAtNode.find( "Image" ) == m_LookAtNode.length()-5 )
00114 {
00115 m_LookAtNode = m_LookAtNode.substr( 0, m_LookAtNode.length()-5 );
00116 m_CameraRotationZ = 0;
00117 m_CameraRotationY = 0;
00118 m_CameraDistance = 0;
00119 m_CameraOpeningAngle = 60.0;
00120 m_CameraImageMode = true;
00121 }
00122 else
00123 {
00124 m_CameraRotationZ = 0;
00125 m_CameraRotationY = 30;
00126 m_CameraDistance = 3000;
00127 m_CameraOpeningAngle = 60.0;
00128 m_CameraImageMode = false;
00129 }
00130
00131 m_LookAt = BaseLib::Math::Vec3d(0,0,0);
00132
00133 m_ForceRedraw = true;
00134 updateGL();
00135 }
00136
00137
00138 void THIS::initializeGL()
00139 {
00140
00141 glEnable ( GL_DEPTH_TEST );
00142 }
00143
00144 void THIS::setProjectionMatrix()
00145 {
00146
00147
00148 glMatrixMode ( GL_PROJECTION );
00149 glLoadIdentity();
00150 gluPerspective ( m_CameraOpeningAngle, ( float ) m_ViewportWidth / ( float ) m_ViewportHeight, 150, 100000 );
00151
00152 glMatrixMode ( GL_MODELVIEW );
00153 }
00154
00155
00156 void THIS::resizeGL ( int w, int h )
00157 {
00158
00159 glViewport ( 0, 0, w, h );
00160 m_ViewportWidth = w;
00161 m_ViewportHeight = h;
00162 m_ForceRedraw = true;
00163 updateGL();
00164 }
00165
00166 void THIS::setBgColor ( float r, float g, float b, float a )
00167 {
00168 glClearColor ( r, g, b, a );
00169 }
00170
00171 void THIS::addPainter ( PainterPlugin *painter )
00172 {
00173 if ( !painter )
00174 {
00175
00176 return;
00177 }
00178 m_Painters.push_back ( painter );
00179 m_PaintersMap[painter->getName()] = painter;
00180 painter->setParent( this );
00181 }
00182
00183
00184
00185
00186 void THIS::paintGL()
00187 {
00188
00189
00190 bool redraw = m_ForceRedraw || m_CameraTurning;
00191
00192
00193 list<PainterPlugin*>::iterator it = m_Painters.begin();
00194 while ( it != m_Painters.end() )
00195 {
00196 if ( ( *it )->needsRedraw() )
00197 {
00198 redraw = true;
00199 ( *it )->wasRedrawn();
00200 }
00201 it++;
00202 }
00203
00204 if ( redraw && isVisible() )
00205 {
00206 setProjectionMatrix();
00207
00208 glClear ( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
00209
00210
00211 glLoadIdentity();
00212
00213 if ( !m_CameraImageMode )
00214 {
00215 glTranslatef( 0,-m_CameraDistance*0.3,0 );
00216 }
00217
00218 BaseLib::Math::Vec3d cameraOpposite( 1,0,0 );
00219
00220
00221 cameraOpposite = BaseLib::Math::rotationY<double,3>( m_CameraRotationY/180.0*M_PI ) * cameraOpposite;
00222 cameraOpposite = BaseLib::Math::rotationZ<double,3>( m_CameraRotationZ/180.0*M_PI ) * cameraOpposite;
00223
00224 BaseLib::Math::Vec3d cameraPos = cameraOpposite;
00225 cameraPos *= m_CameraDistance * -1.0;
00226
00227
00228
00229 gluLookAt ( cameraPos.x, cameraPos.y, cameraPos.z,
00230 cameraOpposite.x, cameraOpposite.y, cameraOpposite.z,
00231 0.0, 0.0, 1.0 );
00232
00233 if ( m_CameraTurning )
00234 {
00235 float time = 1;
00236
00237 float turnTime = 3.0;
00238
00239 float t = ( time - m_CameraTurnStartTime ) / 1000.0 / turnTime;
00240 if ( t > 1.0 ) {
00241 m_CameraTurning = false;
00242
00243 }
00244 else
00245 {
00246
00247 float tSmooth = -0.5*cos ( t*M_PI ) + 0.5;
00248
00249 float deltaRad = tSmooth * 2.0 * M_PI;
00250 glRotatef( deltaRad/M_PI*180.0,0,0,1 );
00251
00252 float sSmooth = 1 - sin( t * M_PI ) / 2.0;
00253 glScalef( sSmooth, sSmooth, sSmooth);
00254 }
00255 }
00256
00257
00258
00259
00260
00261
00262
00263
00264
00265
00266
00267
00268
00269
00270
00271
00272
00273 glColor3f ( 0.5, 0.5, 0.5 );
00274 glLineWidth ( 3.0 );
00275 glBegin ( GL_LINES );
00276 glVertex3f ( 30.0, 30.0, 10.0 );
00277 glVertex3f ( -30.0, -30.0, 10.0 );
00278 glVertex3f ( -30.0, 30.0, 10.0 );
00279 glVertex3f ( 30.0, -30.0, 10.0 );
00280 glEnd();
00281
00282 glTranslatef( m_LookAt.x, m_LookAt.y, 0 );
00283
00284
00285
00286
00287
00288
00289
00290
00291
00292
00293
00294
00295 float nextLayer = 0.0;
00296
00297
00298
00299
00300 list< PainterPlugin*>::iterator it = m_Painters.begin();
00301 while ( it != m_Painters.end() )
00302 {
00303 if ( ( *it )->isVisible() )
00304 {
00305 glPushMatrix();
00306 ( *it )->paint ( nextLayer );
00307 glPopMatrix();
00308
00309 nextLayer += 5.0;
00310 }
00311 it++;
00312 }
00313
00314
00315 }
00316
00317 }
00318
00319
00320 void THIS::performCameraMove()
00321 {
00322 if ( !m_CameraTurning )
00323 {
00324 m_CameraTurning = true;
00325
00326
00327
00328 }
00329 }
00330
00331
00332 QSize THIS::minimumSizeHint() const
00333 {
00334 return QSize ( 150, 120 );
00335 }
00336
00337 QSize THIS::sizeHint() const
00338 {
00339 return QSize ( 400, 300 );
00340 }
00341
00342
00344
00345
00346 void THIS::mousePressEvent ( QMouseEvent* event )
00347 {
00348
00349 m_MousePosOld = event->pos();
00350 }
00351
00352
00353 void THIS::mouseMoveEvent ( QMouseEvent* event )
00354 {
00355 if ( m_CameraImageMode ) {
00356 return;
00357 }
00358
00359 float deltaX = -m_MousePosOld.x() + event->x();
00360 float deltaY = -m_MousePosOld.y() + event->y();
00361 m_MousePosOld = event->pos();
00362
00363 if ( event->buttons() & Qt::RightButton )
00364 {
00365 float rotateZ = deltaX/2.0;
00366 float rotateY = deltaY/2.0;
00367
00368 m_CameraRotationZ -= rotateZ;
00369 m_CameraRotationY += rotateY;
00370
00371 if ( m_CameraRotationZ >= 360 ) { m_CameraRotationZ -= 360; }
00372 if ( m_CameraRotationZ < 0 ) { m_CameraRotationZ += 360; }
00373 if ( m_CameraRotationY >= 89.9 ) { m_CameraRotationY = 89.9; }
00374 if ( m_CameraRotationY <= -89.9 ) { m_CameraRotationY = -89.9; }
00375 m_ForceRedraw = true;
00376 }
00377 else if ( event->buttons() & Qt::LeftButton )
00378 {
00379 BaseLib::Math::Vec3d translation(-deltaY,-deltaX,0);
00380
00381
00382 translation = BaseLib::Math::rotationZ<double,3>( m_CameraRotationZ/180.0*M_PI ) * translation;
00383
00384 translation *= m_CameraDistance/500.0;
00385
00386 m_LookAt += translation;
00387 m_ForceRedraw = true;
00388 }
00389 }
00390
00391
00392 void THIS::wheelEvent ( QWheelEvent* event )
00393 {
00394 if ( ( event->delta() == 0 ) )
00395 {
00396 return;
00397 }
00398
00399 if ( m_CameraImageMode )
00400 {
00401 if ( event->delta() > 0 )
00402 {
00403 m_CameraOpeningAngle += 5;
00404 }
00405 else
00406 {
00407 m_CameraOpeningAngle -= 5;
00408 }
00409 if ( m_CameraOpeningAngle < 5.0 )
00410 {
00411 m_CameraOpeningAngle = 5.0;
00412 }
00413 if ( m_CameraOpeningAngle > 85.0 )
00414 {
00415 m_CameraOpeningAngle = 85.0;
00416 }
00417 m_ForceRedraw = true;
00418 updateGL();
00419 }
00420 else
00421 {
00422 if ( event->delta() > 0 )
00423 {
00424 m_CameraDistance = m_CameraDistance/1.05 - 10.0;
00425 }
00426 else
00427 {
00428 m_CameraDistance = m_CameraDistance*1.05 + 10.0;
00429 }
00430 if ( m_CameraDistance < 0.0 )
00431 {
00432 m_CameraDistance = 0.0;
00433 }
00434 if ( m_CameraDistance > 20000.0 )
00435 {
00436 m_CameraDistance = 20000.0;
00437 }
00438 m_ForceRedraw = true;
00439 }
00440 }
00441
00442 void THIS::setCameraRotation( float rotationY, float rotationZ )
00443 {
00444 m_CameraRotationY = rotationY;
00445 m_CameraRotationZ = rotationZ;
00446 m_ForceRedraw = true;
00447 }
00448
00449 void THIS::setLookAt( BaseLib::Math::Vec3d lookAt )
00450 {
00451 m_LookAt = lookAt;
00452 m_ForceRedraw = true;
00453 }
00454
00455 #undef THIS
00456