【问题标题】:OpenTK multithreading?OpenTK 多线程?
【发布时间】:2016-03-18 16:27:59
【问题描述】:

有没有办法在另一个线程中使用 OpenTK/OpenGL 绘制到屏幕上? 我尝试使用的代码是

GL.Clear(ClearBufferMask.ColorBufferBit);
GL.ClearColor(Color.Black);

GL.Begin(PrimitiveType.Quads);

GL.Color3(Color.FromArgb(255, 0, 0));
GL.Vertex2(-1, 1);
GL.Color3(Color.FromArgb(0, 255, 0));
GL.Vertex2(1, 1);
GL.Color3(Color.FromArgb(0, 0, 255));
GL.Vertex2(1, -1);
GL.Color3(Color.FromArgb(0, 255, 255));
GL.Vertex2(-1, -1f);

GL.End();
SwapBuffers();

上面的代码在创建 GameWindow 的同一个线程中工作,但在从另一个线程调用时不工作。

【问题讨论】:

  • 一个简单的答案是:你不能。见stackoverflow.com/questions/11097170/…
  • 它被要求从另一个线程渲染,而不是以多线程方式。是的,移动上下文或在另一个线程中创建新上下文将产生所需的结果。 (注意资源共享,因为某些 opengl 资源仅存在于每个上下文中)

标签: c# multithreading opengl opentk


【解决方案1】:

我能够用这段代码交换 OpenGL 接受的线程

thread = new Thread(() =>
{
    IGraphicsContext context = new GraphicsContext(GraphicsMode.Default, window.WindowInfo);
    context.MakeCurrent(window.WindowInfo);
    //Render code here
}).Start();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多