Wednesday, September 24, 2014

iOS, GLKView, and default framebuffers

I'm breaking my 3 year blog hiatus to write about this because I couldn't for the life of me find any real help troubleshooting this, and I'd like the answer somewhere on the internet.

So, I'm writing an iPhone game, because I like games. In writing this game, I'm writing everything from scratch, because I apparently have to make everything as hard as I can for some reason. Somewhat recently, I decided I wanted to try offscreen rendering so that I can do post process effects. I set up everything right, and get treated to this wonderful blank screen


So yeah. That's not exactly right.

Off I went to Google and Stack Overflow to try and figure out what I did wrong. My search turned up a lot of good information. For example, calling glBindFramebuffer(GL_FRAMEBUFFER, 0)  doesn't reset the default framebuffer on iOS. I tried that, and no results.

Next thing I tried was using glGetIntegerV with the enum GL_FRAMEBUFFER to extract the default FBO. That of course also didn't work. But interestingly enough, when I looked into that line, I found it wasn't setting the value of the framebuffer at all, it was leaving it at 0.

I did a little (okay, a lot) more digging, and finally found what I was looking for. I'm using GLKView to display the screen. GLKView handles some of the interaction between OpenGL and the windowing system. Because of this, the view is the owner of its framebuffer, and OpenGL has no reference to it. Fortunately, the class has a public method to set its FBO as the current one in the form of -(void)bindDrawable;

TLDR:
If you're using GLKView and trying to do offscreen rendering, use the view's bindDrawable method to set the screen FBO instead of glBindFramebuffer.

2 comments:

  1. You are a life saver! Many, many thanks to you, sir!

    ReplyDelete
  2. when using bindDrawable function, my objects are bring drawn in one-fourth of its actual size. Any idea about what I might be doing wrong? Thank you in advance.

    ReplyDelete