底片效果
camera_ver.vert 数据
attribute vec4 vPosition;//变量 float[4] 一个顶点 java 传过来
attribute vec2 vCoord;//传给片元进行采样的纹理坐标
varying vec2 aCoord;//易变变量 和片元写的一模一样 会传给片元
uniform mat4 vMatrix;
void main() {
// gl_Position = vec4(vec3(0.0), 1.0);
// 内置变量:把坐标点赋值给gl_position就ok
gl_Position = vPosition;
aCoord = (vMatrix * vec4(vCoord,1.0,1.0)).xy;;
}
camera_frag.frag 着色器
#extension GL_OES_EGL_image_external : require
precision mediump float;//数据精度
varying vec2 aCoord;
uniform samplerExternalOES vTexture;//samplerExternalOES 图片,采样器
void main() {
// gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0);
vec4 rgba = texture2D(vTexture,aCoord); //rgba
// gl_FragColor = rgba;//rgba
gl_FragColor = vec4(1.-rgba.r,1.-rgba.g,1.-rgba.b,rgba.a); // 底片效果
// gl_FragColor = vec4(1.-rgba.r,1.-rgba.g,1.-rgba.b,1.-rgba.a); // 底片效果 荧光
}
读取文件,其实就是读取上面的字符串
加载参数
String vertexSharder = OpenGLUtils.readRawTextFile(context, R.raw.camera_vert);
String fragSharder = OpenGLUtils.readRawTextFile(context, R.raw.camera_frag);
//着色器程序准备好
program = OpenGLUtils.loadProgram(vertexSharder, fragSharder);
//获取程序中的变量 索引
vPosition = GLES20.glGetAttribLocation(program, "vPosition");
vCoord = GLES20.glGetAttribLocation(program, "vCoord");
vTexture = GLES20.glGetUniformLocation(program, "vTexture");
vMatrix = GLES20.glGetUniformLocation(program, "vMatrix");
loadprogram()
public static int loadProgram(String vSource, String fSource) {
/**
* 顶点着色器
*/
int vShader = GLES20.glCreateShader(GLES20.GL_VERTEX_SHADER);
//加载着色器代码
GLES20.glShaderSource(vShader, vSource);
//编译(配置)
GLES20.glCompileShader(vShader);
//查看配置 是否成功
int[] status = new int[1];
GLES20.glGetShaderiv(vShader, GLES20.GL_COMPILE_STATUS, status, 0);
if (status[0] != GLES20.GL_TRUE) {
//失败
throw new IllegalStateException("load vertex shader:" + GLES20.glGetShaderInfoLog
(vShader));
}
/**
* 片元着色器
* 流程和上面一样
*/
int fShader = GLES20.glCreateShader(GLES20.GL_FRAGMENT_SHADER);
//加载着色器代码
GLES20.glShaderSource(fShader, fSource);
//编译(配置)
GLES20.glCompileShader(fShader);
//查看配置 是否成功
GLES20.glGetShaderiv(fShader, GLES20.GL_COMPILE_STATUS, status, 0);
if (status[0] != GLES20.GL_TRUE) {
//失败
throw new IllegalStateException("load fragment shader:" + GLES20.glGetShaderInfoLog
(vShader));
}
/**
* 创建着色器程序
*/
int program = GLES20.glCreateProgram();
//绑定顶点和片元
GLES20.glAttachShader(program, vShader);
GLES20.glAttachShader(program, fShader);
//链接着色器程序
GLES20.glLinkProgram(program);
//获得状态
GLES20.glGetProgramiv(program, GLES20.GL_LINK_STATUS, status, 0);
if (status[0] != GLES20.GL_TRUE) {
throw new IllegalStateException("link program:" + GLES20.glGetProgramInfoLog(program));
}
GLES20.glDeleteShader(vShader);
GLES20.glDeleteShader(fShader);
return program;
}
网友评论