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 版本。
  • 使用 in 和 out 关键字替代了原来的 varying
  • 使用 precision mediump float 指定片段着色器的浮点数精度。
  • 使用 texture 函数替代了原来的 texture2D 函数。

你可能感兴趣的:(着色器)