OpenGL ES Template: createFramebuffer called several times?

Posts: 48
Joined: 2011.03
Post: #1
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).

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 )
      g_Done = 1;
Quote this message in a reply
Posts: 40
Joined: 2009.05
Post: #2
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.

- (void)layoutSubviews
    // The framebuffer will be re-created at the beginning of the next setFramebuffer method call.
    [self deleteFramebuffer];
Quote this message in a reply
Posts: 48
Joined: 2011.03
Post: #3
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.
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  Xcode 4 OpenGL Game Template Question Macmenace 4 9,617 Mar 19, 2012 06:03 PM
Last Post: Macmenace
  SDL1.3 Audio with OpenGL ES Template SparkyNZ 1 6,663 Apr 14, 2011 11:36 AM
Last Post: SparkyNZ
  typedef struct - lossing data on some levels at random times jjslay 6 6,532 Jun 26, 2010 02:36 PM
Last Post: jjslay
  Submitting Updates to iTunes Connect and Review Times Bersaelor 4 12,604 May 26, 2010 09:51 AM
Last Post: AnotherJake
  Long shader compile times on iPhone 3G S conal 9 9,992 Nov 20, 2009 03:54 AM
Last Post: OneSadCookie