渲染屏幕(使用FBO和RenderBuffer)以及颜色,深度,模板的像素传输

时间:2013-07-17 22:54:15

标签: c++ opengl fbo depth-buffer stencil-buffer

我要在OpenGL中渲染屏幕,然后将图像传递给QImage。另外,只是为了运动,我还要将深度和模板缓冲区传输到CPU。

为了在屏幕外绘图,我使用了帧缓冲对象和渲染缓冲区(而不是纹理,因为我不需要纹理)。

使用颜色缓冲区(实际原始图像)进行像素传输操作。我看到了我所期待的。但深度和模板不起作用..深度奇怪的图像,模板没什么用。

首先,简单的部分,我实际绘制的内容:

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glLoadIdentity();
    glTranslatef(-1.5f,0.0f,-6.0f);
    glBegin(GL_TRIANGLES);
    glColor3f(1.0f,0.0f,0.0f);
    glVertex3f( 0.0f, 1.0f, 0.0f);
    glColor3f(0.0f,1.0f,0.0f);
    glVertex3f(-1.0f,-1.0f, -1.0f);
    glColor3f(0.0f,0.0f,1.0f);
    glVertex3f( 1.0f,-1.0f, 0.0f);
    glEnd();

这里是FBO和3渲染缓冲区的初始化:

// frame buffer object
glGenFramebuffers(1, &fboId);
glBindFramebuffer(GL_FRAMEBUFFER, fboId);

// render buffer as color buffer
glGenRenderbuffers(1, &colorBuffer);
glBindRenderbuffer(GL_RENDERBUFFER, colorBuffer);
glRenderbufferStorage(GL_RENDERBUFFER, GL_RGBA, width, height);
glBindRenderbuffer(GL_RENDERBUFFER, 0);
// attach render buffer to the fbo as color buffer
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, colorBuffer);

// render buffer as depth buffer
glGenRenderbuffers(1, &depthBuffer);
glBindRenderbuffer(GL_RENDERBUFFER, depthBuffer);
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT, width, height);
glBindRenderbuffer(GL_RENDERBUFFER, 0);
// attach render buffer to the fbo as depth buffer
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, depthBuffer);

glGenRenderbuffers(1, &stencilBuffer);
glBindRenderbuffer(GL_RENDERBUFFER, stencilBuffer);
glRenderbufferStorage(GL_RENDERBUFFER, GL_STENCIL_INDEX, width, height);
glBindRenderbuffer(GL_RENDERBUFFER, 0);
// attach render buffer to the fbo as stencil buffer
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_STENCIL_BUFFER, GL_RENDERBUFFER, stencilBuffer);

最后这里有3种像素传输方法:

QImage FBO::getImage()
{
    // this is called Pixel Transfer operation: http://www.opengl.org/wiki/Pixel_Transfer
    uchar *pixels;
    pixels = new uchar[width * height * 4];
    for(int i=0; i < (width * height * 4) ; i++ ) {
        pixels[i] = 0;
    }

    glBindFramebuffer(GL_FRAMEBUFFER, fboId);
    glReadPixels( 0,0,  width, height, GL_RGBA, GL_UNSIGNED_BYTE, pixels);

    glBindFramebuffer(GL_FRAMEBUFFER, 0);

    QImage qi = QImage(pixels, width, height, QImage::Format_ARGB32);
    qi = qi.rgbSwapped();

    return qi;
}

QImage FBO::getDepth()
{
    // this is called Pixel Transfer operation: http://www.opengl.org/wiki/Pixel_Transfer
    uchar *pixels;
    pixels = new uchar[width * height * 4];
    for(int i=0; i < (width * height * 4) ; i++ ) {
        pixels[i] = 0;
    }

    glBindFramebuffer(GL_FRAMEBUFFER, fboId);
    glBindRenderbuffer(GL_RENDERBUFFER, depthBuffer);
    glReadPixels( 0,0,  width, height, GL_DEPTH_COMPONENT, GL_FLOAT, pixels);

    glBindFramebuffer(GL_FRAMEBUFFER, 0);

    QImage qi = QImage(pixels, width, height, QImage::Format_ARGB32);
    qi = qi.rgbSwapped();

    return qi;
}

