Results 1 to 2 of 2

Thread: converting GLUT/OpenGL to Qt/OpenGL - displaying issue

  1. #1
    Join Date
    Nov 2012
    Posts
    5
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default converting GLUT/OpenGL to Qt/OpenGL - displaying issue

    I am working on converting a pure GLUT/OpenGL/OpenCL code into Qt/OpenGL/OpenCL. For this, I use QGLWidget subclass named "GLWidget" for displaying the animation. My issue is that I would like to keep the GLUT displayfunc working of the first code : in main GLUT loop, I used `glutDisplayFunc(displayfunc);`

    So, in the Qt/OpenGL, I tried to do this :

    Qt Code:
    1. void GLWidget::initializeGL()
    2. {
    3. LoadGLTextures(); // load the textures.
    4. glClearColor(0.0 ,0.0, 0.0, 0.0);
    5. glMatrixMode(GL_PROJECTION);
    6. glLoadIdentity();
    7.  
    8. m_program = _compileProgram(vertexShader);
    9.  
    10. glClampColor(GL_CLAMP_VERTEX_COLOR, GL_FALSE);
    11.  
    12. // memsize of GPU data
    13. unsigned int memSize = sizeof(cl_double4) * 4 * Galaxy->getNumParticles();
    14.  
    15. createVBO(memSize);
    16.  
    17. m_timer = new QTimer(this);
    18. connect(m_timer, SIGNAL(timeout()), this, SLOT(displayfunc()));
    19. m_timer->start(30);
    20. }
    To copy to clipboard, switch view to plain text mode 
    with displayfunc :



    Qt Code:
    1. void GLWidget::displayfunc()
    2. {
    3. if (Galaxy->isFirstLaunch)
    4. {
    5. gluLookAt (0.0, 0.0, 3.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
    6. glScalef(0.03f, 0.03f, 0.03f);
    7. drawPoints();
    8. //Calling kernel for calculatig subsequent positions
    9. Galaxy->runCLKernels();
    10. Galaxy->isFirstLaunch = false;
    11. glFlush();
    12. swapBuffers();
    13. return;
    14. }
    15. else {
    16. clWaitForEvents(1, &Galaxy->glEvent);
    17. drawPoints();
    18. //Calling kernel for calculatig subsequent positions
    19. Galaxy->runCLKernels();
    20. glFlush();
    21. swapBuffers();
    22. }
    23. update();
    24. }
    To copy to clipboard, switch view to plain text mode 

    and drawPoints :

    Qt Code:
    1. void GLWidget::drawPoints()
    2. {
    3. glClear(GL_COLOR_BUFFER_BIT);
    4. glClear(GL_DEPTH_BUFFER_BIT);
    5. glEnable(GL_POINT_SPRITE);
    6. glTexEnvi(GL_POINT_SPRITE, GL_COORD_REPLACE, GL_TRUE);
    7. glEnable(GL_VERTEX_PROGRAM_POINT_SIZE_NV);
    8.  
    9. glEnable(GL_BLEND);
    10. glBlendFunc (GL_SRC_ALPHA, GL_ONE);
    11.  
    12. glUseProgram(m_program);
    13. glUniform1f( glGetUniformLocation(m_program, "pointRadius"), m_particleRadius );
    14. glUniform1f( glGetUniformLocation(m_program, "pointScale"),m_pointScale);
    15.  
    16. GLuint vbo_disk;
    17.  
    18. glBindBuffer(GL_ARRAY_BUFFER, vbo_disk);
    19. glVertexPointer(4, GL_DOUBLE, 4*sizeof(double), Galaxy->pos);
    20. glEnableClientState(GL_VERTEX_ARRAY);
    21.  
    22. glColor4f(1.0f, 1.0f, 1.0f, 0.1f);
    23. glDrawArrays(GL_POINTS, 0, Galaxy->getNumParticles_disk());
    24.  
    25. glBindBuffer(GL_ARRAY_BUFFER, 0);
    26. glDisableClientState(GL_VERTEX_ARRAY);
    27.  
    28. GLuint vbo_halo;
    29.  
    30. glBindBuffer(GL_ARRAY_BUFFER, vbo_halo);
    31. glVertexPointer(4, GL_DOUBLE, 4*sizeof(double), &Galaxy->pos[Galaxy->getNumParticles_disk()]);
    32. glEnableClientState(GL_VERTEX_ARRAY);
    33.  
    34. glColor4f(0.0f, 0.0f, 1.0f, 0.1f);
    35. glDrawArrays(GL_POINTS, 0, Galaxy->getNumParticles_halo());
    36.  
    37. glBindBuffer(GL_ARRAY_BUFFER, 0);
    38. glDisableClientState(GL_VERTEX_ARRAY);
    39.  
    40. glDisable(GL_BLEND);
    41. glDisable(GL_POINT_SPRITE);
    42. }
    To copy to clipboard, switch view to plain text mode 

    Can I do without paintGL() function ?

    I tried to add makeCurrent() into all the functions containing OpenGL functions but nothing displays in GLWidget, just black screen because of `glClearColor(0.0 ,0.0, 0.0, 0.0)`

    Could you see what's wrong ?

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: converting GLUT/OpenGL to Qt/OpenGL - displaying issue

    QGLWidget:: paintGL() is called from the QGLWidget:: paintEvent() handler. Prior to calling this, QGLWidget sets the current GL context (by internally calling makeCurrent()). Implement paintGL() in your GLWidget derived class, and call your displayFunc() directly from that.

    The whole point of using QGLWidget is that it provides its own internal callbacks for the OpenGL initialization, reshape, and display functions. So DO NOT install your own callback functions or make your own GL event loop.

    If you set a breakpoint in your displayFunc method (which apparently you haven't), you will see that in your current implementation it never gets called, because your GLWidget has replaced your glutDisplayFunc callback with its own. So, either move your displayFunc() code into paintGL() or call it from paintGL(). If you do not reimplement paintGL in your own widget, nothing will ever be painted.

    Likewise, you have to do the same with the other gl / glut callbacks - reimplement initializeGL(), resizeGL(), paintGL() (and the overlay versions of all of these if you use overlays), do not use gl / glut callbacks directly because QGLWidget will replace them.

Similar Threads

  1. OpenGL textured quad not displaying correctly
    By vmsgman in forum Qt Programming
    Replies: 6
    Last Post: 4th May 2011, 01:23
  2. QImage displaying through OpenGL
    By strateng in forum Newbie
    Replies: 0
    Last Post: 23rd March 2010, 05:35
  3. Opengl issue with Qt4.4.3
    By j0rt4g4 in forum Newbie
    Replies: 13
    Last Post: 2nd December 2008, 13:36
  4. Replies: 7
    Last Post: 27th June 2007, 10:34
  5. OpenGl Issue
    By vermarajeev in forum Qt Programming
    Replies: 6
    Last Post: 7th April 2007, 06:28

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.