Kotlin

Creating shader for color inversion

Now our rendererer needs shader let's implement it

  1. Add fragmentShaderCode to the MyRenderer class:
private val fragmentShaderCode = """
    precision mediump float;
    uniform sampler2D Ytex;
    uniform sampler2D Utex;
    uniform sampler2D Vtex;
    varying vec2 vTextureCoord;

    void main() {
        float nx = vTextureCoord.x;
        float ny = vTextureCoord.y;

        float y = texture2D(Ytex, vec2(nx, ny)).r;
        float u = texture2D(Utex, vec2(nx, ny)).r - 0.5;
        float v = texture2D(Vtex, vec2(nx, ny)).r - 0.5;

        // Inverted Y color
        y = 1.0 - 1.1643 * (y - 0.0625);

        float r = y + 1.5958 * v;
        float g = y - 0.39173 * u - 0.81290 * v;
        float b = y + 2.017 * u;

        gl_FragColor = vec4(r, g, b, 1.0);
    }
""".trimIndent()
  1. To render the video frames, the renderer class uses OpenGL shaders. In this sample shader produces the inverted color effect, more precisely this is achieved by this line y = 1.0 - 1.1643 * (y - 0.0625); which is inside the fragmentShaderCode

  2. For full implementation of the renderer check the sample app implementation here