作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个库,它负责渲染来自网络的 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;
- (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;
// 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 上下文。即使您确实在程序的早期创建了上下文(在您的代码段中不可见),它也不会在您启动的线程中处于当前状态。
OpenGL 上下文始终是“当前”的,一次只有一个线程,无一异常(exception)。默认情况下,这是创建上下文的线程。任何调用 OpenGL 的线程都必须首先设为“当前”。
你 必须要么在这个线程中创建上下文,要么调用 glXMakeCurrent
(Unix/Linux) 或 aglMakeCurrent
(Mac) 或 wglMakeCurrent
(Windows) 内 ShowThread
(在做任何与 OpenGL 相关的事情之前)。
(可能不是坠机的原因,不过......请参阅 datenwolf 的回答,了解坠机的可能原因——但它是错误的)`
关于opengl - 在线程中使用opengl,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13989965/
我是一名优秀的程序员,十分优秀!