博客
关于我
[FFmpeg + OpenGL + OpenSL ES]OpenGL ES 渲染获取到的yuv数据 - 7
阅读量:273 次
发布时间:2019-03-01

本文共 2444 字,大约阅读时间需要 8 分钟。

在OpenGL中渲染YUV数据时,通常会使用3个纹理分别获取Y、U和V的值,然后通过公式转换为RGB颜色。这种方法让转换过程在GPU完成,效率远高于CPU。

渲染流程

YUV数据的转换涉及三个步骤:获取纹理数据、执行转换公式以及输出结果。每个步骤都需要精确的计算和优化。

获取纹理数据

在OpenGL中,使用顶点着色器和片元着色器来处理纹理数据。顶点着色器负责顶点坐标的处理,片元着色器则负责纹理的采样和转换。代码如下:

attribute vec4 av_Position;attribute vec2 af_Position;varying vec2 v_texPosition;void main() {    v_texPosition = af_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 = texture2D(sampler_y, v_texPosition).r;    float u = texture2D(sampler_u, v_texPosition).r - 0.5;    float v = texture2D(sampler_v, v_texPosition).r - 0.5;    vec3 rgb = vec3(        y + 1.403 * v,        y - 0.344 * u - 0.714 * v,        y + 1.770 * u    );    gl_FragColor = vec4(rgb, 1.0);}

Java工程配置

在Android项目中,需要配置OpenGL渲染环境。创建opengl包,添加必要的GL类:

public class WlRender implements GLSurfaceView.Renderer {    private Context mContext;    private int program_yuv;    private int avPosition_yuv;    private int afPosition_yuv;    private int[] textureId_yuv;    private int width_yuv;    private int height_yuv;    private ByteBuffer y, u, v;    public WlRender(Context context) {        mContext = context;        // 初始化顶点和纹理坐标        // ...    }    @Override    public void onSurfaceCreated(GL10 gl10, EGLConfig eglConfig) {        initRanderYUV();    }    @Override    public void onSurfaceChanged(GL10 gl10, int width, int height) {        GLES20.glViewport(0, 0, width, height);    }    @Override    public void onDrawFrame(GL10 gl10) {        GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT);        renderYUV();        GLES20.glDrawArrays(GLES20.GL_TRIANGLE_STRIP, 0, 4);    }    private void initRanderYUV() {        // 加载着色器程序        // ...        // 创建并绑纹理        GLES20.glGenTextures(3, textureId_yuv);        // 设置纹理参数        // ...    }    public void setYUVRenderData(int width, int height, byte[] y, byte[] u, byte[] v) {        this.width_yuv = width;        this.height_yuv = height;        this.y = ByteBuffer.wrap(y);        this.u = ByteBuffer.wrap(u);        this.v = ByteBuffer.wrap(v);    }    private void renderYUV() {        if (width_yuv > 0 && height_yuv > 0 && y != null && u != null && v != null) {            GLES20.glUseProgram(program_yuv);            // 启用顶点和纹理位置            // ...            // 绘制纹理            // ...        }    }}

注意事项

目前绘制存在问题,但程序能正常运行。后续需要解决这些问题,确保图像正确显示。

转载地址:http://udzo.baihongyu.com/

你可能感兴趣的文章
Python 学习笔记(一)Data type
查看>>
python 安装echarts
查看>>
python 安装opencv及问题解决
查看>>
Python 安装程序:“写入文件 C:\Python27\pythonw.exe 时出错“
查看>>
Python 安装避坑指南:从入门到进阶
查看>>
Python 安装配置详解
查看>>
python 实现儿童算术游戏
查看>>
python 实现字符串转整型
查看>>
Python 实现定时任务的八种方案!
查看>>
Python 实现自动化测试 dubbo 协议接口
查看>>
python 对json数据读取及保存与读取,对dump,dumps,load,loads的理解
查看>>
Python 对mysql数据库的操作
查看>>
Python 对象数据库列表
查看>>
Python 导入requests报错No module named requests
查看>>
python 将函数加到列表中进行调用
查看>>
python 将图片与字符串相互转换
查看>>
Python 嵌套字典全面指南
查看>>
Python 工具v简介
查看>>
python 常见内置函数setattr、getattr、delattr、setitem、getitem、delitem
查看>>
Python 常见的错误类型和继承关系
查看>>