1

经过几次之后,我收到了致命信号 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,
};

任何提示、链接甚至代码片段都将不胜感激!

先感谢您

4

1 回答 1

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);

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

于 2013-04-11T17:52:33.100 回答