How to draw opengl graphics from different threads?

后端 未结 3 2034
难免孤独
难免孤独 2021-01-13 15:12

I want to make an opengl application that shows some 3d graphics and a command-line. I would like to make them separate threads, because they both are heavy processes. I tho

相关标签:
3条回答
  • 2021-01-13 15:48

    I think, rather than trying to create two threads to draw to the screen, you need to use the MVC pattern and make your model thread-safe. Then you can have one thread that grabs the necessary info from the model each frame and throws it on screen, and then two other threads, one for the graphics and one for the command-line, which manage only the model.

    So for instance, you have a Simulation class that has your 3D graphics stuff, and then a CommandLine class that has your command line. Each of these classes does not use OpenGL at all; only manages the data, such as where things are in 3d-space, and in the case of the command-line a queue of the lines on-screen. Then the opengl thread can query thread-safe functions of these classes each frame, to get the necessary info. So for example, it grabs the positions of the 3d things, draws them on-screen, then grabs the lines to display on the command line and draws them.

    0 讨论(0)
  • 2021-01-13 15:50

    You can't do it with 2 viewports.

    Each OpenGL context must be used from the same thread in which it was created.

    Two separate window handles, each with their own context, can be setup and used from two separate threads safely, but not 2 viewports in one window.


    A good place to look at ideas for this is OpenSceneGraph. It does a lot of work with threading to help try to speed up handling and maintain a constant, high framerate with very large scenes.

    I've successfully embedded it and used it from 2 separate threads, but OSG has many checks in place to help with this sort of scenario.

    0 讨论(0)
  • 2021-01-13 15:59

    Its possible you can achieve what you want to do using Overlays.
    Overlays are a somewhat dated feature but it should still be supported in most setups. Basically an overlay is a separate GL Context which is rendered in the same window as another layer, drawing on top of whatever was drawn on the windows with its original context.
    You can read about it here.

    0 讨论(0)
提交回复
热议问题