QImage FBO::getStencil()
{
    // this is called Pixel Transfer operation: http://www.opengl.org/wiki/Pixel_Transfer
    uchar *pixels;
    pixels = new uchar[width * height * 4];
    for(int i=0; i < (width * height * 4) ; i++ ) {
        pixels[i] = 0;
    }

    glBindFramebuffer(GL_FRAMEBUFFER, fboId);
    glBindRenderbuffer(GL_RENDERBUFFER, stencilBuffer);
    glReadPixels( 0,0,  width, height, GL_STENCIL_INDEX, GL_FLOAT, pixels);

    glBindFramebuffer(GL_FRAMEBUFFER, 0);

    QImage qi = QImage(pixels, width, height, QImage::Format_ARGB32);
    qi = qi.rgbSwapped();

    return qi;

}

这里是2个截图(颜色和深度,带有模板我得到一个空的QImage):

enter image description here

enter image description here

颜色正是我正在绘制的(翻转但我认为这是正常的)。深度..我期待一个带有渐变灰色三角形的白色图像..当然我在图像格式中犯了一些错误(GL_FLOAT?)但是我尝试了一些组合,这是最好的结果..紫罗兰色的背景,里面有毛躁的颜色......还有模板缓冲区......我期待黑色背景中的白色三角形轮廓,但是......不知道为什么我什么都看不到.. < / p>

编辑:

好的,不要单独使用模板缓冲区,所以我重构了一点。

宣布FBO时:

// render buffer for both depth and stencil buffer
glGenRenderbuffers(1, &depthStencilBuffer);
glBindRenderbuffer(GL_RENDERBUFFER, depthStencilBuffer);
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH24_STENCIL8, width, height);
glBindRenderbuffer(GL_RENDERBUFFER, 0);
// attach render buffer to the fbo as depth buffer
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, GL_RENDERBUFFER, depthStencilBuffer);

深度的像素传输:

QImage FBO::getDepth()
{
    std::vector<uchar> pixels;
    pixels.reserve(width * height*4);
    for(int i=0; i < (width * height*4) ; i++ ) {
        pixels.push_back(0);
    }

    glBindFramebuffer(GL_FRAMEBUFFER, fboId);
    glBindRenderbuffer(GL_RENDERBUFFER, depthStencilBuffer);
    glReadPixels( 0,0,  width, height,  GL_DEPTH24_STENCIL8, GL_UNSIGNED_BYTE, pixels.data());

    glBindFramebuffer(GL_FRAMEBUFFER, 0);

    std::vector<uchar> _pixels;
    _pixels.reserve(width * height*4);
    for(int i=0; i < (width * height*4) ; i++ ) {
        uchar p_red = pixels[i];
        uchar p_green = pixels[i+1];
        uchar p_blue = pixels[i+2];

        uchar p_stencil = pixels[i+3];

        _pixels.push_back(p_red);
        _pixels.push_back(p_green);
        _pixels.push_back(p_blue);

        _pixels.push_back(255); // alpha
    }

    QImage qi = QImage(_pixels.data(), width, height, QImage::Format_ARGB32);
    //qi = qi.rgbSwapped();

    return qi;
}

模板类似但使用p_stencil和rgb组件

..结果图像是深度和模板的黑色图像

修改

感谢Nicolas的回答我设法为深度和模板缓冲区使用渲染缓冲区,并使用此代码提取深度组件以适合QImage::Format_ARGB32

QImage FBO1::getDepth()
{
    // sizeof( GLuint ) = 4 byte
    // sizeof( uchar ) = 1 byte

    std::vector<GLuint> pixels(width * height);
    glBindFramebuffer(GL_FRAMEBUFFER, fboId);
    glBindRenderbuffer(GL_RENDERBUFFER, depthStencilBuffer);
    glReadPixels( 0,0,  width, height,  GL_DEPTH_STENCIL, GL_UNSIGNED_INT_24_8, pixels.data());
    glBindFramebuffer(GL_FRAMEBUFFER, 0);

    std::vector<uchar> _pixels;
    for(int i=0; i < (width * height) ; i++ ) {
        GLuint color = pixels[i];

        float temp = (color & 0xFFFFFF00) >> 8; // 24 bit of depth component

        // remap temp that goes from 0 to 0xFFFFFF (24 bits) to
        // _temp that goes from 0 to 0xFF (8 bits)
        float _temp = (temp / 0xFFFFFF) * 0xFF;

        _pixels.push_back((uchar)_temp);
        _pixels.push_back((uchar)_temp);
        _pixels.push_back((uchar)_temp);
        _pixels.push_back(255);
    }

    QImage qi = QImage(_pixels.data(), width, height, QImage::Format_ARGB32);
    qi = qi.rgbSwapped();

    return qi;
}

..模板组件仍存在一些问题(以下代码不起作用:它会产生毛刺图像):

