10

I'm rendering into an OpenGL offscreen framebuffer object and like to save it as an image. Note that the FBO is larger than the display size. I can render into the offscreen buffer and use it as texture, which works. I can "scroll" this larger texture through the display using an offset, which makes me confident, that I render into a larger context than the window.

If I save the offscreen buffer to an image file it always gets cropped. The code fragment for saving is:

void ofFBOTexture::saveImage(string fileName) { 
    glReadBuffer(GL_COLOR_ATTACHMENT0_EXT); 
    // get the raw buffer from ofImage
    unsigned char* pixels = imageSaver.getPixels();
    glReadPixels(0, 0, 1024, 1024, GL_RGB, GL_UNSIGNED_BYTE, pixels); 

    imageSaver.saveImage(fileName); 
} 

Please note, that the image content is cropped, the visible part is saved correctly (which means no error in pixel formats, GL_RGB issues etc.), but the remaining space is filled with one color.

So, my question is - what am I doing wrong?

razong
  • 1,269
  • 11
  • 22

4 Answers4

11

Finally I solved the issue.

I have to activate the fbo for saving its contents:

glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fbo);
// save code
...
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);

while only selecting the fbo for glReadPixels via

glReadBuffer(GL_COLOR_ATTACHMENT0_EXT);

doesn't suffice.

(All other things where correct and tested, eg. viewport sizes, width and height of buffer, image texture etc.)

razong
  • 1,269
  • 11
  • 22
  • I'm having the same problem as you. Could you expand on how you obtain the fbo variable? I can't find what to include to access glGenFrameBuffers, which is what I think is used. – Andrew Buss Dec 29 '11 at 21:32
  • 1
    2. GLuint fbo; glGenFramebuffersEXT(1, &fbo); // 1==numbers of fbo to create glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fbo); – razong Jan 03 '12 at 09:14
  • I'm developing for Windows, but I found an excellent description on the OS X dev site here: http://tinyurl.com/7cbs7sr – Andrew Buss Jan 05 '12 at 18:08
0

Two questions to start: How are you creating imageSaver and are you sure your width and height are correct (e.g. are you trying to save a 1024 x 1024 image? What size are you getting)?

MarkusQ
  • 21,814
  • 3
  • 56
  • 68
  • 1. The imageSaver is an openframeworks image object. To avoid any possible errors I even created it through loading an empty bitmap file with size and depth of my desired target image. 2. I get an 1024x1024 image, as desired but only content equal to the rendered window. everything else is "empty" – razong Mar 25 '09 at 21:30
0

You're not doing anything wrong, this has been quite common behaviour with OpenGL graphics drivers for a long time - I recall running up against exactly the same issue on nVidia Geforce 3 cards at least a decade ago, and I adopted a similar solution - rendering to an offscreen texture.

Cruachan
  • 15,733
  • 5
  • 59
  • 112
0

This sounds like you have the wrong viewport size or something similar.

Maurice Gilden
  • 1,934
  • 1
  • 12
  • 16