glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, id_y);
glTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE, width, height, 0, GL_LUMINANCE, GL_UNSIGNED_BYTE, data);
glUniform1i(gvImageTextureY, 0);
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, id_uv);
glTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE_ALPHA, width/2, height/2, 0, GL_LUMINANCE_ALPHA, GL_UNSIGNED_BYTE, data + width*height);
glUniform1i(gvImageTextureUV, 1);
与直接使用RBGA数据不同,这里的参数采用的是GL_LUMINANCE,与GL_LUMINANCE_ALPHA,与GL_RGBA不同,GL_RGBA单独保存R、G、B、A四个数据,而GL_LUMINANCE将这四个数据合并成一个,因为这样1个Y就可以与1个RGBA对应。GL_LUMINANCE_ALPHA代表首先是亮度,然后是alpha值,这样我们就能将U值与V值分别取出。
precision mediump float;
uniform sampler2D mGLUniformTexture;
uniform sampler2D mGLUniformTexture1;
varying highp vec2 textureCoordinate;
const mat3 yuv2rgb = mat3(
1, 0, 1.2802,
1, -0.214821, -0.380589,
1, 2.127982, 0
);
void main() {
vec3 yuv = vec3(
1.1643 * (texture2D(mGLUniformTexture, textureCoordinate).r - 0.0625),
texture2D(mGLUniformTexture1, textureCoordinate).a - 0.5,
texture2D(mGLUniformTexture1, textureCoordinate).r - 0.5
);\
vec3 rgb = yuv * yuv2rgb;
gl_FragColor = vec4(rgb, 1);
}
下一章探讨多重纹理实现简单滤镜
常量 | 描述 |
GL_APHPA | 按照ALPHA值存储纹理单元 |
GL_LUMINANCE | 按照亮度值存储纹理单元 |
GL_LUMINANCE_ALPHA | 按照亮度和alpha值存储纹理单元 |
GL_RGB | 按照RGB成分存储纹理单元 |
GL_RGBA | 按照RGBA成分存储纹理单元 |
The difference between the three single-component texture formats, GL_ALPHA, GL_LUMINANCE and GL_INTENSITY, is in the way the four-component RGBA color vector is generated. If the value for a given texel is X, then the RGBA color vector generated is:
* GL_ALPHA: RGBA = (0, 0, 0, X)
* GL_LUMINANCE: RGBA = (X, X, X, 1)
* GL_INTENSITY: RGBA = (X, X, X, X)
In other words, if we interpret the alpha as transparency, GL_ALPHA would represent a completely black texture with varying transparency, GL_LUMINANCE is an opaque texture with varying color (a grayscale image), and GL_INTENSITY is a combination where both
the color and alpha channel is varying.