Android OpenGL combination of SurfaceTexture (external image) and ordinary texture

Lukáš Jezný picture Lukáš Jezný · Nov 14, 2012 · Viewed 11.4k times · Source

i would like to mix camera preview SurfaceTexture with some overlay texture. I am using these shaders for processing:

    private final String vss = "attribute vec2 vPosition;\n"
        + "attribute vec2 vTexCoord;\n"
        + "varying vec2 texCoord;\n"
        + "void main() {\n" 
        + "  texCoord = vTexCoord;\n"
        + "  gl_Position = vec4 ( vPosition.x, vPosition.y, 0.0, 1.0 );\n"
        + "}";

private final String fss = "#extension GL_OES_EGL_image_external : require\n"
        + "precision mediump float;\n"
        + "uniform samplerExternalOES sTexture;\n"
        + "uniform sampler2D filterTexture;\n"
        + "varying vec2 texCoord;\n"
        + "void main() {\n"
        +"  vec4 t_camera = texture2D(sTexture,texCoord);\n"
        //+"  vec4 t_overlayer = texture2D(filterTexture, texCoord);\n" 
        //+ "  gl_FragColor = t_overlayer;\n" + "}";
        + "  gl_FragColor = t_camera;\n" + "}";

My goal is to mix t_camera and t_overlayer. When i show t_camera or t_overlayer separately, it works (showing camera preview or texture). But when i uncomment t_overlayer, then t_camera became black (somehow badly sampled). My overlayer texture is 512x512 and CLAMPT_TO_EDGE. This problem occurs only for example on: Android Emulator, HTC Evo 3D. But on SGS3, HTC One X, it works just fine.

What is wrong? Is it Evo 3D missing some extension or what?

Answer

Marco picture Marco · Mar 27, 2013

I imagine you have this problem because you are not setting the correct texture id on your code. This is a tipical error on assumptions which seem logical, but is actually not defined so on the documentation. If you check the documentation of this extension you see the following (edited) TEXT:

Each TEXTURE_EXTERNAL_OES texture object may require up to 3 texture image units for each texture unit to which it is bound. When is set to TEXTURE_EXTERNAL_OES this value will be between 1 and 3 (inclusive). For other valid texture targets this value will always be 1. Note that, when a TEXTURE_EXTERNAL_OES texture object is bound, the number of texture image units required by a single texture unit may be 1, 2, or 3, while for other texture objects each texture unit requires exactly 1 texture image unit.

This means that at leas one additional will work, provided that you use id 0 for it. In your case:

GLES20.glUniform1i(sTextureHandle, 1);
GLES20.glActiveTexture(GLES20.GL_TEXTURE1);
GLES20.glBindTexture(GLES11Ext.GL_TEXTURE_EXTERNAL_OES,
        sTextureId);

For your 2D texture:

GLES20.glUniform1i(filterTextureHandle, 0);
GLES20.glActiveTexture(GLES20.GL_TEXTURE0);
GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, filterTextureID);

I'm sure this will workout for you.