openGL在Android中使用包括2中方法
1:使用Android接口
继承Android 中openGl提供的 GLSurfaceView.Renderer接口,复写三个方法。
@Override
public void onSurfaceCreated(GL10 gl, EGLConfig config) {
}
@Override
public void onSurfaceChanged(GL10 gl, int width, int height) {
}
@Override
public void onDrawFrame(GL10 gl) {
}
2:自行实现EGL环境
自己实现EGL环境,重写EGL环境,实现GLThread线程
本文主要介绍下实现Android 接口基本流程:
第一步 :配置openGL环境
主要是创建了openGL的Program,以及将定点坐标和片元坐标设置给Program id,并将此ID保存,方便后续操作openGL
//创建着色器
public static int loadShader(int shaderType, String source)
{
int shader = GLES20.glCreateShader(shaderType);
if(shader != 0)
{
GLES20.glShaderSource(shader, source);
GLES20.glCompileShader(shader);
int[] compile = new int[1];
GLES20.glGetShaderiv(shader, GLES20.GL_COMPILE_STATUS, compile, 0);
if(compile[0] != GLES20.GL_TRUE)
{
GLES20.glDeleteShader(shader);
shader = 0;
}
}
return shader;
}
public static int createProgram(String vertexSource, String fragmentSource)
{
//创建定点坐标
int vertexShader = loadShader(GLES20.GL_VERTEX_SHADER, vertexSource);
if(vertexShader == 0)
{
return 0;
}
//创建片元坐标
int fragmentShader = loadShader(GLES20.GL_FRAGMENT_SHADER, fragmentSource);
if(fragmentShader == 0)
{
return 0;
}
//创建CPU和GPU之间传值的 openGL程序的 代表值
int program = GLES20.glCreateProgram();
if(program != 0)
{
//定点和片元着色器和openGL对应绑定
GLES20.glAttachShader(program, vertexShader);
GLES20.glAttachShader(program, fragmentShader);
GLES20.glLinkProgram(program);
int[] linsStatus = new int[1];
GLES20.glGetProgramiv(program, GLES20.GL_LINK_STATUS, linsStatus, 0);
if(linsStatus[0] != GLES20.GL_TRUE)
{
GLES20.glDeleteProgram(program);
program = 0;
}
}
return program;
}
此操作完成之后就得到了一个openGL和CPU之间沟通的桥梁ID 也就是program,这个GPU上跑的程序的定点坐标为
int vertexShader = loadShader(GLES20.GL_VERTEX_SHADER, vertexSource);中传入的vertexSource,得到的定点桥梁为
vertexShader ,
片元的桥梁为: int fragmentShader = loadShader(GLES20.GL_FRAGMENT_SHADER, fragmentSource);片元着色器的桥梁ID为fragmentShader
第二步 :获取glsl中变量
//顶点着色器
attribute vec4 av_Position;
attribute vec2 af_Position;
varying vec2 v_texPosition;
void main() {
v_texPosition = af_Position;
//内置变量 gl_Position
gl_Position = av_Position;
}
//片元着色器
precision mediump float;
varying vec2 v_texPosition;
uniform sampler2D sampler_y;
uniform sampler2D sampler_u;
uniform sampler2D sampler_v;
void main() {
float y,u,v;
y = texture2D(sampler_y,v_texPosition).r;
u = texture2D(sampler_u,v_texPosition).r- 0.5;
v = texture2D(sampler_v,v_texPosition).r- 0.5;
vec3 rgb;
// yuv---> rgb 固定代码
rgb.r = y + 1.403 * v;
rgb.g = y - 0.344 * u - 0.714 * v;
rgb.b = y + 1.770 * u;
//内置变量 gl_FragColor
gl_FragColor = vec4(rgb,1);
}
//获取GLSL中变量
private void initRenderYUV()
{
//加载顶点着色器
String vertexSource = MNShaderUtil.readRawTxt(context, R.raw.vertex_shader);
//加载片元着色器
String fragmentSource = MNShaderUtil.readRawTxt(context, R.raw.fragment_shader);
//真正创建opengl 对应的gpu程序,获取操作的ID
program_yuv = MNShaderUtil.createProgram(vertexSource, fragmentSource);
//获取顶点着色器中的av_Position和af_Position变量
avPosition_yuv = GLES20.glGetAttribLocation(program_yuv, "av_Position");
afPosition_yuv = GLES20.glGetAttribLocation(program_yuv, "af_Position");
//获取片元着色器变量sampler_y sampler_u sampler_v
sampler_y = GLES20.glGetUniformLocation(program_yuv, "sampler_y");
sampler_u = GLES20.glGetUniformLocation(program_yuv, "sampler_u");
sampler_v = GLES20.glGetUniformLocation(program_yuv, "sampler_v");
textureId_yuv = new int[3];
//生成yuv的纹理坐标,生成3个纹理 对应 y u v 分别三个纹理
GLES20.glGenTextures(3, textureId_yuv, 0);
for(int i = 0; i < 3; i++)
{
//绑定纹理,配置纹理的参数
GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, textureId_yuv[i]);
GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_S, GLES20.GL_REPEAT);
GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_T, GLES20.GL_REPEAT);
GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MIN_FILTER, GLES20.GL_LINEAR);
GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MAG_FILTER, GLES20.GL_LINEAR);
}
}
第二步 :渲染出图形
渲染出视频图形的原理是 通过将 视频解码成YUV格式之后,将Y U V 分别作为三个纹理进行渲染到上面构造的三个纹理上,进行合成展示,视频即可显示
@Override
public void onDrawFrame(GL10 gl) {
GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT);
GLES20.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
renderYUV();
// 刷新
GLES20.glDrawArrays(GLES20.GL_TRIANGLE_STRIP, 0, 4);
}
private void renderYUV() {
if (width_yuv > 0 && height_yuv > 0 && y != null && u != null && v != null) {
GLES20.glUseProgram(program_yuv);
GLES20.glEnableVertexAttribArray(avPosition_yuv);
GLES20.glVertexAttribPointer(avPosition_yuv, 2, GLES20.GL_FLOAT, false, 8, vertexBuffer);
GLES20.glEnableVertexAttribArray(afPosition_yuv);
GLES20.glVertexAttribPointer(afPosition_yuv, 2, GLES20.GL_FLOAT, false, 8, textureBuffer);
GLES20.glActiveTexture(GLES20.GL_TEXTURE0);
GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, textureId_yuv[0]);
GLES20.glTexImage2D(GLES20.GL_TEXTURE_2D, 0, GLES20.GL_LUMINANCE,
width_yuv, height_yuv, 0, GLES20.GL_LUMINANCE, GLES20.GL_UNSIGNED_BYTE, y);
GLES20.glActiveTexture(GLES20.GL_TEXTURE1);
GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, textureId_yuv[1]);
GLES20.glTexImage2D(GLES20.GL_TEXTURE_2D, 0, GLES20.GL_LUMINANCE,
width_yuv / 2, height_yuv / 2, 0, GLES20.GL_LUMINANCE, GLES20.GL_UNSIGNED_BYTE, u);
GLES20.glActiveTexture(GLES20.GL_TEXTURE2);
GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, textureId_yuv[2]);
GLES20.glTexImage2D(GLES20.GL_TEXTURE_2D, 0, GLES20.GL_LUMINANCE,
width_yuv / 2, height_yuv / 2, 0, GLES20.GL_LUMINANCE, GLES20.GL_UNSIGNED_BYTE, v);
GLES20.glUniform1i(sampler_y, 0);
GLES20.glUniform1i(sampler_u, 1);
GLES20.glUniform1i(sampler_v, 2);
y.clear();
u.clear();
v.clear();
y = null;
u = null;
v = null;
}
}
网友评论