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.

Wednesday, April 20, 2011

Maxscript Function Pre-declaration

Anyone who has spent more than 10 minutes writing MAXScript has probably run into this problem before. You're coding along, and you do one of these:

fn funcOne = (
funcTwo()
)

fn funcTwo = (
print "Hello, World!"
)



Upon getting to the definition, Maxscript will promptly lose its shit over your function ordering. "The hell is a FuncTwo()?" it will cry. It will then halt execution and schedule an appointment with its shrink because it's halucinating about functions that don't exist.

At this point you may think, "Aha, but Maxscript has structures! Structures will solve all my problems!" This is honestly not a bad way to think. Using Maxscript structures make a lot of Maxscript headaches go away! I read about how cool they are on TA.O and everything!*

So, armed with your newfound love of structures, you write the following:

struct orderingStruct
(
fn funcOne = (
funcTwo()
),

fn funcTwo = (
print "Hello, World!"
)
)

You hit ctrl+e, thinking you've solved this problem. But no, Maxscript freaks out at you at the same point. It still has NO idea what a funcTwo() is, and promptly halts execution and cries at you. You're not really sure what's wrong here. Other languages with structures think this is just fine, but Maxscript can't deal. So, what do you do? Well, What would a C++ programmer do?

Predeclare.

The issue is that Maxscript just doesn't know what sort of thing a funcTwo is. For all it knows, it could be some crazy built-in dotNET thing. Really all you need to do is to let Maxscript know ahead of time that there is a function called funcTwo, and it will be happy. So, knowing this, you write this:



struct orderingStruct
(
/* --- Interface --- */
fn funcOne = (), fn funcTwo(),

/*--- Implementation --- */
fn funcOne =(
funcTwo()
),

fn funcTwo =(
print "Hello, World!"
)
)
And, like magic, the problem goes away!


*= Sarcasm aside, I am a HUGE advocate of structs in any and all Maxscript code. You can ALWAYS use moar structures in your Maxscripting. They are as rad as Maxscript gets!