我正在使用C语言和Win32 API,想要知道如何实现一个用于加载资源(纹理和VBO)的OpenGL次线程。
根据我找到的信息,应该使用wglShareLists()实现,但是我不确定该如何设置次线程:
我需要一个新的设备上下文还是只需要一个新的渲染上下文?
我需要调用哪些wgl函数?
private bool TestSharingObject(RenderContext rContextShare)
{
uint texture = 0;
// rContextShader is a context sharing with this RenderCOntext
this.MakeCurrent(true);
if (Caps.TextureObject.Supported == true) {
// Generate texture name
Gl.GenTextures(1, out texture);
// Ensure existing texture object
Gl.BindTexture(Gl.TEXTURE_2D, texture);
Gl.BindTexture(Gl.TEXTURE_2D, 0);
// Self test
if (Gl.IsTexture(texture) == false)
throw new NotSupportedException();
}
// Make current sharing context
rContextShare.MakeCurrent(true);
return ((texture != 0) && (Gl.IsTexture(texture) == true));
}
在你的次要线程中以任何你需要的方式加载资源,然后将它们的所有权传回到主线程进行GL调用。
在GL上下文之间共享“列表”应该可以工作。在主线程上执行所有GL调用确实有效 :)
只有在主线程上通过wglShareLists完成所有工作线程时,它才能正常工作。 使用消息映射,传递渲染上下文的引用,并让主线程创建渲染上下文,仅由主线程使用wglShareLists。然后,在工作线程上使用由主线程创建的渲染上下文调用wglMakeCurrent。在对渲染上下文进行任何gl操作之前,必须先调用wglShareLists。这是因为参数hglrc2(与hglrc1共享显示列表的OpenGL渲染上下文)要求在调用wglShareLists时不包含任何现有的显示列表。