OpenGL和OpenGL ES显示YUV图片的着色器差别(一)

这里解释的只是用于显示YUV图片的代码,没有增加任何效果:

OpenGL 的片段着色器片段:

复制代码
const char *fsrc = "varying vec2 textureOut; \
    uniform sampler2D tex_y; \
    uniform sampler2D tex_u; \
    uniform sampler2D tex_v; \
    void main(void) \
    { \
        vec3 yuv; \
        vec3 rgb; \
        yuv.x = texture2D(tex_y, textureOut).r; \
        yuv.y = texture2D(tex_u, textureOut).r - 0.5; \
        yuv.z = texture2D(tex_v, textureOut).r - 0.5; \
        rgb = mat3( 1,       1,         1, \
                    0,       -0.39465,  2.03211, \
                    1.13983, -0.58060,  0) * yuv; \
        gl_FragColor = vec4(rgb, 1); \
    }";

OpenGL 的顶点着色器片段:

复制代码
const char *vsrc = "attribute vec4 vertexIn; \
    attribute vec2 textureIn; \
    varying vec2 textureOut;  \
    void main(void)           \
    {                         \
        gl_Position = vertexIn; \
        textureOut = textureIn; \
    }";

OpenGL ES 的片段着色器片段:

复制代码
const char *fsrc = "#version 320 es\n\
    precision mediump float;\n\
    in vec2 textureOut;\n\
    uniform sampler2D tex_y;\n\
    uniform sampler2D tex_u;\n\
    uniform sampler2D tex_v;\n\
    out vec4 fragColor;\n\
    void main(void)\n\
    {\n\
        vec3 yuv;\n\
        vec3 rgb;\n\
        yuv.x = texture(tex_y, textureOut).r;\n\
        yuv.y = texture(tex_u, textureOut).r - 0.5;\n\
        yuv.z = texture(tex_v, textureOut).r - 0.5;\n\
        rgb = mat3(1.0, 1.0, 1.0,\n\
                   0.0, -0.39465, 2.03211,\n\
                   1.13983, -0.58060, 0.0) * yuv;\n\
        fragColor = vec4(rgb, 1.0);\n\
    }";

OpenGL ES的顶点着色器片段:

复制代码
const char *vsrc = "#version 320 es\n\
    in vec4 vertexIn;\n\
    in vec2 textureIn;\n\
    out vec2 textureOut;\n\
    void main(void)\n\
    {\n\
        gl_Position = vertexIn;\n\
        textureOut = textureIn;\n\
    }";

差别:

  • 添加了 #version 320 es 指令,指定使用 OpenGL ES 3.2 版本。
  • 使用 inout 关键字替代了原来的 varying
  • 使用 precision mediump float 指定片段着色器的浮点数精度。
  • 使用 texture 函数替代了原来的 texture2D 函数。
相关推荐
UTwelve1 天前
【UE】材质与半透明 - 01.将半透明作为后期材质
ue5·材质·着色器
二狗哈11 天前
Cesium快速入门29:CMZL数据格式加载
3d·状态模式·webgl·cesium·着色器·地图可视化
二狗哈13 天前
Cesium快速入门27:GeoJson自定义样式
前端·cesium·着色器
二狗哈15 天前
Cesium快速入门24:Appearance编写着色器修改外观
3d·webgl·cesium·着色器·地图可视化
二狗哈16 天前
Cesium快速入门22:fabric自定义着色器
运维·开发语言·前端·webgl·fabric·cesium·着色器
米芝鱼20 天前
UnityURP3D管线自定义功能shader
游戏·unity·shader·urp·着色器
联系QQ 192263820 天前
PEM电解槽Simulink模型,得出I-V曲线图,通过调参可以分析各参数对电解电压的影响。 ...
着色器
Echo_NGC223724 天前
【AirSim 教程指南】Part 3:相机与传感器(RGB / 深度 / 分割 / LiDAR)
人工智能·计算机视觉·游戏引擎·ar·无人机·图形渲染·着色器
gis分享者1 个月前
学习threejs,使用自定义GLSL 着色器,实现抽象艺术特效
threejs·着色器·glsl·shadermaterial·抽象艺术
熊猫悟道1 个月前
Unity shader 之,Shader内部时间离散处理
unity·游戏引擎·材质·着色器