OpenGL|Android OpenGL ES基本用法(11),简单滤镜操作
目录 【OpenGL|Android OpenGL ES基本用法(11),简单滤镜操作】加入简单的滤镜操作
文章图片
fragment_shader1.glsl
precision mediump float;
varying vec2 ft_Position;
uniform sampler2D sTexture;
void main() {
gl_FragColor = vec4(vec3(1.0 - texture2D(sTexture, ft_Position)), 1.0);
}
fragment_shader2.glsl
precision mediump float;
varying vec2 ft_Position;
uniform sampler2D sTexture;
void main() {
lowp vec4 textureColor = texture2D(sTexture, ft_Position);
float gray = textureColor.r * 0.2125 + textureColor.g * 0.7154 + textureColor.b * 0.0721;
gl_FragColor = vec4(gray, gray, gray, textureColor.w);
}
fragment_shader3.glsl
precision mediump float;
varying vec2 ft_Position;
uniform sampler2D sTexture;
void main() {
lowp vec4 textureColor = texture2D(sTexture, ft_Position);
gl_FragColor = vec4((textureColor.rgb + vec3(-0.5f)), textureColor.w);
}
读取shader的时候,读取各自对应的
package com.example.opengl.eglpic;
import android.content.Context;
import android.opengl.GLES20;
import com.example.opengl.R;
import com.example.opengl.egl.MyGlSurfaceView;
import com.example.opengl.egl.ShaderUtil;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;
public class PicRenderMulti implements MyGlSurfaceView.EGLRender {private Context context;
private float[] vertexData = https://www.it610.com/article/{
-1f, -1f,
1f, -1f,
-1f, 1f,
1f, 1f
};
private FloatBuffer vertexBuffer;
private float[] fragmentData = {
0f, 1f,
1f, 1f,
0f, 0f,
1f, 0f
};
private FloatBuffer fragmentBuffer;
private int program;
private int vPosition;
private int fPosition;
private int sampler;
private int vboId;
private int textureId;
private int curIndex;
public void setTextureId(int textureId, int curIndex) {
this.textureId = textureId;
this.curIndex = curIndex;
}public PicRenderMulti(Context context) {
this.context = context;
vertexBuffer = ByteBuffer.allocateDirect(vertexData.length * 4)
.order(ByteOrder.nativeOrder())
.asFloatBuffer()
.put(vertexData);
vertexBuffer.position(0);
fragmentBuffer = ByteBuffer.allocateDirect(fragmentData.length * 4)
.order(ByteOrder.nativeOrder())
.asFloatBuffer()
.put(fragmentData);
fragmentBuffer.position(0);
}@Override
public void onSurfaceCreated() {
String vertexSource = ShaderUtil.readRawTxt(context, R.raw.vertex_shader);
String fragmentSource;
switch (curIndex) {
case 0:
fragmentSource = ShaderUtil.readRawTxt(context, R.raw.fragment_shader1);
break;
case 1:
fragmentSource = ShaderUtil.readRawTxt(context, R.raw.fragment_shader2);
break;
case 2:
fragmentSource = ShaderUtil.readRawTxt(context, R.raw.fragment_shader3);
break;
default:
fragmentSource = ShaderUtil.readRawTxt(context, R.raw.fragment_shader);
}
program = ShaderUtil.createProgram(vertexSource, fragmentSource);
vPosition = GLES20.glGetAttribLocation(program,"v_Position");
fPosition = GLES20.glGetAttribLocation(program, "f_Position");
sampler = GLES20.glGetUniformLocation(program, "sTexture");
int[] vbos = new int[1];
GLES20.glGenBuffers(1, vbos, 0);
vboId = vbos[0];
GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, vboId);
GLES20.glBufferData(GLES20.GL_ARRAY_BUFFER, vertexData.length * 4 + fragmentData.length * 4, null, GLES20.GL_STATIC_DRAW);
GLES20.glBufferSubData(GLES20.GL_ARRAY_BUFFER, 0, vertexData.length * 4, vertexBuffer);
GLES20.glBufferSubData(GLES20.GL_ARRAY_BUFFER, vertexData.length * 4, fragmentData.length * 4, fragmentBuffer);
GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, 0);
}@Override
public void onSurfaceChanged(int width, int height) {
GLES20.glViewport(0, 0, width, height);
}@Override
public void onDrawFrame() {
GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT);
GLES20.glClearColor(1f, 0f, 0f, 1f);
GLES20.glUseProgram(program);
GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, textureId);
GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, vboId);
GLES20.glEnableVertexAttribArray(vPosition);
GLES20.glVertexAttribPointer(vPosition, 2, GLES20.GL_FLOAT, false, 8,
0);
GLES20.glEnableVertexAttribArray(fPosition);
GLES20.glVertexAttribPointer(fPosition, 2, GLES20.GL_FLOAT, false, 8,
vertexData.length * 4);
GLES20.glDrawArrays(GLES20.GL_TRIANGLE_STRIP, 0, 4);
GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, 0);
GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, 0);
}
}
推荐阅读
- android第三方框架(五)ButterKnife
- 做一件事情的基本原理是什么()
- Android中的AES加密-下
- 带有Hilt的Android上的依赖注入
- dubbo基本认识
- android|android studio中ndk的使用
- Android事件传递源码分析
- RxJava|RxJava 在Android项目中的使用(一)
- Android7.0|Android7.0 第三方应用无法访问私有库
- 深入理解|深入理解 Android 9.0 Crash 机制(二)