onDrawFrame中的glDrawArrays上的致命信号(GLSurfaceView.Renderer,SurfaceTexture.OnFrameAvailableListener)

时间:2013-04-11 17:46:02

标签: android opengl-es-2.0 shader textures

我发现了很多致命信号11

GLES20.glDrawArrays(GLES20.GL_TRIANGLE_STRIP, 0, 4);

在我的OnDrawFrame函数中调用。在崩溃之前正确绘制了纹理,我相信这意味着我的顶点和片段着色器工作正常。

我想做的是使用GLSurfaceView.Renderer和SurfaceTexture.OnFrameAvailableListener将两个纹理混合在一起。

第一个纹理是一个实时相机预览SurfaceTexture,它可以自己运行。第二个tetxure只是一个位图图像,它也可以自己工作。但是将它们结合起来会导致崩溃。

我的OnDrawFrame如下:

public void onDrawFrame(GL10 glUnused) { 
    GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT);

    synchronized(this) { 
        if (updateSurface) {
            mSurface.updateTexImage();
            mSurface.getTransformMatrix(mSTMatrix);
            updateSurface = false;
        }
    } 

    maPositionHandle = GLES20.glGetAttribLocation(mProgram, "aPosition");
    maTextureHandle = GLES20.glGetAttribLocation(mProgram, "aTextureCoord");
    muMVPMatrixHandle = GLES20.glGetUniformLocation(mProgram, "uMVPMatrix"); 
    muSTMatrixHandle = GLES20.glGetUniformLocation(mProgram, "uSTMatrix");
    muCRatioHandle = GLES20.glGetUniformLocation(mProgram, "uCRatio");

    GLES20.glBindTexture(GL_TEXTURE_EXTERNAL_OES, textures[0]);
    GLES20.glActiveTexture(GLES20.GL_TEXTURE0);
    int t1h = GLES20.glGetUniformLocation ( mProgram, "sTexture1" );
    //        GLES20.glUniform1i(t1h, textures[0]);
    GLES20.glUniform1i(t1h, 1);

    GLES20.glBindTexture(GL_TEXTURE_EXTERNAL_OES, textures[1]);
    GLES20.glActiveTexture(GLES20.GL_TEXTURE1);
    int t2h = GLES20.glGetUniformLocation ( mProgram, "sTexture2" );
    GLES20.glUniform1i(t2h, 2);

    GLES20.glUseProgram(mProgram);

    GLES20.glEnable(GLES20.GL_BLEND);
    GLES20.glBlendFunc(GLES20.GL_SRC_ALPHA, GLES20.GL_ONE_MINUS_SRC_ALPHA); 

    mTriangleVertices.position(0);
    GLES20.glEnableVertexAttribArray(maPositionHandle);
    GLES20.glVertexAttribPointer(maPositionHandle, 3, GLES20.GL_FLOAT, false,
            4*5, mTriangleVertices);

    mTriangleVertices.position(3);
    GLES20.glEnableVertexAttribArray(maTextureHandle);
    GLES20.glVertexAttribPointer(maTextureHandle, 3, GLES20.GL_FLOAT, false,
            4*5, mTriangleVertices);

    Matrix.multiplyMM(mMVPMatrix, 0, mVMatrix, 0, mMMatrix, 0);  
    Matrix.multiplyMM(mMVPMatrix, 0, mProjMatrix, 0, mMVPMatrix, 0);

    GLES20.glUniformMatrix4fv(muMVPMatrixHandle, 1, false, mMVPMatrix, 0);
    GLES20.glUniformMatrix4fv(muSTMatrixHandle, 1, false, mSTMatrix, 0);
    GLES20.glUniform1f(muCRatioHandle, mCameraRatio);

    GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, 0);
    GLES20.glBindBuffer(GLES20.GL_ELEMENT_ARRAY_BUFFER, 0);

    GLES20.glDrawArrays(GLES20.GL_TRIANGLE_STRIP, 0, 4);
    GLES20.glDisableVertexAttribArray(maPositionHandle);
    GLES20.glDisableVertexAttribArray(maTextureHandle);
    //        GLES20.glFlush();
}

其中mTriangleVertices是:

mTriangleVertices = ByteBuffer.allocateDirect(mTriangleVerticesData.length
            * FLOAT_SIZE_BYTES).order(ByteOrder.nativeOrder()).asFloatBuffer();
mTriangleVertices.put(mTriangleVerticesData).position(0);

和mTriangleVerticesData是:

private final float[] mTriangleVerticesData = {
    // X, Y, Z, U, V
    -1.0f, -1.0f, 0, 0.f, 0.f,
    1.0f, -1.0f, 0, 1.f, 0.f,
    -1.0f,  1.0f, 0, 0.f, 1.f,
    1.0f,   1.0f, 0, 1.f, 1.f,
};

非常感谢任何提示,链接甚至代码片段!

提前谢谢

1 个答案:

答案 0 :(得分:2)

不知道这是否是导致崩溃的原因,但至少为着色器分配了一个太高的纹理ID。例如;

GLES20.glActiveTexture(GLES20.GL_TEXTURE0);
int t1h = GLES20.glGetUniformLocation ( mProgram, "sTexture1" );
GLES20.glUniform1i(t1h, 1);

应该阅读

GLES20.glActiveTexture(GLES20.GL_TEXTURE0);
int t1h = GLES20.glGetUniformLocation ( mProgram, "sTexture1" );
GLES20.glUniform1i(t1h, 0);

编辑:啊,这可能是导致崩溃的原因:

GLES20.glVertexAttribPointer(maTextureHandle, 3, GLES20.GL_FLOAT, false,
        4*5, mTriangleVertices);

应该是

GLES20.glVertexAttribPointer(maTextureHandle, 2, GLES20.GL_FLOAT, false,
        4*5, mTriangleVertices);

更可能因为每个顶点有两个纹理值。