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 函数。
相关推荐
指掀涛澜天下惊2 天前
DirectX12(D3D12)基础教程六 计算着色器通用计算
着色器·计算着色器·gpu计算
Forest_10109 天前
GLSL(OpenGL 着色器语言)基础语法
着色器
滴水成川17 天前
Metal 着色器与渲染管线
着色器·metal
stevenzqzq17 天前
openGl片段着色器的含义
opengl·着色器
Allen747417 天前
往期项目shader着色器实践效果应用合集
着色器·ta
不收藏找不到我25 天前
10、基于osg引擎生成热力图高度图实现3D热力图可视化、3D热力图实时更新(带过渡效果)
3d·数据可视化·着色器
不收藏找不到我1 个月前
7、基于osg引擎实现读取vtk数据通过着色器实现简单体渲染(1)
着色器
MessiGo1 个月前
OpenGL(4)着色器
apache·着色器
太妃糖耶1 个月前
Shader中着色器的编译目标级别
unity·shader·着色器
HELLOMILI1 个月前
[Unity3D] 动态立方体贴图系统
游戏·unity·游戏引擎·图形渲染·着色器