osg中相机的attach可以直接输出到pbo存储,因此我们可以使用其做点事。
1、首先初始化pbo,开辟内存,要在渲染线程执行,只需初始化一次
//初始化
mutable GLuint pbo[1];
mutable void* pixelData;
mutable GLuint pixelDataSize;
pixelDataSize = /* canvas->width() * canvas->height() **/ 4 * sizeof(float);
pixelData = (void*)malloc(pixelDataSize);
auto functions = QOpenGLContext::currentContext()->versionFunctions<QOpenGLFunctions_4_5_Core>();
functions->glGenBuffers(1, pbo);
functions->glBindBuffer(GL_PIXEL_PACK_BUFFER, pbo[0]);
functions->glBufferData(GL_PIXEL_PACK_BUFFER, pixelDataSize, pixelData, GL_DYNAMIC_COPY);
functions->glBindBuffer(GL_PIXEL_PACK_BUFFER, 0);
m_first = false;
2、 PBO的读取
//pbo读取
float* dat = new float[4 * wd * ht];
auto functions = QOpenGLContext::currentContext()->versionFunctions<QOpenGLFunctions_4_5_Core>();
// PBO方式:缓冲区捆绑GL_PIXEL_PACK_BUFFER 准备接收glReadPixels从屏幕读取的像素。
functions->glBindBuffer(GL_PIXEL_PACK_BUFFER, pbo[0]);
glReadPixels(x, y, wd, ht, GL_RGBA, GL_FLOAT,
NULL); //最后一个参数为NULL,直接将读取出的内容存到了PBO
dat = (float*)(functions->glMapBuffer(GL_PIXEL_PACK_BUFFER, GL_READ_ONLY));
functions->glUnmapBuffer(GL_PIXEL_PACK_BUFFER);
functions->glBindBuffer(GL_PIXEL_PACK_BUFFER, 0); //释放捆绑
//pbo存储到纹理
// Next bind the PBO as the unpack buffer, then push the pixels straight into the texture
//缓冲区捆绑GL_PIXEL_UNPACK_BUFFER 准备将内容交给glTexImage2D加载放到对应的纹理对象上
glBindBuffer(GL_PIXEL_UNPACK_BUFFER, pixBuffObjs[0]);
// Setup texture unit for new blur, this gets imcremented every frame
glActiveTexture(GL_TEXTURE0);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB8, screenWidth, screenHeight, 0, GL_RGB, GL_UNSIGNED_BYTE, NULL); //可看见最后一个data数据是NULL,实际他是从PBO读取
glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0);