4

I write a program using OpenGL. It implements a simple function: draw a teapot.And in order to make it look nice on the screen, I enable multisample anti-aliasing. And it does. Look at the following bitmap:

enter image description here

But when I save it as a bmp picture, it looks bad. I use FBO and PBO to do it. Now I post part of my code here:

glGenFramebuffers(1,&m_frameBuffer);
glBindFramebuffer(GL_FRAMEBUFFER,m_frameBuffer);

glGenRenderbuffers(1,&m_renderBufferColor);
glBindRenderbuffer(GL_RENDERBUFFER,m_renderBufferColor);
glRenderbufferStorage(GL_RENDERBUFFER,GL_RGB,
    m_subImageWidth,m_subImageHeight);
glFramebufferRenderbuffer(GL_FRAMEBUFFER,GL_COLOR_ATTACHMENT0,
    GL_RENDERBUFFER,m_renderBufferColor);

glGenRenderbuffers(1,&m_renderBufferDepth);
glBindRenderbuffer(GL_RENDERBUFFER,m_renderBufferDepth);
glRenderbufferStorage(GL_RENDERBUFFER,GL_DEPTH_COMPONENT,
    m_subImageWidth,m_subImageHeight);
glFramebufferRenderbuffer(GL_FRAMEBUFFER,GL_DEPTH_ATTACHMENT,
    GL_RENDERBUFFER,m_renderBufferDepth);

glBindFramebuffer(GL_FRAMEBUFFER,0);

glBindFramebuffer(GL_FRAMEBUFFER,m_frameBuffer);



glGenBuffers(1,m_subImageBuffer);
glBindBuffer(GL_PIXEL_PACK_BUFFER,m_subImageBuffer);
        glBufferData(GL_PIXEL_PACK_BUFFER,m_bufferSize,
            NULL,GL_STREAM_READ);

glBindFramebuffer(GL_FRAMEBUFFER,m_frameBuffer);

glBindBuffer(GL_PIXEL_PACK_BUFFER,m_subImageBuffer);
glPixelStorei(GL_PACK_ALIGNMENT,1);
//注意:以BGR的顺序读取
glReadPixels(0,0,m_subImageWidth,m_subImageHeight,
        GL_BGR,GL_UNSIGNED_BYTE,bufferOffset(0));
GLUtils::checkForOpenGLError(__FILE__,__LINE__);
m_subPixels[i] = static_cast<GLubyte*>(glMapBuffer(GL_PIXEL_PACK_BUFFER,GL_READ_ONLY));
gltGenBMP(subImageFile,GLT_BGR,m_subImageWidth,m_subImageHeight,m_subPixels[i]);
glBindBuffer(GL_PIXEL_PACK_BUFFER,0);
glUnmapBuffer(GL_PIXEL_PACK_BUFFER);
glBindBuffer(GL_PIXEL_PACK_BUFFER,0);

enter image description here

I am very curious.Why they are different : render to default famebuffer and save to a bmp picture?

Actually, what I want to do is to get 9 small bitmaps in 9 different adjacent angle and then synthesis one bitmap to display on a stereoscope 3D screen. But the synthesised bitmap looks bad.

Could someone tell me why?

genpfault
  • 51,148
  • 11
  • 85
  • 139
XiaJun
  • 1,855
  • 4
  • 24
  • 41
  • First, 8-bit / 24-bit BMPs, which are most common, don't support Alpha. I don't know if gltGenBMP generates the uncommon 32-bit BMPs. You may want to verify that. You are better off using a 3rd party library and saving it to PNG or TIFF. – Karthik Kumar Viswanathan Jun 21 '12 at 03:52
  • @Karthik Kumar Viswanathan This is a function I write to save a bmp .And it generates 24 bit BMPs. – XiaJun Jun 21 '12 at 07:02

1 Answers1

4

Just because you enable multisample on your frame buffer does not mean your FBO will have it too. You need to use glRenderbufferStorageMultisample when creating the FBO.

See: FBO Blitting is not working

And: http://www.opengl.org/wiki/GL_EXT_framebuffer_multisample

This is also relevant: glReadPixels from FBO fails with multisampling

Community
  • 1
  • 1
Ville Krumlinde
  • 7,021
  • 1
  • 33
  • 41