osg中pbo的使用

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);
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值