我想使用GLSurfaceView.Renderer和SurfaceTexture.OnFrameAvailableListener將兩個紋理混合在一起。Android OpenGL ES 2.0多紋理和相機
的第一個紋理是一個活生生的攝像頭預覽表面紋理,其通過自身的(質量不好工作得很好,但那是另一回事:)
第二tetxure只是一個位圖圖像,這也是由它自己的正常工作。
對於這兩種紋理我使用:
GLES20.glActiveTexture(GLES20.GL_TEXTUREX); //GL_TEXTURE0 and GL_TEXTURE1 respectively
GLES20.glBindTexture(GL_TEXTURE_EXTERNAL_OES, mTextureX);
GLES20.glTexParameteri(GL_TEXTURE_EXTERNAL_OES, GLES20.GL_TEXTURE_MIN_FILTER, GLES20.GL_LINEAR);
GLES20.glTexParameteri(GL_TEXTURE_EXTERNAL_OES, GLES20.GL_TEXTURE_MAG_FILTER, GLES20.GL_NEAREST);
GLES20.glTexParameteri(GL_TEXTURE_EXTERNAL_OES, GLES20.GL_TEXTURE_WRAP_S, GLES20.GL_CLAMP_TO_EDGE);
GLES20.glTexParameteri(GL_TEXTURE_EXTERNAL_OES, GLES20.GL_TEXTURE_WRAP_T, GLES20.GL_CLAMP_TO_EDGE);
內onSurfaceCreated
。
在我OnDrawFrame我:
mTriangleVertices.position(TRIANGLE_VERTICES_DATA_POS_OFFSET);
GLES20.glEnableVertexAttribArray(maPositionHandle);
checkGlError("glEnableVertexAttribArray maPositionHandle");
GLES20.glVertexAttribPointer(maPositionHandle, 3, GLES20.GL_FLOAT, false,
TRIANGLE_VERTICES_DATA_STRIDE_BYTES, mTriangleVertices);
checkGlError("glVertexAttribPointer maPosition");
mTriangleVertices.position(TRIANGLE_VERTICES_DATA_UV_OFFSET);
GLES20.glEnableVertexAttribArray(maTextureHandle);
checkGlError("glEnableVertexAttribArray maTextureHandle");
GLES20.glVertexAttribPointer(maTextureHandle, 3, GLES20.GL_FLOAT, false,
TRIANGLE_VERTICES_DATA_STRIDE_BYTES, mTriangleVertices);
checkGlError("glVertexAttribPointer maTextureHandle");
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.glDrawArrays(GLES20.GL_TRIANGLE_STRIP, 0, 4);
checkGlError("glDrawArrays");
我的着色器:
private final String mVertexShader =
"uniform mat4 uMVPMatrix;\n" +
"uniform mat4 uSTMatrix;\n" +
"uniform float uCRatio;\n" +
"attribute vec4 aPosition;\n" +
"attribute vec4 aTextureCoord;\n" +
"varying vec2 vTextureCoord;\n" +
"varying vec2 vTextureNormCoord;\n" +
"void main() {\n" +
" gl_Position = uMVPMatrix * aPosition;\n" +
" vTextureCoord = (uSTMatrix * aTextureCoord).xy;\n" +
" vTextureNormCoord = aTextureCoord.xy;\n" +
"}\n";
和
private final String mFragmentShader =
"#extension GL_OES_EGL_image_external : require\n" +
"precision mediump float;\n" +
"varying vec2 vTextureCoord;\n" +
"varying vec2 vTextureNormCoord;\n" +
"uniform samplerExternalOES sTexture;\n" +
"uniform samplerExternalOES sTexture1;\n" +
"void main() {\n" +
" vec4 temp_FragColor1 = texture2D(sTexture, vTextureCoord);\n" +
" vec4 temp_FragColor2= texture2D(sTexture1, vTextureCoord);\n" +
"gl_FragColor = temp_FragColor1;\n" +
"}\n";
的什麼,我試圖做的想法,是在該片段中着色器,要有
gl_FragColor = temp_FragColor1 + temp_FragColor2;
與相機頂部的位圖紋理實時預覽(位圖紋理與相機紋理混合)。因此,如果位圖紋理片段的alpha小於1,則應顯示相機紋理的片段。 當我嘗試這個,我得到一個致命的信號11.
任何提示,鏈接,甚至代碼片段將不勝感激!
提前謝謝
檢查''mix''着色器命令。如果我正確理解你的目標,那麼使用alpha作爲混合因子應該能夠做到這一點 – harism 2013-04-10 17:46:37