Opengl创建FBO-致命信号11错误

时间:2015-07-30 06:39:57

标签: android opengl-es opengl-es-2.0

在我的安卓游戏中,我想在FBO中渲染我的场景并附加一个纹理,稍后使用该纹理,但当我使用glGenFramebuffers时,我收到此错误:

07-30 06:17:27.839: A/libc(1338): Fatal signal 11 (SIGSEGV), code 1, fault addr 0xd0 in tid 1353 (GLThread 157)

如果需要,这是我的代码:

createTexture功能:

public int CreateTexture(int w, int h){
        int[] textureId = new int[1];
        int i;      
        i = GLES20.glGetError();
        GLES20.glGenTextures(1, textureId,0);
        GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, textureId[0]);
        GLES20.glTexImage2D(GLES20.GL_TEXTURE_2D, 0, GLES20.GL_RGBA, w, h, 0, GLES20.GL_RGBA, GLES20.GL_FLOAT, null);
ijad kon vali ba hichi poresh nakon hanooz
        GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MAG_FILTER, GLES20.GL_NEAREST);
        GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MIN_FILTER, GLES20.GL_NEAREST);
        GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_S, GLES20.GL_CLAMP_TO_EDGE);
        GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_T, GLES20.GL_CLAMP_TO_EDGE);

        if(i!=0){
            Log.d("ERROR", "ERROR Happend"+i+"");
            return i;
        }

        GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, 0);
        return textureId[0];
    } 

onDrawFrame

public synchronized void onDrawFrame(GL10 gl) {
    if(first){
    int renderTexture;
    renderTexture = CreateTexture(128,128 );
    GLES20.glGenFramebuffers(1, FBO,0);
    GLES20.glFramebufferTexture2D(GLES20.GL_FRAMEBUFFER, GLES20.GL_COLOR_ATTACHMENT0, GLES20.GL_TEXTURE_2D, renderTexture, 0);
    first = false;
    }
    GLES20.glBindFramebuffer(GLES20.GL_FRAMEBUFFER, FBO[0]);
    // Some 'global' settings.
    //gl.glEnableClientState(GLES20.GL_VERTEX_ATTRIB_ARRAY_ENABLED);
    gl.glEnableClientState(GLES10.GL_VERTEX_ARRAY);

    gl.glVertexPointer(3, GLES20.GL_FLOAT, 0, mBufVertices);
    // Enable color array.
    gl.glEnableClientState(GLES10.GL_COLOR_ARRAY);
    gl.glColorPointer(4, GLES20.GL_FLOAT, 0, mBufColors);
    //inja khasiate transparent ro faAl mikonim ke betoonim too CurlPage be kaaghaz alpha bedim
    gl.glEnable(GLES20.GL_BLEND);
    gl.glBlendFunc(GLES20.GL_SRC_ALPHA, GLES20.GL_ONE_MINUS_SRC_ALPHA);
    gl.glDisable(GLES10.GL_LIGHTING);
    gl.glDisable(GLES20.GL_TEXTURE_2D);
    gl.glDrawArrays(GLES20.GL_TRIANGLE_STRIP, 0, mVerticesCountFront);
    int backStartIdx = Math.max(0, mVerticesCountFront - 2);
    //Log.d("mVerticesCountFront", ""+mVerticesCountFront+"");
    int backCount = mVerticesCountFront + mVerticesCountBack - backStartIdx;

    // Draw back facing blank vertices.
    //gl.glColor4f(1.0f, 0.0f, 0.0f, 0.5f);
    gl.glDrawArrays(GLES20.GL_TRIANGLE_STRIP, backStartIdx, backCount);

    // Disable textures and color array.
    gl.glDisableClientState(GLES10.GL_TEXTURE_COORD_ARRAY);
    gl.glDisableClientState(GLES10.GL_COLOR_ARRAY);

    GLES20.glBindFramebuffer(GLES20.GL_FRAMEBUFFER, 0);
    gl.glDisableClientState(GLES10.GL_VERTEX_ARRAY);
}

为什么我会收到这样的错误?

1 个答案:

答案 0 :(得分:2)

首先,你每次在onDrawFrame()上创建一个新的纹理和一个新的FBO,每当你的应用程序想要绘制一个框架时,它将分配更多将以灾难结束的内存。

你需要在开始渲染之前一次创建纹理和FBO,比如initFrameBuffer(GL10 gl),并保持生成FBO整数。

然后只是

public synchronized void onDrawFrame(GL10 gl) {
GLES20.glBindFramebuffer(GLES20.GL_FRAMEBUFFER, FBO[0]);
.... Draw calls.
}