Why does calling glMatrixMode (GL_PROJECTION) give me EXC_BAD_ACCESS in an iPhone app?

I have an iphone application 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);

When going with the debugger, I get EXC BAD ACCESS when the first line is executed. Any ideas why this is happening?

Btw I have another application where I do the same and it works great. So I tried to duplicate everything in this application (#imports, adding OpenGLES framework, etc.), but now I'm just stuck.

+5
source share
5 answers

I came across this with OpenGL calls if two threads try to draw onto an OpenGL scene right away. However, this is not like what you are doing.

? , UIView, OpenGL, initWithCoder:

context = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES1];

if (!context || ![EAGLContext setCurrentContext:context] || ![self createFramebuffer]) 
{
    [self release];
    return nil;
}

createFramebuffer :

- (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 XCode. , glMatrixMode(), .

, OpenGL applicationDidFinishLaunching:? OpenGL, UIApplicationDelegate?

+4

, , - , Apple ES 2.0, spec, , ES 1.1.

+3

, . - , .

, - ?

+2

glOrthof . glLoadIdentity()

+1

Reboot the iPhone simulator. This problem is definitely due to the fact that the OpenGL context is not set properly. I found that sometimes iPhone Simulator has problems and needs to be restarted in order for the OpenGL context to be set correctly using [EAGLContext setCurrentContext:].

+1
source

All Articles