Android通过openGL实现视频贴纸功能
GLSL代码
1.vertex代码,文件vertex_filter_stricker.glsl
attribute vec2 inputTextureCoordinate; //纹理坐标
attribute vec4 position;// 顶点坐标
varying vec2 textureCoordinate;//纹理坐标点变换后输出
void main() {
gl_Position = position;
textureCoordinate = inputTextureCoordinate;
}
2.fragment代码,文件fragment_filter_stricker.glsl
//设置精度
precision highp float;
varying highp vec2 textureCoordinate;
uniform sampler2D videoTexture;
uniform sampler2D uImageTexture;
uniform vec4 imageRect;
void main(){
lowp vec4 c1 = texture2D(videoTexture, textureCoordinate);
lowp vec2 vCamTextureCoord2 = vec2(textureCoordinate.x, 1.0-textureCoordinate.y);
if (vCamTextureCoord2.x>imageRect.r && vCamTextureCoord2.x<imageRect.b && vCamTextureCoord2.y>imageRect.g && vCamTextureCoord2.y<imageRect.a)
{
vec2 imagexy = vec2((vCamTextureCoord2.x-imageRect.r)/(imageRect.b-imageRect.r), (vCamTextureCoord2.y-imageRect.g)/(imageRect.a-imageRect.g));
lowp vec4 c2 = texture2D(uImageTexture, imagexy);
lowp vec4 outputColor = c2+c1*c1.a*(1.0-c2.a);
outputColor.a = 1.0;
gl_FragColor = outputColor;
} else {
gl_FragColor = c1;
}
}
Java代码
package com.dong.opencamera.core