iDevGames Forums
OpenGL ES Template: createFramebuffer called several times? - Printable Version

+- iDevGames Forums (http://www.idevgames.com/forums)
+-- Forum: Development Zone (/forum-3.html)
+--- Forum: iPhone, iPad & iPod Game Development (/forum-11.html)
+--- Thread: OpenGL ES Template: createFramebuffer called several times? (/thread-8834.html)



OpenGL ES Template: createFramebuffer called several times? - SparkyNZ - Apr 13, 2011 06:01 PM

Hi, I'm having some difficulty understanding what the OpenGL ES template is trying to do.

I want to initialise my OpenGL matrix etc in one place. Up until now I've had an InitGL() call within my createFrameBuffer method. It wasn't until I noticed a bug on the iPhone that wasn't in the Windows version of my app that made me realise my InitGL() function was being called twice.

Since then I've hacked things somewhat by adding a global flag to stop my function being called twice.

Can somebody please tell me why mulitple calls to createFrameBuffer are made? Does this have something to do with the application splash screen display? (which I'm not displaying at present).


Code:
int g_Done = 0;

- (void)createFramebuffer
{
  if (context && !defaultFramebuffer)
  {
    [EAGLContext setCurrentContext:context];
        
    // Create default framebuffer object.
    glGenFramebuffers(1, &defaultFramebuffer);
    glBindFramebuffer(GL_FRAMEBUFFER, defaultFramebuffer);
        
    // Create color render buffer and allocate backing store.
    glGenRenderbuffers(1, &colorRenderbuffer);
    glBindRenderbuffer(GL_RENDERBUFFER, colorRenderbuffer);
    [context renderbufferStorage:GL_RENDERBUFFER fromDrawable:(CAEAGLLayer *)self.layer];
    glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_WIDTH, &framebufferWidth);
    glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_HEIGHT, &framebufferHeight);
    
    glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, colorRenderbuffer);
    
    if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE)
        NSLog(@"Failed to make complete framebuffer object %x", glCheckFramebufferStatus(GL_FRAMEBUFFER));
      
    // PDS: This should only be called once.. but its not..
    if( ! g_Done )
    {
      InitGL();      
      g_Done = 1;
    }
  }
}



RE: OpenGL ES Template: createFramebuffer called several times? - iamflimflam1 - Apr 16, 2011 09:10 AM

The template is a bit of a mess in my opinion.

The reason you are seeing createFrameBuffer twice is because the layoutSubviews method on EAGLView deletes the frame buffer which causes createFrameBuffer to be called the next time setFrameBuffer is called (which is on every draw loop).

So the first call happens in the awakeFromNib of the controller which calls setFrameBuffer (which then calls createFrameBuffer).

And then when you view is actually laid out the layoutSubViews gets called which deletes the frame buffer which causes setFrameBuffer to regenerate them with a call to createFrameBuffer.

The reason layoutSubview deletes the frame buffer is because if your view changes size you'd want to regenerate the frame buffers to match the view size.

You are probably better off putting your one time initialisation code into the awakeFromNib call in the view controller.

Code:
- (void)layoutSubviews
{
    // The framebuffer will be re-created at the beginning of the next setFramebuffer method call.
    [self deleteFramebuffer];
}



RE: OpenGL ES Template: createFramebuffer called several times? - SparkyNZ - Apr 16, 2011 02:46 PM

Thanks FlimFlam.. Yeah I thought it was a right mess too. :-) Its funny that I've been switching back and forth between using an SDL main loop and OpenGL ES template instead.. Through the journey I've been pushed back into using SDL - for some reason SDL with the OpenGL ES template code was very glitchy and stuttery when I used it for audio alone. However.. there's always the possibility of me coming back to non-SDL code again so.. thanks for the help. I'll definitely move my one-off time code for sure.