Results 1 to 8 of 8

Thread: QT + OpenGL + Thread => aaaahhhhh !

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jul 2008
    Posts
    10
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default QT + OpenGL + Thread => aaaahhhhh !

    Hi, I've got a problem with threads and OpenGL in QT.
    Could you help me please ?

    I'm currently using QT 4.4 with C++.
    In my program, I've got a class which is subclassing the GL widget : " class gldrawer : public QGLWidget ".
    Everything works well : i'm drawing a cloud of points, the maximum number i tried with my program was 31 000 000
    See attached file interface.jpg to get an idea...

    I'm a student with not enough experience in the QT domain but I managed to implement a free fly camera, into my class gldrawer.
    That is probably not the best way to do it but it was easier for me to do it that way to begin with Qt + opengl.

    The surface is updated ONLY when a user interact, for example when the user push the forward button or change colors... => CPU is at 0% most of the time.
    To have a fluid animation during moving phases (user keeps pushing forward for example), I managed to draw the scene indefinitely => CPU at 100% on one core.
    I did that with a timer and a 0ms delay : " qTimerRedraw.start(0); ". The main reason was in fact my lack of knowledge with QThread at the time (I was just beginning with Qt!).

    The job is done, user can interact with opengl & GUI buttons in the same time, the event is just add to the event list so its sometimes a little lagging, but at least it's no crashing.
    (I've got more lags with QSliders when the opengl scene is huge and long to render...). I know this is not perfect, but it kinda works.


    Now I'm trying to launch an automated animation : you click a button and the camera have to follow a list of points, indefinitely. You reclick and it stops the camera.
    My idea was to create a thread to run to the points and replace the camera at each step ; this way the loop is not in the same thread than the GUI => no freeze, can reclick to stop the camera.

    As you know, QT + OpenGL + Threads = complex situation !
    Moreover the Qt documentation isn't enough clear about that, just a word about makeCurrent() but no example... I don't know how to separate my classes...


    Whatever I did some tests and this is the best I've come to :
    -I launch a thread when I click on the button, the thread is corerctly terminated when I reclick
    -Inside this thread, the loop is correctly executed, calculations for the camera are OK :
    I've got a pointer to my gldrawer object and i call a public method of it with the new camera coordinates : this->mygl->setPosition(current, cible); where this->mygl is a gldrawer * obtain in the constructor.

    As you notice, setPosition can be called from 2 different thread at the same time.... And guess what, setPosition is calling this->updateGL()...


    The code compile and run... But nothing happend when I click the button to launch the 'camera' thread. Well in fact the code is executed (new thread, loop, setposition, ...) , values are updated, ... but the screen is not refreshed

    Why ?

    I tried to lock the GL Context with makeCurrent(); and a mutex, like in the following code, but without success.

    //-------------
    I've found a multithreaded opengl + qt example right there : http://apsy.gse.uni-magdeburg.de/mai...e/threadedcube
    It's a great example, it works well. Very interesting.
    //-------------

    Thanks for taking time.
    Attached Images Attached Images

Similar Threads

  1. QTimer ->start(0) + OpenGL + resize/move window => crash
    By anthibug in forum Qt Programming
    Replies: 5
    Last Post: 8th July 2008, 11:01
  2. KDE/QWT doubt on debian sarge
    By hildebrand in forum KDE Forum
    Replies: 13
    Last Post: 25th April 2007, 06:13
  3. Replies: 10
    Last Post: 20th March 2007, 22:19
  4. Problem closing a QMainWindow in Qt4.2
    By ian in forum Qt Programming
    Replies: 11
    Last Post: 17th October 2006, 00:49

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.