NoelFB / blah

A small 2d c++ game framework

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

flip_vertically when drawing multiple framebuffer

feresr opened this issue · comments

Hi Noel! I keep learning cpp/openGL by looking at your code :) thanks you for making this open source.

There's something I can't figure out and I thought I'd ask here, batch.flip_vertically is set to true ever time we draw a framebuffer texture: m_batch.flip_vertically = ... && texture->is_framebuffer();

I thought that would cause the content to end up "flipped" vertically if we render to multiple framebuffers an odd number of times:

// ping pong a few times between framebuffer textures, flipping the texture on each render
{
batch.tex(buffer->texture(0), Blah::Vec2f::zero, Color::white);
batch.render(buffer2); // the texture is flipped once here (DOWN)
batch.clear();
}
{
batch.tex(buffer2->texture(0), Blah::Vec2f::zero, Color::white);
batch.render(buffer); // then again (UP)
batch.clear();
}

// render to screen
batch.tex(buffer->texture(0), Vec2f::zero, Color::white);
batch.render(App::backbuffer()); // here again (DOWN)
batch.clear();

It works as expected in 'blah' though. And I can't figure out why that is. There's definitely something I'm missing

The reason this exists is because OpenGL considers the bottom-left to be 0,0, which means to draw what is expected I need to flip the vertical UVs if we're using OpenGL when drawing a frame buffer. If you were to blit the frame buffer directly you would see it is upside down. So when it's drawn it gets flipped. That's why when you draw them multiple times there's no flip-flopping because the resulting frame buffer in OpenGL is actually upside down. There might be better ways to mitigate this, but this is the approach I took...

Other rendering APIs don't do this (ex. D3D11 has 0,0 at top-left, so nothing needs to be done).

Also note that this only matters for the Sprite Batcher since it tries to implement a way to perform simple drawing. If you draw stuff on your own using your own vertex buffers (Mesh in Blah), you would need to account for this difference yourself by checking App::renderer().origin_bottom_left

Oh I think I get it know, Thank you!
I finally figured it out, the reason this is not needed in my version of Blah is because I define the ortho matrix like this (bottom and top arguments flipped) which works for me because it's OpenGL only. Whereas in in Blah it's defined like this

(ps: I pretty much copied and pasted re-implemented your repo to understand how it all works, changing some things here and there)
Anyways, thank you for taking the time to reply :) closing this now