SensorDataGLWidget.cpp
Go to the documentation of this file.
00001 /*******************************************************************************
00002  *  SensorDataGLWidget.cpp
00003  *
00004  *  (C) 2006 AG Aktives Sehen <agas@uni-koblenz.de>
00005  *           Universitaet Koblenz-Landau
00006  *
00007  *  Additional information:
00008  *  $Id: SensorDataGLWidget.cpp 44313 2011-04-06 22:46:28Z agas $
00009  *******************************************************************************/
00010 
00011 
00012 //#include <QtOpenGL>
00013 #include <GL/glut.h>
00014 #include <iostream>
00015 #include <cmath>
00016 #include <sstream>
00017 #include <fstream>
00018 
00019 #include "SensorDataGLWidget.h"
00020 
00021 //#include "Architecture/Config/Config.h"
00022 //#include "Architecture/Tracer/Tracer.h"
00023 //#include "Architecture/Singleton/Clock.h"
00024 
00025 //#include "Messages/SceneGraphM.h"
00026 
00027 #define THIS SensorDataGLWidget
00028 
00029 using namespace std;
00030 
00031 int THIS::WidgetCounter = 0;
00032 
00033 // the zooming step that is made when the mouse wheel is turned
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     // default cursor = drag cursor
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   //  m_Timer = Timer ( ProfilerEntry::CODE_SEGMENT, "GUI Thread", s.str() );
00053 
00054     setBgColor ( 0,0,0,0 );
00055 
00056     m_RedrawTimer = new QTimer( this ); // create internal timer
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 //void THIS::processMessage ( Message* newMessage )
00068 //{
00069 //    ostringstream stream;
00070 
00071 //    m_Timer.startMeasure();
00072 //    switch ( newMessage->getType() )
00073 //    {
00074 //    case MessageTypes::SCENE_GRAPH_M:
00075 //        {
00076 //            if ( SceneGraphM* message = Message::castTo<SceneGraphM> ( newMessage ) )
00077 //            {
00078 //                m_SceneGraph = message->getSceneGraph();
00079 //            }
00080 //            break;
00081 //        }
00082 
00083 //    default:
00084 //        break;
00085 //    }
00086 
00087 //    //call processMessage functions of all plugins
00088 //    list<PainterPlugin*>::iterator it = m_Painters.begin();
00089 //    while ( it != m_Painters.end() )
00090 //    {
00091 //        if ( ( *it )->isVisible() )
00092 //        {
00093 //            ( *it )->processMessage ( newMessage );
00094 //        }
00095 //        it++;
00096 //    }
00097 
00098 //    m_Timer.submit();
00099 //}
00100 
00101 void THIS::followNode( const QString& nodeName )
00102 {
00103     m_LookAtNode = nodeName.toStdString();
00104 
00105     //tell painters that node has changed
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     // initial opengl settings
00141     glEnable ( GL_DEPTH_TEST );
00142 }
00143 
00144 void THIS::setProjectionMatrix()
00145 {
00146     //  TRACE_INFO( "Setting projection to opening angle: " << m_CameraOpeningAngle );
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     //   TRACE_ERROR( "resize" + Tracer::toString( w ) );
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         //TRACE_ERROR( "Received 0-pointer!" );
00176         return;
00177     }
00178     m_Painters.push_back ( painter );
00179     m_PaintersMap[painter->getName()] = painter;
00180     painter->setParent( this );
00181 }
00182 
00183 
00184 // DISPLAY ROUTINES //////////////////////////////////////////////////////////////
00185 
00186 void THIS::paintGL()
00187 {
00188     //m_Timer.startMeasure();
00189 
00190     bool redraw = m_ForceRedraw || m_CameraTurning;
00191 
00192     //check if we need to re-draw
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         //     glMatrixMode ( GL_MODELVIEW );
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 ); // TODO
00219 
00220         //rotate around y and Z axis
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         //note: cameraOpposite is needed if m_CameraDistance==0.0
00228 
00229         gluLookAt ( cameraPos.x, cameraPos.y, cameraPos.z, // position of the eye
00230                     cameraOpposite.x, cameraOpposite.y, cameraOpposite.z, // eye position mirrored at 0,0
00231                     0.0, 0.0, 1.0 ); // Up-vector
00232 
00233         if ( m_CameraTurning )
00234         {
00235             float time = 1;//Clock::getInstance()->getTimestamp(); TODO
00236             //time for full turn (sec)
00237             float turnTime = 3.0;
00238             //t runs from 0 to 1 in <turnTime> seconds
00239             float t = ( time - m_CameraTurnStartTime ) / 1000.0 / turnTime;
00240             if ( t > 1.0 ) {
00241                 m_CameraTurning = false;
00242                // TRACE_INFO( "360 degree camera move finished." );
00243             }
00244             else
00245             {
00246                 //tSmooth also runs from 0 to 1, but with smooth acceleration and decceleration
00247                 float tSmooth = -0.5*cos ( t*M_PI ) + 0.5;
00248                 //calculate turn angle from t
00249                 float deltaRad = tSmooth * 2.0 * M_PI;
00250                 glRotatef( deltaRad/M_PI*180.0,0,0,1 );
00251                 // scale the image to make the camera zoom
00252                 float sSmooth = 1 - sin( t * M_PI ) / 2.0;
00253                 glScalef( sSmooth, sSmooth, sSmooth);
00254             }
00255         }
00256         /*
00257     // draw coord system in start point
00258     glLineWidth ( 1.0 );
00259     glBegin ( GL_LINES );
00260     glColor3f ( 0.5, 0.0, 0.0 );
00261     glVertex3f ( 100.0, 0.0, 0.0 );
00262     glVertex3f ( 0.0, 0.0, 0.0 );
00263     glColor3f ( 0.0, 0.5, 0.0 );
00264     glVertex3f ( 0.0, 100.0, 0.0 );
00265     glVertex3f ( 0.0,   0.0, 0.0 );
00266     glColor3f ( 0.0, 0.0, 0.5 );
00267     glVertex3f ( 0.0, 0.0, 100.0 );
00268     glVertex3f ( 0.0, 0.0,   0.0 );
00269     glEnd();
00270 
00271 */
00272         // draw look at point
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 //        if ( !m_SceneGraph.empty() ) // TODO
00285 //        {
00286 //            BaseLib::Math::Mat4d worldToLookAtNode = m_SceneGraph.getTransformation( "World", m_LookAtNode );
00287 //            //TRACE_INFO( worldToLookAtNode.niceString( 1, "worldToLookAtNode" ) );
00288 //            double glMat[16];
00289 //            worldToLookAtNode.toColumnMajor( glMat );
00290 //            glMultMatrixd( glMat );
00291 //        }
00292 
00293         //nextLayer: used to stack elements on floor in y-direction
00294         //to avoid z-buffer problems
00295         float nextLayer = 0.0;
00296 
00297         //     ostringstream s;
00298         //     s << "Paint order:" << endl;
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                 //s << nextLayer << ": " << (*it)->getName() << endl;
00309                 nextLayer += 5.0;
00310             }
00311             it++;
00312         }
00313         //TRACE_INFO( s.str() );
00314 
00315     }
00316     //m_Timer.submit();
00317 }
00318 
00319 
00320 void THIS::performCameraMove()
00321 {
00322     if ( !m_CameraTurning )
00323     {
00324         m_CameraTurning = true;
00325         //m_CameraTurnStartTime = Clock::getInstance()->getTimestamp(); // TODO
00326         //make sure updateGL() is called in short intervals
00327         //TRACE_INFO( "Starting 360 degree camera move." );
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     // save position
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         //make translation axis relative to current camera rotation
00382         translation = BaseLib::Math::rotationZ<double,3>( m_CameraRotationZ/180.0*M_PI ) * translation;
00383         //translate more if we're far away
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 ) // wheel goes up
00402         {
00403             m_CameraOpeningAngle += 5;
00404         }
00405         else // wheel goes down
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 ) // wheel goes up
00423         {
00424             m_CameraDistance = m_CameraDistance/1.05 - 10.0;
00425         }
00426         else // wheel goes down
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 


obj_rec_gui
Author(s): AGAS/agas@uni-koblenz.de
autogenerated on Mon Oct 6 2014 02:53:43