我必须在 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):
颜色和我画的一模一样(翻转了,但我认为这是正常的)。深度..我期待一个带有渐变灰色三角形的白色图像..当然我在图像格式上犯了一些错误(GL_FLOAT
?),但我尝试了一些组合这是最好的结果.. 紫罗兰色背景,里面有故障颜色.. 以及模板缓冲区.. 我期待黑色背景中的白色三角形轮廓,但是.. 不知道为什么我看不到任何东西..
编辑:
好吧,永远不要单独使用模板缓冲区,所以我重构了一点。
声明 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 组件
..结果图像的深度和模板都是黑色图像
编辑
感谢尼古拉斯的回答,我成功地使用渲染缓冲区作为深度和模板缓冲区,并提取深度组件以适合 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;
}
最佳答案
The depth.. I'm expecting a white image with a gradient gray triangle..
您的代码没有暗示这一点。
uchar *pixels;
pixels = new uchar[width * height * 4];
这将创建一个整数数组。这也是内存泄漏;使用std::vector<uchar>
相反。
glReadPixels( 0,0, width, height, GL_DEPTH_COMPONENT, GL_FLOAT, pixels);
这告诉 OpenGL 将 float 写入整数数组。所以OpenGL会处理pixels
作为 GLfloat
的数组写作时的 s。
QImage qi = QImage(pixels, width, height, QImage::Format_ARGB32);
我假设这会将其解释为某种每组件 8 位整数图像格式。所以你正在解释 float
s 为 8 位整数。没有理由期望这种行为是理性的。
您应该将深度缓冲区作为 float 读取,并以更合理的方式将其转换为像素颜色,或者您应该将深度缓冲区作为整数值读取,让 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_STENCIL8
是 an 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 位是模板。
关于c++ - 离屏渲染(使用 FBO 和 RenderBuffer)以及颜色、深度、模板的像素传输,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17711673/