SDL2 & OpenGL Issue writing to Offscreen FBOs


Hi. Is there something unique to SDL that I should be aware of when trying to write to an offscreen FBO & texture when using OpenGL with SDL2?

I posted the below question to Stackoverflow here but I have narrowed down my problem to the size of the texture I create with glTexImage2D(). Originaly I was trying to create a texture 512x512 pixels to draw into and then use glReadPixels() to get the RGB values back. Same code works fine in a non-SDL framework application but I always get black (zero RGB) when running under SDL framework unless I use a texture of 1024x1024 or larger.

I’m aware of the “power of 2 rule” for texture dimensions but I wasn’t aware of a minimum size for texture dimensions.

Is this an SDL feature specific to FBO drawing or is there perhaps an SDL configuration parameter that I’m not aware of?


Taking a brief look at the code

glReadPixels( x, g_ScreenHeight - y - 1, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, abRGBA );

g_ScreenHeight is greater than 512, which might be why you are not getting the expected results.


Thanks Stan. I think you gave me the hint to solve the problem!

My projection was setup like this:

g_Ortho = glm::ortho( 0.0f, (GLfloat) g_ScreenWidth, (GLfloat) g_ScreenHeight, 0.0f, 0.0f, 1000.0f );

…and g_Ortho is fed into my shader program.

When I switch to using the FBO, I should be configuring the ortho projection for the FBO as well - and to use the texture width and height instead of the screen width and height.

Awesome! Thanks for pointing me in the right direction. Funny, I tried changing the g_ScreenHeight inside the glReadPixels() code and that wasn’t working either - because the whole projection must have been a mess. :slight_smile:


Hm, that’s strange. AFAIK projection does not have an effect on glReadPixels.


You are correct. The problem was with the drawing to the FBO rather than the reading.