opengl多线程的问题

本文介绍了解决OpenGL在多线程环境下加载资源时出现纹理无法正常显示的问题。通过使用wglCreateContext和wglShareLists函数实现资源共享,确保不同线程间的OpenGL渲染上下文能够正确工作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  准备给游戏加入一个Loading画面,采用多线程,前面很顺利,但是就是载入后精灵无法正常显示,看不见纹理,开始以为是使用DevIL库出问题,单步调试发现DevIL载入图片正常,而opengl的glGenTextures出错了,返回的索引总是0。

后来google在gamedev上找到答案,原来opengl不是线程安全的(不知道正确不?),找到了关键方向:wglCreateContext.

后来在msdn上找到API解释,以及代码实例:

 

HDC    hdc; 
HGLRC  hglrc; 
 
// create a rendering context  
hglrc = wglCreateContext (hdc); 
 
// make it the calling thread's current rendering context 
wglMakeCurrent (hdc, hglrc);
 
// call OpenGL APIs as desired ... 
 
// when the rendering context is no longer needed ...   
 
// make the rendering context not current  
wglMakeCurrent (NULL, NULL) ; 
 
// delete the rendering context  
wglDeleteContext (hglrc);

 

只要hdc一样,创建的context都会渲染到相同的设备上面

但是实验很久,都没成功,最后发现是少了一个函数:wglShareLists(HGLRC  hglrc1, HGLRC  hglrc2)

注:hglrc1分享hglrc2的资源

// 创建多线程
// 获取当前hdc
HDC hDC = GetDC(hWnd);
// 创建渲染context
HGLRC hrc1 = wglCreateContext(hDC);
// 分享资源
wglShareLists(wglGetCurrentContext(), hrc1);
...
...
// 载入资源的时候
wglMakeCurrent(hDC,  hrc1);
...
...
// 使用后
wglMakeCurrent(NULL,  NULL);
wglDeleteContext(hrc1);

转载于:https://www.cnblogs.com/shadow21/archive/2011/01/17/1937801.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值