渲染屏幕(使用FBO和RenderBuffer)和像素传输的颜色,深度,模板 [英] Render off screen (with FBO and RenderBuffer) and pixel transfer of color, depth, stencil

查看:5426
本文介绍了渲染屏幕(使用FBO和RenderBuffer)和像素传输的颜色,深度,模板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在OpenGL中渲染屏幕,然后将图像传递给QImage。另外,只是为了锻炼我想转移到CPU也深度和模板缓冲区。



对于绘制离屏我已经使用帧缓冲区对象与渲染缓冲区(而不是纹理,因为我不需要纹理)。



使用颜色缓冲区(实际原始图像)的像素传输操作。我看到了我的期望。



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

p>

  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渲染缓冲区的初始化:

  //帧缓冲区对象
glGenFramebuffers(1,& fboId);
glBindFramebuffer(GL_FRAMEBUFFER,fboId);

//渲染缓冲区作为颜色缓冲区
glGenRenderbuffers(1,& colorBuffer);
glBindRenderbuffer(GL_RENDERBUFFER,colorBuffer);
glRenderbufferStorage(GL_RENDERBUFFER,GL_RGBA,width,height);
glBindRenderbuffer(GL_RENDERBUFFER,0);
//将渲染缓冲区作为颜色缓冲区附加到fbo
glFramebufferRenderbuffer(GL_FRAMEBUFFER,GL_COLOR_ATTACHMENT0,GL_RENDERBUFFER,colorBuffer);

//将缓冲区渲染为深度缓冲区
glGenRenderbuffers(1,& depthBuffer);
glBindRenderbuffer(GL_RENDERBUFFER,depthBuffer);
glRenderbufferStorage(GL_RENDERBUFFER,GL_DEPTH_COMPONENT,width,height);
glBindRenderbuffer(GL_RENDERBUFFER,0);
//将渲染缓冲区作为深度缓冲区附加到fbo
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);
//将渲染缓冲区附加到fbo作为模板缓冲区
glFramebufferRenderbuffer(GL_FRAMEBUFFER,GL_STENCIL_BUFFER,GL_RENDERBUFFER,stencilBuffer);

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

  QImage FBO :: getImage()
{
//这称为像素传输操作: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()
{
//这称为像素传输操作: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()
{
//这称为像素传输操作: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):





>



颜色一个是我正在绘制(翻转,但它是正常的我想)。深度..我期待一个白色图像与渐变灰色三角形..确定我在图像格式( GL_FLOAT ?)有一些错误,但我试过一些组合,这是最好的结果..紫罗兰色背景与其内部的毛刺色的颜色..和模具缓冲区..我期待一个白色的三角形轮廓在黑色背景,但不知道为什么我没有看到任何东西。



编辑:



在声明FBO时

  // render buffer for depth和stencil buffer 
glGenRenderbuffers(1,& depthStencilBuffer);
glBindRenderbuffer(GL_RENDERBUFFER,depthStencilBuffer);
glRenderbufferStorage(GL_RENDERBUFFER,GL_DEPTH24_STENCIL8,width,height);
glBindRenderbuffer(GL_RENDERBUFFER,0);
//将渲染缓冲区作为深度缓冲区附加到fbo
glFramebufferRenderbuffer(GL_FRAMEBUFFER,GL_DEPTH_STENCIL_ATTACHMENT,GL_RENDERBUFFER,depthStencilBuffer);

深度的像素传输:

  QImage FBO :: getDepth()
{
std :: vector< uchar&像素;
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.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 with rgb component



..结果图片是深度和钢板的黑色图片



EDIT



感谢Nicolas的回答我设法使用一个renderbuffer用于深度和模板缓冲区,并提取深度组件以适应 QImage :: Format_ARGB32 使用此代码:

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

std :: vector< GLuint>像素(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> _像素;
for(int i = 0; i <(width * height); i ++){
GLuint color = pixels [i]

float temp =(color& 0xFFFFFF00)>> 8; // 24位深度分量

//重映射temp从0到0xFFFFFF(24位)到
// _temp从0到0xFF(8位)
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;
}

..模板组件还有一些问题(下面的代码不工作:它产生glitches图像):

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

std :: vector< GLuint>像素(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> _像素;
for(int i = 0; i <(width * height); i ++){
GLuint color = pixels [i];

uchar temp =(color& 0x000000FF); //深度组件的最后8位

_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;
}


解决方案


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


你的代码不建议。

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

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

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

这告诉OpenGL将 float 写入你的 。因此,OpenGL将在写入时将像素作为 GLfloat 的数组。

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



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



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



此外,你应该总是 合并深度/模板图片,而不是两个单独的renderbuffer。






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

您似乎不了解像素传输工作。



首先,像素传输格式指定您正在阅读的组件。它指定它们的大小。 GL_DEPTH24_STENCIL8 图片格式 ,而不是像素传输格式。如果要从图像读取深度和模板,请使用 GL_DEPTH_STENCIL 。像素传输格式没有大小。



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



大小来自第二个参数,即像素传输类型。在这种情况下, GL_UNSIGNED_BYTE 意味着它将读取深度和模板,将每个转换为无符号的8位值,并将那些每像素存储到 pixels.data()



深度缓冲区每个像素只存储一个值。深度/模板只存储2.您无法使用OpenGL将其复制到每像素4个组件格式。 因此,如果你稍后建立你的QImage,它必须我一些方法,每个像素需要1或2个值。



一般来说,如果你想要读取深度缓冲区,并且您希望深度缓冲区的数据实际上有意义,您可以这样做:

  std ::向量< GLuint>像素(width * height); // std :: vector value  - 初始化其元素。 

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

如何将这些放在QImage中进行可视化取决于你。但是这会得到一个unsigned int数组,其中高24位是深度分量,低8位是模板。


I'have to render off screen in OpenGL and then pass the image to a QImage. Plus, just for exercise I'd like to transfer to the CPU also the depth and the stencil buffer.

For drawing offscreen I've used Frame Buffer Object with Render Buffer (and not with texture because I don't need the texture).

Pixel transfer operation with color buffers (the actual raw image) works.. I see what I'm expecting. But depth and stencil are not working.. strange image for depth and nothing with for the stencil.

First, the easy part, what I'm actually drawing:

    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();

Here the initalization of the FBO and of the 3 render buffer:

// 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);

And finally here 3 methods for the pixel transfer:

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;

}

Here the 2 screenshot (color and depth, with stencil I get an empty QImage):

The color one is exactly what I'm drawing (flipped but it is normal I think). The depth.. I'm expecting a white image with a gradient gray triangle.. For sure I'm making some mistake in the format of the image (GL_FLOAT?) but I've tried some combinations and this is the best result.. a violet background with a glitchy colors inside it.. and with the stencil buffer.. i'm expecting a white triangle outline in a black background but.. no idea why I don't see anything..

EDIT:

Ok, never use stencil buffer alone, so I've refactored a little bit.

when declaring the 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);

pixel transfer of the depth:

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;
}

the stencil is similar but using p_stencil with rgb component

.. the result image is a black image for both depth and stencil

EDIT

thanks to Nicolas answer I managed to use a renderbuffer for both depth and stencil buffer and extract the depth component to fit a QImage::Format_ARGB32 with this code:

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;
}

.. Still some problems with stencil component (the following code does not work: it produces glitches image):

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;
}

