我有一个用于iPhone的OpenGL ES应用程序,我正在开发,作为另一个平台的面向2D应用程序的端口。出于性能考虑,我选择使用OpenGL ES来渲染图形。然而,主要应用在单独的线程上运行(由于原来的应用程序设计),所以从我的应用程序委托中我这样做:如何使iPhone OpenGL ES上下文更新直接消失?
- (void) applicationDidFinishLaunching:(UIApplication *)application {
CGRect rect = [[UIScreen mainScreen] bounds];
glView = [[EAGLView alloc] initWithFrame:rect];
[window addSubview:glView];
// launch main application in separate thread
[NSThread detachNewThreadSelector:@selector(applicationMainThread) toTarget:self withObject:nil];
}
然而,我注意到,试图呈现applicationMainThread内的任何电话屏幕上的东西不会渲染任何东西,直到该线程终止。
我在子应用程序线程上设置了实际的OpenGL ES上下文,而不是UI线程。如果我这样做:
- (void) applicationMainThread {
CGRect rect = [[UIScreen mainScreen] bounds];
[glView createContext]; // creates the open GL ES context
//Initialize OpenGL states
glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
glEnable(GL_TEXTURE_2D);
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glMatrixMode(GL_PROJECTION);
glOrthof(0, rect.size.width, 0, rect.size.height, -1, 1);
glMatrixMode(GL_MODELVIEW);
Texture2D *tex = [[Texture2D alloc] initWithImage:[UIImage imageNamed:@"iphone_default.png"]];
glBindTexture(GL_TEXTURE_2D, [tex name]);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glDisable(GL_BLEND);
[tex drawInRect:[glView bounds]];
glEnable(GL_BLEND);
[tex release];
[glView drawView];
}
然后纹理几乎立即更新到屏幕,正如我所期望的。
然而,如果之后的[glView drawView函数]调用我添加这一行:
[NSThread sleepForTimeInterval:5.0]; // sleep for 5 seconds
然后5秒延迟完成后的画面仅更新。这使我相信只有在线程本身终止时才会更新屏幕(需要进行更多测试以确认)。这意味着当我用实际的应用程序代码替换多个屏幕更新时,实际上没有任何更新发生(留下白色屏幕),直到应用程序线程退出,而不是我想要的!
那么 - 有没有什么办法可以解决这个问题,还是我做了一件明显错误的事情?
你能告诉我们你的'glView :: drawView'里面有什么吗? – zoul 2009-12-31 09:35:49
- (void)drawView {EAGLContext setCurrentContext:context]; glBindRenderbufferOES(GL_RENDERBUFFER_OES,viewRenderbuffer); [context presentRenderbuffer:GL_RENDERBUFFER_OES]; } – Gabriel 2009-12-31 15:35:15
你好同伴阳光海岸开发! – alex 2010-09-24 00:45:45