Results 1 to 8 of 8

Thread: QT + OpenGL + Thread => aaaahhhhh !

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,017 Times in 4,793 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QT + OpenGL + Thread => aaaahhhhh !

    updateGL has to be called in the main thread. And of course you realize that by using threads you slow down your application, right?

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

    Default Re: QT + OpenGL + Thread => aaaahhhhh !

    Quote Originally Posted by wysota View Post
    updateGL has to be called in the main thread.
    Flashbang !
    Ok, that is the thing !
    So stupid it was not clear for me...

    I absolutely know i'm slowing my application by using mutex.
    But i really don't care here, it's just for launching a camera motion move and having the ability to play/pause that.

    //------------------------------------

    This morning I did a test : using a timer into gldrawer, with all the code into the gldrawer `thread`. It did works.
    But... I can't stop the animation until it's finished.
    Qt Code:
    1. void gldrawer::moveCamera()
    2. {
    3. int nb = number of key points (BIG)
    4. for(int i=0; i < nb; i++)
    5. {
    6. go to the next point
    7. draw()
    8. }
    9. }
    10.  
    11.  
    12. connect(&qMoveRedraw, SIGNAL(timeout()), this, SLOT(moveCamera()));
    13. qMoveRedraw.start(30);
    To copy to clipboard, switch view to plain text mode 
    i simplify the moveCamera() code but the idea is here... takes time !
    as soon as i resize or even click in the window : crash, and that's normal...


    //------------------------------------

    ok back to the beginning of my message :
    i just did some tests and i manage to get what i need

    In fact i just tell my gldrawer to draw continously meanwhile the second thread do stuff !

    I also did some tests with mutex & QWaitCondition but i think i will use sleep rather than mutex.

    Thanks again for your help !

  3. #3
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,017 Times in 4,793 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QT + OpenGL + Thread => aaaahhhhh !

    Quote Originally Posted by anthibug View Post
    I absolutely know i'm slowing my application by using mutex.
    No, by using threads... Contrary to what people think threads tend to slow down execution of code, not speed it up. Unless of course you have more processing units than threads in your application and no other cpu consuming task is running.


    But i really don't care here, it's just for launching a camera motion move and having the ability to play/pause that.
    What do you use threads for, anyway?

    In fact i just tell my gldrawer to draw continously meanwhile the second thread do stuff !
    Why should it draw all the time? It doesn't make sense as the movement (not redrawing) depends on the other tasks running on your machine. You should first run a benchmark to determine how fast you can draw and then adjust the timer so that you have a constant frequency of the "game loop" and redraw as often as you know is possible.

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

    Default Re: QT + OpenGL + Thread => aaaahhhhh !

    Quote Originally Posted by wysota View Post
    No, by using threads... Contrary to what people think threads tend to slow down execution of code, not speed it up. Unless of course you have more processing units than threads in your application and no other cpu consuming task is running.
    I completely agree, you misunderstand me !
    Re-read what i said : i'm slowing my application
    Not solving, slowing ! Sorry for my english...



    Quote Originally Posted by wysota View Post
    Quote Originally Posted by TiTi
    But i really don't care here, it's just for launching a camera motion move and having the ability to play/pause that.
    What do you use threads for, anyway?
    Yep that was not really explicit, give me another chance :
    -my gldrawer is in the same thread than the gui
    -i want to launch a processing to take screenshots from A point to B point into the cloud points scene
    -if i do that from the `main` thread, i'm freezeing my application : no buttons anymore, no resize, etc... or crash
    I've to wait until the end of animation and cross finger that a crash doesn't happend cause of events.

    => so my idea was to create a second thread from which the translation of the camera is done in an infinite loop


    Quote Originally Posted by wysota View Post
    Why should it draw all the time? It doesn't make sense as the movement (not redrawing) depends on the other tasks running on your machine. You should first run a benchmark to determine how fast you can draw and then adjust the timer so that you have a constant frequency of the "game loop" and redraw as often as you know is possible.
    -it's not a game but i think you know that
    -why draw all the time : because, as you said : "updateGL has to be called in the main thread"
    =>with the second thread looping and moving the camera, the only way to refresh the screen was to draw from the first thread.
    I'm also re-saying : i don't care to use a timer with a 0ms delay that take 100% of one core of my CPU.

    -in the latest modifications, i'm using a QMutex and a QWaitCondition* in a way that the second thread wait that the first thread do all the painting [wakeAll() after updateGL()]

    Of course the animation is not perfectly fluid, but i don't care because i just need to take the screenshot of the gl surface at every frame , i'm going to recompose a video from the captures. (I'm doing that because the scene is huuuuuuuuuuuge). [yes i know i didn't clearly explain all of that, but i aldready said a lot...]

    So this way i'm minimazing the call in terms of 'redrawing'...


    Sure it appears pretty messed up from your eye, and i'm far from beeing a pro at QT+OpenGL+Huge scenes... Even more far from beeing a good teller..
    So as a novice, who knew nothing a few weeks ago, it was hard to figure it out and explain my problem(s) but I manage to resolve them.

    I'm assuring you this thread animation thing is just one of the functionnalities ; it's not impacting the viewer in itself, with the free fly camera.



    *about that i can't find QWaitCondition in the assistant, only found here : http://doc.trolltech.com/4.4/qwaitcondition.html

  5. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,017 Times in 4,793 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QT + OpenGL + Thread => aaaahhhhh !

    Quote Originally Posted by anthibug View Post
    -i want to launch a processing to take screenshots from A point to B point into the cloud points scene
    -if i do that from the `main` thread, i'm freezeing my application : no buttons anymore, no resize, etc... or crash
    I've to wait until the end of animation and cross finger that a crash doesn't happend cause of events.
    It won't freeze as long as you let the application process its events from time to time. Make a single shot at a time instead of trying to make all at once in one long loop.

    -it's not a game but i think you know that
    It's still called a game loop.

    -why draw all the time : because, as you said : "updateGL has to be called in the main thread"
    But why all the time? If nothing changes, what's the point of redrawing the screen? If the content is already stale, drop the frame and continue calculations, no need to redraw the display again.

    You surely have some function that changes the position of the camera that you call periodically - that's the "game loop". To have a steady movement it is important that you call the loop in regular intervals, otherwise your animation will get messed up. So call the loop every 40ms or so to get 25 frames per second, recalculate the position there, call updateGL if you want to redraw the display and make a shot. After you do that, return the flow to the event loop and let the timer timeout again 40ms-<time you spent on calculations/redrawing/snapshotting> later. All that in one thread and in one function call. If your computer cannot keep up, slow down the loop (have less fps but increase the step to compensate the animation).

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
  •  
Qt is a trademark of The Qt Company.