Combine image with its mask with Android shaders?

Viewed 737

I'm developing an app for android and I use GL for some behaivors and I have a problem with an specific case.

I have an image of an ambient (for example, a room), and also y have a mask that is all white and marks in black the location of the a wall.

Well I want to make a mix with this two images and get the result (for example, tint the wall in red)

But first at all I need to indicate to the shader who are the two images, and here I have troubles.

Here is my fragment shader:

public static final String fs_Image_mix =
        "precision mediump float;" +

                "arying LOWP vec4 v_color;" +
                "varying vec2 v_texCoord;" +

                "uniform sampler2D s_texture;" +
                "uniform sampler2D s_mask;" +

                "void main() {" +
                //"  gl_FragColor = texture2D( s_texture, v_texCoord );" +

                "   vec4 color0 = texture2D(s_texture, v_texCoords);" +
                "   vec4 color1 = texture2D(s_mask, v_texCoords);" +

                "   float mask = texture2D(s_mask, v_texCoords).a;" +
                "   mask = step(1.0, mask);" +

                "   gl_FragColor = v_color * mix(color0, color1, mask);" +
                "}";

And here is my Draw function were I pass the background texture:

public void Draw(float[] mvpMatrix, int[] textureHandle, int textureIndex){

    GLES20.glActiveTexture(GLES20.GL_TEXTURE0);

    // Apply transparency to the texture
    GLES20.glEnable(GLES20.GL_BLEND);
    GLES20.glBlendFunc(GLES20.GL_SRC_ALPHA, GLES20.GL_ONE_MINUS_SRC_ALPHA);

    // Ensure that uses the correct texture
    GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, textureHandle[textureIndex]);

    // get handle to vertex shader's vPosition member
    int mPositionHandle = GLES20.glGetAttribLocation(GraphicTools.sp_Image_mix, "vPosition");

    // Enable generic vertex attribute array
    GLES20.glEnableVertexAttribArray(mPositionHandle);

    // Prepare the triangle coordinate data
    GLES20.glVertexAttribPointer(mPositionHandle, COORDS_PER_VERTEX,
            GLES20.GL_FLOAT, false,
            vertexStride, vertexBuffer);

    // Get handle to texture coordinates location
    int mTexCoordLoc = GLES20.glGetAttribLocation(GraphicTools.sp_Image_mix, "a_texCoord" );

    // Enable generic vertex attribute array
    GLES20.glEnableVertexAttribArray ( mTexCoordLoc );

    // Prepare the texturecoordinates
    GLES20.glVertexAttribPointer ( mTexCoordLoc, 2, GLES20.GL_FLOAT,
            false,
            0, uvBuffer);

    // Get handle to shape's transformation matrix
    int mtrxhandle = GLES20.glGetUniformLocation(GraphicTools.sp_Image_mix, "uMVPMatrix");

    // Apply the projection and view transformation
    GLES20.glUniformMatrix4fv(mtrxhandle, 1, false, mvpMatrix, 0);

    // Get handle to textures locations
    int mSamplerLoc = GLES20.glGetUniformLocation (GraphicTools.sp_Image_mix, "s_texture" );

    // Set the sampler texture unit to 0, where we have saved the texture.
    GLES20.glUniform1i ( mSamplerLoc, 0);

    // Draw the triangle
    GLES20.glDrawElements(GLES20.GL_TRIANGLES, indices.length,
            GLES20.GL_UNSIGNED_SHORT, drawListBuffer);

    // Disable vertex array
    GLES20.glDisableVertexAttribArray(mPositionHandle);
    GLES20.glDisableVertexAttribArray(mTexCoordLoc);

}

How can I fill "s_mask" variable with desired mask? remember that textureHandle[textureIndex] is the background image.

1 Answers
Related