解决方案

The depth.. I'm expecting a white image with a gradient gray triangle..

Your code doesn't suggest that.

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

This creates an array of integers. It's also a memory leak; use std::vector<uchar> instead.

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

This tells OpenGL to write floats to your array of integers. So OpenGL will treat pixels as an array of GLfloats when writing.

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

I'll assume that this is going to interpret this as some kind of 8-bit-per-component integer image format. So you're interpreting floats as 8-bit integers. There's no reason to expect this to behave rationally.

You should either be reading the depth buffer as floats and converting that to your pixel colors in a more reasonable way, or you should be reading the depth buffer as an integer value, letting OpenGL do the greyscale conversion for you.

Also, you should always be using a combined depth/stencil image, not two separate renderbuffers.


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

You don't seem to understand how pixel transfers work.

First, the pixel transfer format specifies which components that you're reading. It does not specify their sizes. GL_DEPTH24_STENCIL8 is an image format, not a pixel transfer format. If you want to read the depth and stencil from an image, you use GL_DEPTH_STENCIL. Pixel transfer formats don't have sizes.

So this function is just giving you an OpenGL error.

The size comes from the second parameter, the pixel transfer type. In this case GL_UNSIGNED_BYTE means that it will read the depth and stencil, convert each into an unsigned, 8-bit value, and store two of those per-pixel into pixels.data().

Depth buffers only store 1 value per pixel. Depth/stencil only store 2. You cannot copy them into a 4-component-per-pixel format with OpenGL. Therefore, however you build your QImage later, it must me some method that takes 1 or 2 values per pixel.

Generally speaking, if you want to read the depth buffer, and you want the depth buffer's data to actually be meaningful, you do this:

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());

How you put those in a QImage for visualization is up to you. But this gets you an array of unsigned ints, where the high 24 bits are the depth component, and the low 8 bits are the stencil.

这篇关于渲染屏幕(使用FBO和RenderBuffer)和像素传输的颜色,深度,模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