为什么调用glMatrixMode(GL_PROJECTION)在iPhone应用程序中给我EXC_BAD_ACCESS? [英] Why does calling glMatrixMode(GL_PROJECTION) give me EXC_BAD_ACCESS in an iPhone app?
问题描述
我有一个iphone应用程序,我在appDidFinishLaunching中调用这三个函数:
I have an iphone app where I call these three functions in appDidFinishLaunching:
glMatrixMode(GL_PROJECTION);
glOrthof(0, rect.size.width, 0, rect.size.height, -1, 1);
glMatrixMode(GL_MODELVIEW);
当我使用调试器时,我执行第一行时得到EXC BAD ACCESS。任何想法为什么会发生这种情况?
When stepping through with the debugger I get EXC BAD ACCESS when I execute the first line. Any ideas why this is happening?
顺便说一下,我有另一个应用程序,我做同样的事情,它工作正常。所以我试图复制该应用程序中的所有内容(#imports,添加OpenGLES框架等),但现在我只是卡住了。
Btw I have another application where I do the same thing and it works fine. So I've tried to duplicate everything in that app (#imports, adding OpenGLES framework, etc) but now I'm just stuck.
推荐答案
如果两个线程试图立即绘制到OpenGL场景,我会遇到OpenGL调用。但是,这听起来不像你正在做的那样。
I've run into this with OpenGL calls if two threads are attempting to draw to the OpenGL scene at once. However, that doesn't sound like what you're doing.
你是否在此次通话之前正确初始化了显示上下文和帧缓冲?例如,在我执行OpenGL绘图的UIView子类中,我在其initWithCoder中调用以下内容:方法:
Have you properly initialized your display context and framebuffer before this call? For example, in my UIView subclass that does OpenGL drawing, I call the following in its initWithCoder: method:
context = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES1];
if (!context || ![EAGLContext setCurrentContext:context] || ![self createFramebuffer])
{
[self release];
return nil;
}
createFramebuffer方法如下所示:
The createFramebuffer method looks like the following:
- (BOOL)createFramebuffer
{
glGenFramebuffersOES(1, &viewFramebuffer);
glGenRenderbuffersOES(1, &viewRenderbuffer);
glBindFramebufferOES(GL_FRAMEBUFFER_OES, viewFramebuffer);
glBindRenderbufferOES(GL_RENDERBUFFER_OES, viewRenderbuffer);
[context renderbufferStorage:GL_RENDERBUFFER_OES fromDrawable:(CAEAGLLayer*)self.layer];
glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES, GL_COLOR_ATTACHMENT0_OES, GL_RENDERBUFFER_OES, viewRenderbuffer);
glGetRenderbufferParameterivOES(GL_RENDERBUFFER_OES, GL_RENDERBUFFER_WIDTH_OES, &backingWidth);
glGetRenderbufferParameterivOES(GL_RENDERBUFFER_OES, GL_RENDERBUFFER_HEIGHT_OES, &backingHeight);
if (USE_DEPTH_BUFFER) {
glGenRenderbuffersOES(1, &depthRenderbuffer);
glBindRenderbufferOES(GL_RENDERBUFFER_OES, depthRenderbuffer);
glRenderbufferStorageOES(GL_RENDERBUFFER_OES, GL_DEPTH_COMPONENT16_OES, backingWidth, backingHeight);
glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES, GL_DEPTH_ATTACHMENT_OES, GL_RENDERBUFFER_OES, depthRenderbuffer);
}
if(glCheckFramebufferStatusOES(GL_FRAMEBUFFER_OES) != GL_FRAMEBUFFER_COMPLETE_OES)
{
return NO;
}
return YES;
}
这是一个样板代码,由OpenGL ES Application模板生成XCode中。也许在调用glMatrixMode()之前没有初始化东西,你就会崩溃。
This is pretty much boilerplate code, as generated by the OpenGL ES Application template in XCode. Perhaps by not initializing things before calling glMatrixMode(), you're getting a crash.
另外,你为什么要在applicationDidFinishLaunching中进行OpenGL绘图?对于OpenGL调用,视图或视图控制器不是比UIApplicationDelegate更适合的地方吗?
Also, why are you doing OpenGL drawing in applicationDidFinishLaunching:? Wouldn't a view or view controller be a more appropriate place for OpenGL calls than your UIApplicationDelegate?
这篇关于为什么调用glMatrixMode(GL_PROJECTION)在iPhone应用程序中给我EXC_BAD_ACCESS?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!