gpt4 book ai didi

opengl - 在线程中使用opengl

转载 作者:行者123 更新时间:2023-12-04 05:17:58 26 4
gpt4 key购买 nike

我有一个库,它负责渲染来自网络的 opengl 和 primimaet 流。

我在罂粟下写的,但打算在linux上使用

所以窗口是为 objective-c 创建的
我开始在另一个接收和解码数据的单独线程中绘制。

我在 opengl 的方法上崩溃了错误(EXT_BAD_ACCESS),即使我只在单个线程中使用它们。

我的代码
主要过剩:

int main(int argc, const char * argv[]){
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGBA);
int win = glutGetWindow();
glutInitWindowSize(800, 600);
glutCreateWindow("OpenGL lesson 1");
client_init(1280, 720, win, "192.168.0.98", 8000, 2222);
return 0;

}

或 objective-c
- (id)initWithFrame:(NSRect)frameRect pixelFormat:(NSOpenGLPixelFormat*)format{
self = [super initWithFrame:frameRect];
if (self != nil) {
NSOpenGLPixelFormatAttribute attributes[] = {

NSOpenGLPFANoRecovery,
NSOpenGLPFAFullScreen,
NSOpenGLPFAScreenMask,
CGDisplayIDToOpenGLDisplayMask(kCGDirectMainDisplay),
(NSOpenGLPixelFormatAttribute) 0
};
_pixelFormat = [[NSOpenGLPixelFormat alloc] initWithAttributes:attributes];
if (!_pixelFormat)
{
return nil;
}
//_pixelFormat = [format retain];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(_surfaceNeedsUpdate:)
name:NSViewGlobalFrameDidChangeNotification
object:self];
_openGLContext = [self openGLContext];
client_init(1280, 720, win, "192.168.0.98", 8000, 2222);
}
return self;

}

client_init 代码
    // pthread_create(&posixThreadID, NULL, (void*(*)(void*))ShowThread, dh_tmp);
pthread_create(&posixThreadID, NULL, (void*(*)(void*))ShowThread, NULL);

void* ShowThread(struct drawhandle * dh){

//glViewport(0, 0, dh->swidth, dh->sheight);//EXT_BAD_ACCESS
glViewport(0, 0, 1280, 720);//EXT_BAD_ACCESS

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
//gluOrtho2D(0, dh->swidth, 0, dh->sheight);
gluOrtho2D(0, 1280, 0, 720);

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

...
return 0;
}

我认为问题是什么?那个未创建的上下文opengl。

如何在 macOS/linux 中创建它?

最佳答案

该线程没有当前的 OpenGL 上下文。即使您确实在程序的早期创建了上下文(在您的代码段中不可见),它也不会在您启动的线程中处于当前状态。

OpenGL 上下文始终是“当前”的,一次只有一个线程,无一异常(exception)。默认情况下,这是创建上下文的线程。任何调用 OpenGL 的线程都必须首先设为“当前”。

必须要么在这个线程中创建上下文,要么调用 glXMakeCurrent (Unix/Linux) 或 aglMakeCurrent (Mac) 或 wglMakeCurrent (Windows) 内 ShowThread (在做任何与 OpenGL 相关的事情之前)。

(可能不是坠机的原因,不过......请参阅 datenwolf 的回答,了解坠机的可能原因——但它是错误的)`

关于opengl - 在线程中使用opengl,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13989965/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com