本文介绍了安卓4.3 PBO不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 PBO 采取截图。但是,结果图像是全黑的。这工作完全正常,而不 PBO 。有没有办法,我必须这样做之前先照顾好任何事情吗?

我甚至通过渲染到 FBO 键,然后用试 GLES30.glReadBuffer(GLES30.GL_COLOR_ATTACHMENT0),无希望

 公共无效SetupPBO(){
    GLES30.glGenBuffers(1,pbuffer而使,0);
    GLES30.glBindBuffer(GLES30.GL_PIXEL_PACK_BUFFER,pbuffer而使[0]);
    INT大小=(int)的this.mScreenHeight *(INT)this.mScreenWidth * 4;
    GLES30.glBufferData(GLES30.GL_PIXEL_PACK_BUFFER,大小,空,GLES30.GL_DYNAMIC_READ);
    checkGlError(glReadBuffer);
    GLES30.glBindBuffer(GLES30.GL_PIXEL_PACK_BUFFER,0);
}

私人无效渲染(浮动[] M){
    ....... //正常渲染逻辑
    exportBitmap();
}

 私人无效exportBitmap(){
    INT screenshotSize =(int)的this.mScreenWidth *(INT)this.mScreenHeight;
    ByteBuffer的BB = ByteBuffer.allocateDirect(screenshotSize * 4);
    bb.order(ByteOrder.nativeOrder());

    //设置目标帧缓冲读
    GLES30.glReadBuffer(GLES30.GL_FRONT);
    checkGlError(glReadBuffer);
    GLES30.glBindBuffer(GLES30.GL_PIXEL_PACK_BUFFER,pbuffer而使[0]);
    GLES30.glReadPixels(0,0,(int)的mScreenWidth,(int)的mScreenHeight,GL10.GL_RGBA,GL10.GL_UNSIGNED_BYTE,BB); //< ------不工作?????

    INT pixelsBuffer [] =新的INT [screenshotSize]
    。bb.asIntBuffer()得到(pixelsBuffer);
    BB = NULL;

    的for(int i = 0; I< screenshotSize ++我){
        //阿尔法和绿色通道的位置是pserved而$ P $
        //红色和蓝色互换
        pixelsBuffer [I] =((pixelsBuffer [1]  - 安培; 0xff00ff00))
                    | ((pixelsBuffer [1]  - 安培; 0x000000ff)<< 16)
                    | ((pixelsBuffer [1]  - 安培; 0x00ff0000)>> 16);
    }

    点阵位图= Bitmap.createBitmap((INT)mScreenWidth,(INT)mScreenHeight,Bitmap.Config.ARGB_8888);
    bitmap.setPixels(pixelsBuffer,screenshotSize  - (int)的mScreenWidth, - (int)的mScreenWidth,0,0,(int)的mScreenWidth,(int)的mScreenHeight);
    SaveBitmap(位);
}
 

解决方案

  GLES30.glReadPixels(0,0,(INT)mScreenWidth,(INT)mScreenHeight,GL10.GL_RGBA,GL10。 GL_UNSIGNED_BYTE,BB);
 

BB是跨preT为您的PBO抵消。因此,你写了缓冲液(在某些驱动程序中的code引起死机)。你应该通过BB 0代替。以检索从PBO数据使用glMapBuffer。

I am using PBO to take screenshot. However, the result image is all black. It works perfectly fine without PBO. Is there any thing that I need to take care before doing this ?

I even tried by rendering to a FBO and then use GLES30.glReadBuffer(GLES30.GL_COLOR_ATTACHMENT0), no hope

public void SetupPBO(){
    GLES30.glGenBuffers(1, pbuffers, 0);
    GLES30.glBindBuffer(GLES30.GL_PIXEL_PACK_BUFFER, pbuffers[0]);
    int size = (int)this.mScreenHeight *  (int)this.mScreenWidth * 4;
    GLES30.glBufferData(GLES30.GL_PIXEL_PACK_BUFFER, size, null, GLES30.GL_DYNAMIC_READ);
    checkGlError("glReadBuffer");
    GLES30.glBindBuffer(GLES30.GL_PIXEL_PACK_BUFFER, 0);
}

private void Render(float[] m) {
    .......//Normal render logic
    exportBitmap();
}

 private void exportBitmap() {
    int screenshotSize = (int)this.mScreenWidth * (int)this.mScreenHeight;
    ByteBuffer bb = ByteBuffer.allocateDirect(screenshotSize * 4);
    bb.order(ByteOrder.nativeOrder());

    // set the target framebuffer to read
    GLES30.glReadBuffer(GLES30.GL_FRONT);
    checkGlError("glReadBuffer");
    GLES30.glBindBuffer(GLES30.GL_PIXEL_PACK_BUFFER, pbuffers[0]);
    GLES30.glReadPixels(0, 0, (int)mScreenWidth, (int)mScreenHeight, GL10.GL_RGBA, GL10.GL_UNSIGNED_BYTE, bb);  //<------ not working ?????

    int pixelsBuffer[] = new int[screenshotSize];
    bb.asIntBuffer().get(pixelsBuffer);
    bb = null;

    for (int i = 0; i < screenshotSize; ++i) {
        // The alpha and green channels' positions are preserved while the
        // red and blue are swapped
        pixelsBuffer[i] = ((pixelsBuffer[i] & 0xff00ff00))
                    | ((pixelsBuffer[i] & 0x000000ff) << 16)
                    | ((pixelsBuffer[i] & 0x00ff0000) >> 16);
    }

    Bitmap bitmap = Bitmap.createBitmap((int)mScreenWidth, (int)mScreenHeight, Bitmap.Config.ARGB_8888);
    bitmap.setPixels(pixelsBuffer, screenshotSize - (int)mScreenWidth, -(int)mScreenWidth, 0, 0, (int)mScreenWidth, (int)mScreenHeight);
    SaveBitmap(bitmap);
}
解决方案
GLES30.glReadPixels(0, 0, (int)mScreenWidth, (int)mScreenHeight, GL10.GL_RGBA, GL10.GL_UNSIGNED_BYTE, bb);

bb is interpret as an offset in your PBO. Thus you're writing out of buffer (On some drivers this code cause crash). You should pass 0 instead of bb. To retrive the data from PBO use glMapBuffer.

这篇关于安卓4.3 PBO不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-13 18:41