How to properly write to a FBO?

Hello, I’m trying to write to an FBO to do some shader testing, but I’ve found some technical issues regarding how to do so.

Currently, I have set up a rectangle batch I create with my shader, I bind the fbo with bindFrameBuffer(), set all the transformations to mat4() (+ doubling the scale of the rect), and draw the rectangle. This works if i’m drawing to the app screen, but with the FBO which has different dimensions than of the app window, it draws differently.

More specifically, it seems the app is on default 640 by 480 pixels. my FBO is now 100x100px. from what I’ve tested, all bindFrameBuffer does is copy to the FBO what would have been drawn on the bottom left 100x100 square of the app. so even though I made all the transformations identities, the clipspace to screenspace calculations for the FBO become “wrong”.

Is there a better way to draw on to an FBO/texture/surface directly? or is there a way to pretend the FBO is actually the window?
I’m aware the simplest solution is either to transform the rectangle based on the size of the fbo+screen, or draw fullscreen and blit/condense the image down to size, but these solutions feel hacky.

Assuming you’re drawing in 2D, setting up your matrices to render to an FBO shouldn’t be much more involved than:

    gl::ScopedFramebuffer fboBind { _yourFbo };
    gl::ScopedMatrices mat;
    gl::setMatricesWindow ( _yourFbo->getSize() );
    gl::ScopedViewport vp { ivec2(), _yourFbo->getSize() };

    // draw

I suspect the part you were missing was setting the viewport (directly analogous to glViewport) if you want to do some more reading.