QImage FBO1::getStencil()
{
    // sizeof( GLuint ) = 4 byte
    // sizeof( uchar ) = 1 byte

    std::vector<GLuint> pixels(width * height);
    glBindFramebuffer(GL_FRAMEBUFFER, fboId);
    glBindRenderbuffer(GL_RENDERBUFFER, depthStencilBuffer);
    glReadPixels( 0,0,  width, height,  GL_DEPTH_STENCIL, GL_UNSIGNED_INT_24_8, pixels.data());
    glBindFramebuffer(GL_FRAMEBUFFER, 0);

    std::vector<uchar> _pixels;
    for(int i=0; i < (width * height); i++ ) {
        GLuint color = pixels[i];

        uchar temp = (color & 0x000000FF); // last 8 bit of depth component

        _pixels.push_back(temp);
        _pixels.push_back(temp);
        _pixels.push_back(temp);
        _pixels.push_back(255);
    }

    QImage qi = QImage(_pixels.data(), width, height, QImage::Format_ARGB32);
    qi = qi.rgbSwapped();

    return qi;
}

2 个答案:

答案 0 :(得分:3)

  

深度..我期待带有渐变灰色三角形的白色图像..

您的代码不建议。

uchar *pixels;
pixels = new uchar[width * height * 4];

这会创建一个整数数组。这也是内存泄漏;请改用std::vector<uchar>

glReadPixels( 0,0,  width, height, GL_DEPTH_COMPONENT, GL_FLOAT, pixels);

这告诉OpenGL将浮动写入整数数组。因此,OpenGL会在写作时将pixels视为GLfloat的数组。

QImage qi = QImage(pixels, width, height, QImage::Format_ARGB32);

我将假设这将把它解释为某种8位每组件整数图像格式。所以你将float解释为8位整数。没有理由期望这种行为理性。

您应该将深度缓冲区作为浮点数读取并以更合理的方式将其转换为像素颜色,或者您应该将深度缓冲区作为整数值读取,让OpenGL为您执行灰度转换。

此外,您应始终使用combined depth/stencil image,而不是两个单独的渲染缓冲区。


glReadPixels( 0,0,  width, height,  GL_DEPTH24_STENCIL8, GL_UNSIGNED_BYTE, pixels.data());

您似乎不了解pixel transfers的工作方式。

首先,pixel transfer format指定您正在阅读的组件。 指定其大小。 GL_DEPTH24_STENCIL8an image format,不是像素传输格式。如果要从图像中读取深度和模板,请使用GL_DEPTH_STENCIL。像素传输格式没有大小。

所以这个函数只是给你一个OpenGL错误。

大小来自第二个参数pixel transfer type。在这种情况下,GL_UNSIGNED_BYTE表示它将读取深度和模板,将每个转换为无符号的8位值,并将每个像素中的两个存储到pixels.data()

深度缓冲区仅为每个像素存储1个值。深度/模板仅存储2.您无法使用OpenGL将它们复制为每像素4个元素的格式。因此,无论您稍后构建QImage,都必须我使用每个像素需要1或2个值的方法。

一般来说,如果你想读深度缓冲区,并且你希望深度缓冲区的数据真正有意义,你可以这样做:

std::vector<GLuint> pixels(width * height);  //std::vector value-initializes its elements.

glBindFramebuffer(GL_FRAMEBUFFER, fboId);
glReadPixels( 0,0,  width, height,  GL_DEPTH_STENCIL, GL_UNSIGNED_INT_24_8, pixels.data());

如何将这些放入QImage进行可视化取决于您自己。但这会得到一个无符号整数数组,其中高24位是深度分量,低8位是模板。

答案 1 :(得分:2)

嗯,深度图片看起来像是可以从这段代码中看出来的:

 glReadPixels( 0,0,  width, height, GL_DEPTH_COMPONENT, GL_FLOAT, pixels);

 glBindFramebuffer(GL_FRAMEBUFFER, 0);

 QImage qi = QImage(pixels, width, height, QImage::Format_ARGB32);

QImage不知道如何处理浮点数,因此它解释每个32位(在浮点数内)的每一组,好像它们是ARGB组件,每个8位。浮点在低位有一个25位的尾数,很好地映射到那些组件。你看到的那些频段只是增加的尾数,裁剪成一定数量的位。

QImage实际上是一个非常有限的东西(它甚至不能处理HDR配置,例如每个通道16位,这有点令人沮丧)。无论如何,你最好的办法是将这个浮点数转换为0 ... 255的范围,并将其作为灰度图像传递给QImage。

另外,单独的深度模板对于性能而言也很重要。在可能的情况下,始终使用组合格式。

......好吧,有Nicol Bolas的回答,这和我写的一样。另外他指出了内存泄漏。