在OpenGL中将掩码应用于视频

问题描述 投票:0回答:1

我是OpenGL的新生。我试着找到解决问题的方法。我录制了带alpha蒙版的视频。视频RGB和底部的顶部是alpha。据我所知需要更改我的着色器,但我不明白如何做到这一点。

顶点着色器

    private static final String vertexShaderCode =
        "attribute vec4 vPosition;" +
                "attribute vec4 vTexCoordinate;" +
                "uniform mat4 textureTransform;" +
                "varying vec2 v_TexCoordinate;" +
                "void main() {" +
                "   v_TexCoordinate = (textureTransform * vTexCoordinate).xy;" +
                "   gl_Position = vPosition;" +
                "}";

片段着色器

    private static final String fragmentShaderCode =
        "#extension GL_OES_EGL_image_external : require\n" +
                "precision mediump float;" +
                "uniform samplerExternalOES texture;" +
                "varying vec2 v_TexCoordinate;" +
                "void main () {" +
                "    vec4 color = texture2D(texture, v_TexCoordinate);" +
                "    gl_FragColor = color;" +
                "}";

enter image description here

android opengl-es
1个回答
1
投票

要跳过片段,可以在片段着色器中使用discard关键字。

您必须对纹理进行2次查找。上半部分中的第一个用于获取RGB颜色通道,第二个用于下半部分以获取红色通道中的掩码:

vec2 c_uv  = vec2(texture.x, texture.y*0.5); 
vec4 color = texture2D(c_uv, v_TexCoordinate);

vec2  m_uv = vec2(texture.x, 0.5 + texture.y*0.5); 
float mask = texture2D(m_uv, v_TexCoordinate).r;

片段着色器可能如下所示:

#extension GL_OES_EGL_image_external : require
precision mediump float;

uniform samplerExternalOES texture;
varying vec2 v_TexCoordinate;

void main () {

    vec2  m_uv = vec2(v_TexCoordinate.x, 0.5 + v_TexCoordinate.y*0.5); 
    float mask = texture2D(texture, m_uv).r;
    if ( mask < 0.5 )
        discard;

    vec2 c_uv  = vec2(v_TexCoordinate.x, v_TexCoordinate.y*0.5); 
    vec4 color = texture2D(texture, c_uv);  
    gl_FragColor = color;
}
© www.soinside.com 2019 - 2024. All rights reserved.