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 函数。
相关推荐
ct9785 天前
Cesium高级特效与着色器开发全指南
前端·gis·cesium·着色器
vQAvXEsg10 天前
三菱FX5U四轴控制系统实战手记
着色器
梵尔纳多23 天前
OpenGL着色器语言(GLSL)
c++·opengl·着色器
Mars-xq23 天前
godot 毛玻璃效果着色器shader
游戏引擎·godot·着色器
UTwelve1 个月前
【UE】材质与半透明 - 01.将半透明作为后期材质
ue5·材质·着色器
二狗哈1 个月前
Cesium快速入门29:CMZL数据格式加载
3d·状态模式·webgl·cesium·着色器·地图可视化
二狗哈2 个月前
Cesium快速入门27:GeoJson自定义样式
前端·cesium·着色器
二狗哈2 个月前
Cesium快速入门24:Appearance编写着色器修改外观
3d·webgl·cesium·着色器·地图可视化
二狗哈2 个月前
Cesium快速入门22:fabric自定义着色器
运维·开发语言·前端·webgl·fabric·cesium·着色器
米芝鱼2 个月前
UnityURP3D管线自定义功能shader
游戏·unity·shader·urp·着色器