Images and mask in OpenGL ES 2.0

后端 未结 2 936
栀梦
栀梦 2020-12-11 02:09

I\'m learning OpenGL ES 2.0 and I\'d like to create an App to better understand how it works. The App has a set of filter that the user can apply on images (I know, nothing

2条回答
  •  不思量自难忘°
    2020-12-11 03:11

    There is no need for multiple shaders or framebuffers, just multiple texture units. Simply use 3 texture units which are all indexed by the same texture coordinates and use the Mask texture to select between the other two textures. The fragment shader would look like this:

    uniform sampler2D uTextureUnit_1;
    uniform sampler2D uTextureUnit_2;
    uniform sampler2D uTextureMask;
    varying vec2 vTextureCoordinates;
    
    void main()
    {
        vec4 vColor_1 = texture2D(uTextureUnit_1, vTextureCoordinates);
        vec4 vColor_2 = texture2D(uTextureUnit_2, vTextureCoordinates);
        vec4 vMask = texture2D(uTextureMask, vTextureCoordinates);
    
        if (vMask.r > 0.5)
            gl_FragColor = vColor_1;
        else
            gl_FragColor = vColor_2;
    }
    

    You can see that using a third texture unit just to do a binary test on the Red channel is not very efficient, so it would be better to encode the mask into the alpha channels of Textures 1 or 2, but this should get you started.

提交回复
热议问题