C:/Projekte/C++/FlowVIS_107/src/OpenGLWidget.cpp

Go to the documentation of this file.
00001 #include "OpenGLWidget.h"
00002 #include <iostream>
00003 #include <QColorDialog>
00004 #include <QMouseEvent>
00005 #include <QColor>
00006 
00007 using namespace std;
00008 
00009 OpenGLWidget::OpenGLWidget(QWidget* parent /*= 0*/, Qt::WFlags flags /*= 0*/) : QGLWidget(parent)
00010 {
00011   setupUi(this);
00012   m_parent = parent;
00013   cout << "Creating OpenGLWidget..." << endl;
00014 
00016   m_isActiveBackground = false;
00017   m_isActiveArrows = false;
00018 
00019   m_isActiveStreamlines = false;
00020   m_isActiveTransferFunction = false;
00021 
00023   m_clearColor = QColor(Qt::black);
00024   m_flowData = NULL;
00025 
00026   m_librariesInitialised = false;
00027   m_isFileOpen = false;
00028   m_aspectRatio = 1.0f;
00029 
00030   m_streamLine = NULL;
00031 
00033   m_defaultOrtho.left = 0.0;
00034   m_defaultOrtho.right = 1.0;
00035   m_defaultOrtho.bottom = 1.0;
00036   m_defaultOrtho.top = 0.0;
00037 
00038   // nk - set current view to default at startup/reset
00039   m_currentOrtho = m_defaultOrtho;
00040 
00041   // generate textureIDs for backgroundTexture
00042   //glGenTextures(max_channels, m_backgroundTexID);
00043 }
00044 
00045 
00046 
00047 OpenGLWidget::~OpenGLWidget()
00048 {
00049 }
00050 
00051 void OpenGLWidget::initializeGL()
00052 {
00053         cout << "initializeGL... " << endl;
00054         // qDebug() << "init";
00055 
00056   if ( !m_librariesInitialised )
00057   {
00058           initGlew();  // Initialize Glew Extension Wrangler
00059           initDevIL(); // Initialize Image Library Lib (for textures)
00060 
00061     if ( !glewIsSupported( "GL_VERSION_2_0" ) )
00062     {
00063       cout << "OpenGL-Version 2.0 not supported!" << endl;
00064       exit( 1 );
00065     }
00066 
00067     m_librariesInitialised = true;
00068   }
00069 
00070   // load shader
00071   m_colorLookupShader = new Shader();
00072   m_colorLookupShader->Load("colorLookup.vert", "colorLookup.frag");
00073 
00075   m_tfTextureLocation = glGetUniformLocation(m_colorLookupShader->GetProgram(), "tfTexture");
00076   m_bgTextureLocation = glGetUniformLocation(m_colorLookupShader->GetProgram(), "bgTexture");
00077   m_showTfLocation = glGetUniformLocation(m_colorLookupShader->GetProgram(), "showTF");
00078 
00080   m_colorLookupShader->Bind();
00081   glUniform1i( m_bgTextureLocation, 0); // nk - set bg texture to texture unit 0
00082   glUniform1i( m_tfTextureLocation, 1 ); // nk - set tf texture to texture unit 1
00083   m_colorLookupShader->Unbind();
00084 
00086   m_arrows = new Arrows(this);
00087 
00088   // set clear color
00089   qglClearColor( m_clearColor );
00090   glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
00091 }
00092 
00093 void OpenGLWidget::resizeGL(int width, int height)
00094 {
00095   printf("Resize GL\n");
00096 
00097   m_windowWidth = width;
00098   m_windowHeight = height;
00099 
00100   setDataView();
00101 }
00102 
00103 void OpenGLWidget::resetDataView()
00104 {
00105   setDataView(true);
00106   this->update();
00107 }
00108 
00109 void OpenGLWidget::setDataView(bool reset)
00110 {
00111   // set aspect ratio of viewport
00112   if(m_isFileOpen)
00113   {
00114     if(m_dataDimY <= 0)
00115     {
00116       printf("m_dataDimY invalid\n");
00117       exit(-1);
00118     }
00119 
00120     //TODO: WindowAspect
00121     m_aspectRatio = m_dataDimX / (float) m_dataDimY;
00122 
00123     if(m_aspectRatio < 1.0)
00124     {
00125       int centerX = m_windowWidth / 2;
00126       glViewport(centerX - m_dataDimX / 2, 0, m_windowWidth * m_aspectRatio, m_windowHeight);
00127     }
00128     else
00129     {
00130       glViewport(0, 0, m_windowWidth, m_windowHeight);
00131     }
00132 
00134     if(reset == true)
00135     {
00136       printf("Reset DataView\n");
00137       m_currentOrtho = m_defaultOrtho;
00138     }
00139 
00140     glMatrixMode(GL_PROJECTION);
00141     glLoadIdentity();
00142     gluOrtho2D(m_currentOrtho.left, m_currentOrtho.right, m_currentOrtho.bottom, m_currentOrtho.top);
00143 
00144     //gluOrtho2D(0.0, 1.0, 1.0, 0.0); // because of left-handed y-axes, flip view!
00145     //was: gluOrtho2D(0.0, 1.0, 0.0, 1.0);
00146 
00147     glMatrixMode(GL_MODELVIEW);
00148     glLoadIdentity ();
00149   }
00150   else
00151   {
00152     glViewport(0, 0, m_windowWidth, m_windowHeight);
00153 
00154     glMatrixMode(GL_PROJECTION);
00155           glLoadIdentity();
00156 
00157     glMatrixMode(GL_MODELVIEW);
00158     glLoadIdentity ();
00159   }
00160 }
00161 
00162 void OpenGLWidget::setActiveBackground(int bg)
00163 {
00164   if(!m_isFileOpen)
00165     return;
00166 
00167   printf("SetBackground\n");
00168 
00169   int bgCount = m_flowData->getScalarChannelCount();
00170 
00171   if(bg >= bgCount) // background: none
00172   {
00173     m_isActiveBackground = false;
00174     m_arrows->setActiveBackground(-1);
00175     this->update();
00176   }
00177   else
00178   {
00179     m_isActiveBackground = true;
00180     m_activeBackground = bg;
00181     m_arrows->setActiveBackground(bg);
00182     this->update();
00183   }
00184 }
00185 
00186 
00188 void OpenGLWidget::setColorBarTexture(QGradientStops stops)
00189 {
00190   printf("setColorBarTexture\n");
00191   Texture texture;
00192   glGenTextures(1, &m_colorBarTexID);
00193   glBindTexture(GL_TEXTURE_1D, m_colorBarTexID);
00194   m_colorBarTexID = texture.CreateColorBar(stops);
00195   printOglError("CreateColorBar");
00196 
00197   update();
00198 }
00199 
00200 void OpenGLWidget::updateStreamLines()
00201 {
00202   printf("Update Streamlines\n");
00203 
00205   if(m_isActiveStreamlines)
00206   {
00207     m_streamLine->create();
00208     this->update();
00209   }
00210 }
00211 
00212 void OpenGLWidget::selectStreamLineColor()
00213 {
00214   printf("Select Streamline Color\n");
00215 
00216   QColor white = QColor(Qt::white);
00217   QColor color;
00218 
00219   color = QColorDialog::getColor(white);
00220 
00221   if(!color.isValid())
00222   {
00223     printf("New Streamline color not valid - set to white\n");
00224     color = white;
00225   }
00226   float slColor[4];
00227 
00228   slColor[0] = (float) color.redF();
00229   slColor[1] = (float) color.greenF();
00230   slColor[2] = (float) color.blueF();
00231   slColor[3] = (float) color.alphaF();
00232 
00233   // nk - set color in streamline
00234   m_streamLine->setLineColor(slColor);
00235 }
00236 
00237 void OpenGLWidget::setStreamGlyphSize(int gs)
00238 {
00240   m_streamLine->setGlyphSize(gs);
00241 }
00242 
00243 void OpenGLWidget::setStreamGlyphDistance(int gd)
00244 {
00246   m_streamLine->setGlyphDistance(gd); 
00247 }
00248 
00249 void OpenGLWidget::setEuler()
00250 {
00251   m_streamLine->setIntegrationMethod(EULER);
00252 }
00253 
00254 void OpenGLWidget::setRungeKutta()
00255 {
00256   m_streamLine->setIntegrationMethod(RUNGE_KUTTA2);
00257 }
00258 
00259 
00260 void OpenGLWidget::selectStreamGlyphColor()
00261 {
00262   printf("Select Streamline Glyph Color\n");
00263 
00264   QColor white = QColor(Qt::white);
00265   QColor color;
00266 
00267   color = QColorDialog::getColor(white);
00268 
00269   if(!color.isValid())
00270   {
00271     printf("New Streamline Glyph Color not valid - set to white\n");
00272     color = white;
00273   }
00274 
00275   float slColor[4];
00276 
00277   slColor[0] = (float) color.redF();
00278   slColor[1] = (float) color.greenF();
00279   slColor[2] = (float) color.blueF();
00280   slColor[3] = (float) color.alphaF();
00281 
00282   m_streamLine->setGlyphColor(slColor);
00283 }
00284 
00285 
00286 void OpenGLWidget::createStreamLines()
00287 {
00288   cout << "createStreamLines..." << endl;
00289 /*
00290   // create streamlines
00291   if ( m_flowData != NULL ) 
00292   {
00293     m_streamLine = new StreamLine( m_flowData );
00294     m_streamLine->create();
00295   }
00296 */
00297 }
00298 
00299 
00300 void OpenGLWidget::setFlowData(FlowData* data)
00301 {
00302   m_flowData = data;
00303 
00304   // nk - moved into 
00305   // Init flow textures
00306   // if(m_flowData != NULL) ...
00308 
00309   //if ( m_flowData == NULL && m_streamLine != NULL )
00310   //{
00311   //  m_streamLine->reset();
00312   //}
00313   //else // if ( m_flowData != NULL ) <-- else error
00314   //{
00315   //  // throws exception at file close when nothing is openend
00316   //  m_streamLine = new StreamLine( m_flowData );
00317   //  //m_streamLine->create(); // via update button
00318   //}
00319 
00320   Texture texture;
00321 
00322   // Init flow textures
00323   if(m_flowData != NULL)
00324   {
00325     m_isFileOpen = true;
00326 
00328     m_geomData = m_flowData->getGeometry();
00329 
00331     m_dataDimX = m_geomData->getDimX();
00332     m_dataDimY = m_geomData->getDimY();
00333 
00335     m_arrows->setFlowData(data);
00336 
00338     setDataView();
00339 
00341     if(m_streamLine == NULL)
00342     {
00343       m_streamLine = new StreamLine(m_flowData);
00344     }
00345     else
00346     {
00347       m_streamLine->reset();
00348     }
00349 
00350     int bgTexture = 0;
00351 
00352     // TODO: Texture generation for scalar data
00353     for(int i = 0; i < data->getChannelCount(); i++)
00354     {
00356       if(data->getIsScalar(i) == NORMALIZED)
00357       {
00358         printf("ChannelCount: %d, Channel(%d): Background\n",data->getChannelCount(), i);
00359         texture.CreateBackground(data->getChannel(i), m_dataDimX, m_dataDimY, m_backgroundTexID, bgTexture);
00360         bgTexture++;
00361       }
00362     }
00363   }
00364   else
00365   {
00366     m_isFileOpen = false;
00367     int bgTexture = 0;
00368 
00369     delete m_streamLine;
00370     m_streamLine = NULL;
00371 
00372     // TODO: delete textures
00373     for(int i = 0; i < max_channels; i++)
00374     {
00375        texture.Delete(i);
00376     }
00377 
00378   }
00379 }
00380 
00381 void OpenGLWidget::setActiveArrows(bool gl)
00382 {
00383   m_isActiveArrows = gl;
00384   this->update();
00385 }
00386 
00387 void OpenGLWidget::setActiveTransferFunction(bool tf)
00388 {
00389   m_isActiveTransferFunction = tf;
00390   this->update();
00391 }
00392 
00393 void OpenGLWidget::setLinesActive(bool isActive)
00394 {
00396   m_isActiveStreamlines = isActive;
00397   m_streamLine->setLinesActive(m_isActiveStreamlines);
00398   if(isActive)
00399   {
00400     m_streamLine->create();    
00401   }
00402 
00403   this->update();
00404 }
00405 
00406 void OpenGLWidget::setDSep( int dSep )
00407 {
00409   float value = dSep / (float) MAX_SLIDER_VALUE;
00410   m_streamLine->setDSep(value);
00411 
00412   //printf("dsep: %f\n", value); nk - values ok
00413 }
00414 void OpenGLWidget::setDTest( int dTest )
00415 {
00417   float value = dTest / (float) MAX_SLIDER_VALUE;
00418   m_streamLine->setDTest(value);
00419 
00420   //printf("dtest: %f\n", value); // nk - values ok
00421 }
00422 
00423 void OpenGLWidget::setTaperingActive( bool isActive )
00424 {
00425   m_streamLine->setTaperingActive(isActive);
00426   this->update();
00427 }
00428 void OpenGLWidget::setGlyphActive( bool isActive )
00429 {
00430   m_streamLine->setGlyphActive(isActive);
00431   this->update();
00432 } 
00433 
00434 void OpenGLWidget::drawBackground()
00435 {
00437   glActiveTexture(GL_TEXTURE0);
00438   glEnable(GL_TEXTURE_2D);
00439   glBindTexture(GL_TEXTURE_2D, m_backgroundTexID[m_activeBackground]);
00440   glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_NONE);
00441   
00443   glActiveTexture(GL_TEXTURE1);
00444   glEnable(GL_TEXTURE_1D);
00445   glBindTexture(GL_TEXTURE_1D, m_colorBarTexID);
00446   glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_NONE);
00447   
00448   glPushMatrix();
00449 
00450   vec3 pos;
00451   int index;
00452   float texX, texY;
00453   
00454   // use shader program
00455   m_colorLookupShader->Bind();
00456 
00458     glUniform1i(m_showTfLocation, m_isActiveTransferFunction);
00459 
00462     for(int y = 1; y < m_geomData->getDimY(); y++)
00463     {
00464       glBegin(GL_TRIANGLE_STRIP);
00465       //glBegin(GL_POINTS); /// shows grid
00466       for(int x = 0; x < m_geomData->getDimX(); x++)
00467       {
00469         index = y * m_dataDimX + x;
00470 
00471         // nk - get normalized position from geometry data array
00472         pos = m_geomData->getNormPos(index);
00473         
00474         // nk - texX stays constant for this and 2nd index
00475         texX = x / (float) (m_geomData->getDimX() - 1);
00476 
00477         // nk - texture generation must be ok - but assigning to grid was wrong indeed
00478         texY = y / (float) (m_geomData->getDimY() - 1);
00479         glTexCoord2f(texX, texY);
00480         glVertex2fv(pos.v);
00481 
00483         index = (y-1) * m_dataDimX + x;
00484         pos = m_geomData->getNormPos(index);
00485         texY = (y-1) / (float) (m_geomData->getDimY() - 1);
00486         //nk - wrong glTexCoord2f(pos[X], pos[Y]);
00487         glTexCoord2f(texX, texY);
00488         glVertex2fv(pos.v);
00489       }
00490       glEnd();
00491     }
00492   
00493   m_colorLookupShader->Unbind();
00494 
00495   glDisable(GL_TEXTURE_1D);
00496   glDisable(GL_TEXTURE_2D);
00497 
00498   glPopMatrix();
00499 }
00500 
00501 void OpenGLWidget::paintGL()
00502 {
00503   glClear( GL_COLOR_BUFFER_BIT );
00504   glDisable( GL_DEPTH_TEST );
00505 
00506   // do nothing if no file loaded
00507   if ( !m_isFileOpen )
00508     return;
00509 
00510   glColor3f(0.0f, 0.0f, 0.0f);
00511 
00512   //TEST: enable background texture
00513   if ( m_isActiveBackground )
00514   {
00515     drawBackground();
00516   }
00517 
00518   if ( m_isActiveArrows )
00519   {
00520     m_arrows->draw();
00521   }
00522 
00523   // gv show for streamlines
00524   if ( m_isActiveStreamlines )
00525   {
00526     m_streamLine->draw();
00527   }
00528 
00529 }
00530 
00531 
00532 
00533 void OpenGLWidget::wheelEvent(QWheelEvent *we)
00534 {
00535   //nk - use mouse wheel for zoom
00536   float zoom = 0.0f;
00537 
00538   if(we->delta() > 0) // zoom in
00539   {
00540     zoom = 0.01f;
00541 
00542   }
00543   else // zoom out
00544   {
00545     zoom = -0.01f;
00546   }
00547 
00548   // nk - add zoom to current ortho matrix
00549   m_currentOrtho.left   += zoom;
00550   m_currentOrtho.right  -= zoom;
00551   m_currentOrtho.bottom -= zoom;
00552   m_currentOrtho.top    += zoom;
00553 
00554   // nk - update view
00555   setDataView();
00556   this->update();
00557 
00558   //printf("Zoom: %f\n", zoom);
00559 }
00560 
00561 void OpenGLWidget::mouseMoveEvent(QMouseEvent* me)
00562 {
00563   float panX = 0.01f;
00564   float panY = 0.01f;
00565 
00566   // nk - left button -> panX
00567   if(m_panXButton == Qt::LeftButton)
00568   {
00569     if(me->x() > m_currentMousePos.x())
00570     {
00571       m_currentOrtho.left += panX;
00572       m_currentOrtho.right += panX;
00573     }
00574     else
00575     {
00576       m_currentOrtho.left -= panX;
00577       m_currentOrtho.right -= panX;
00578     }
00579 
00581     setDataView();
00583     this->update();
00584   }
00585   else 
00586     if (m_panYButton == Qt::RightButton)
00587     {
00588       if(me->y() > m_currentMousePos.y())
00589       {
00590         m_currentOrtho.bottom += panY;
00591         m_currentOrtho.top += panY;
00592       }
00593       else
00594       {
00595         m_currentOrtho.bottom -= panY;
00596         m_currentOrtho.top -= panY;
00597       }
00598       
00600       setDataView();
00602       this->update();
00603     }
00604 }
00605 
00606 void OpenGLWidget::mousePressEvent(QMouseEvent *pe)
00607 {
00608   // nk - use left button for panX
00609   if(pe->button() == Qt::LeftButton)
00610   {
00611     m_panXButton = pe->button();
00612     m_currentMousePos = pe->pos();
00613   }
00614 
00615   // nk - use right button for panY
00616   if(pe->button() == Qt::RightButton)
00617   {
00618     m_panYButton = pe->button();
00619     m_currentMousePos = pe->pos();
00620   }
00621 }
00622 
00623 void OpenGLWidget::mouseReleaseEvent(QMouseEvent* re)
00624 {
00625   // nk - release button
00626   m_panXButton = Qt::NoButton;
00627   m_panYButton = Qt::NoButton;
00628 }
00629 
00630 void OpenGLWidget::initGlew()
00631 {
00632         // Initialize GLEW
00633         cout << "- Initializing GLEW ..." << endl;
00634         GLenum err = glewInit();
00635 
00636         if (err != GLEW_OK)
00637         {
00638                 // glewInit failed, something is seriously wrong
00639                 cerr << "Error initializing GLEW: " << glewGetErrorString(err) << endl;
00640                 return;
00641         }
00642         cout << "- GLEW initialized." << endl << endl;
00643 }
00644 
00645 void OpenGLWidget::initDevIL()
00646 {
00647   // Initialize DevIL (an image library)
00648         cout << "- Initializing DevIL ..." << endl;
00649         ilInit();
00650 
00651         if (ilGetError() != IL_NO_ERROR)
00652         {
00653                 cerr << "Error initializing DevIL" << endl;
00654                 return;
00655         }
00656 
00657         ilEnable(IL_ORIGIN_SET);
00658         ilOriginFunc(IL_ORIGIN_LOWER_LEFT);
00659         cout << "- DevIL initialized." << endl << endl;
00660 }

Generated on Mon Jan 21 14:50:12 2008 for VisLU by  doxygen 1.5.4