Hi All,
In my program I am using more that on GLWidget and all works fine but rendering is too slow.
Now I am trying to speed-up the rendering of a scene by using a thread to calculate the vertex list of one of the GLWidgets. It works but sometimes I see artifacts in the image and after about 30 seconds it always crashes the app. I read here (http://doc.qt.digia.com/qq/qq06-glim...glapplications) that it should be possible to do that but so far I am not able to get stable results. The thread gets called by the render() function to pass in an image which is the basis for the calculation of the vertex list. This happens about 30x per second.
There is also another thread (http://www.qtcentre.org/threads/3631...d-GUI-possible) which mentions that this should work but it does not.
Here is the code Thread I wrote:

Qt Code:
  1. class GLWaveformThread : public QThread
  2. {
  3. Q_OBJECT
  4. public:
  5. explicit GLWaveformThread(WaveformGLWidget *glWidget);
  6. ~GLWaveformThread();
  7. void render(const Img::Image &image);
  8.  
  9. protected:
  10. void run();
  11.  
  12. private:
  13. QMutex mutex;
  14. QWaitCondition condition;
  15. bool restart;
  16. bool abort;
  17. QImage fPix;
  18. WaveformGLWidget *myGLW;
  19. };
  20.  
  21. GLWaveformThread::GLWaveformThread(WaveformGLWidget *glWidget) :
  22. QThread(), myGLW(glWidget)
  23. {
  24. restart = false;
  25. abort = false;
  26. }
  27.  
  28. void GLWaveformThread::render(QImage &image)
  29. {
  30. QMutexLocker locker(&mutex);
  31.  
  32. if(image.isEmpty())
  33. return;
  34.  
  35. fPix = image;
  36.  
  37. if (!isRunning()) {
  38. start(LowPriority);
  39. } else {
  40. restart = true;
  41. condition.wakeOne();
  42. }
  43. }
  44.  
  45. void GLWaveformThread::run()
  46. {
  47. forever {
  48. mutex.lock(); //-----------
  49. QImage in = fPix;
  50. myGLW->makeCurrent();
  51. glDeleteLists(myGLW->vectors(), 1);
  52. glNewList(myGLW->vectors(), GL_COMPILE);
  53. int w = fPix.width();
  54. int h = fPix.height();
  55. mutex.unlock(); //-----------
  56.  
  57. if (abort)
  58. return;
  59.  
  60. glColor3f(.2, .1, .1);
  61. for(int y = 0; y < h; y++)
  62. {
  63. glBegin(GL_LINE_STRIP);
  64. for(int x=0; x < w; x++)
  65. glVertex3f(...); // draw some lines
  66.  
  67. glEnd();
  68. }
  69. glEndList();
  70.  
  71. mutex.lock(); //-----------
  72. myGLW->updateGL();
  73. if (!restart)
  74. condition.wait(&mutex);
  75. restart = false;
  76. mutex.unlock(); //-----------
  77. }
  78. }
To copy to clipboard, switch view to plain text mode 

This is the code for the widget:


Qt Code:
  1. void WaveformGLWidget::initializeGL()
  2. {
  3. glClearColor(0.0, 0.0, 0.0, 0.0);
  4. glClearAccum(0.0, 0.0, 0.0, 0.0);
  5. myVectors = glGenLists(1);
  6. }
  7.  
  8. void WaveformGLWidget::resizeGL(int width, int height)
  9. {
  10. glViewport(0, 0, width, height);
  11. glMatrixMode(GL_PROJECTION);
  12. glLoadIdentity();
  13. glMatrixMode(GL_MODELVIEW);
  14. glLoadIdentity();
  15. }
  16.  
  17. void WaveformGLWidget::paintGL()
  18. {
  19. glPushMatrix();
  20. makeCurrent();
  21. glScalef(.6, 1.9, 1.0);
  22. glTranslatef(-1.5,-.5,0.);
  23. glClear(GL_COLOR_BUFFER_BIT);
  24. glBlendFunc(GL_ONE, GL_ONE);
  25. glEnable(GL_BLEND);
  26.  
  27. glCallList(myVectors);
  28. glDisable(GL_BLEND);
  29.  
  30. glPopMatrix();
  31. }
To copy to clipboard, switch view to plain text mode 

Thanks in advance
Markus