Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what do texture2D().r and texture2D().a mean?

I am using OpenGL ES in Android programming, when I transform YUV(NV21) to RGB in shader, like:

vec3 yuv = vec3(
        (texture2D(u_TextureY, vTextureCoord).r - 0.0625),
        texture2D(u_TextureUV, vTextureCoord).a - 0.5,
        texture2D(u_TextureUV, vTextureCoord).r - 0.5
    );

then I'll get YUV data that seperating from u_TextureY and u_TextureUV.

I know that NV21 format is like: YYYYYY...UVUV... BUT how can I transform YUYV422 to RGB? So, my problem is what do "r" and "a" mean in texture2D(u_TextureY, vTextureCoord).r and .a ? then I can find the way to do YUYV422->RGB.

like image 576
Shuyun6 Avatar asked Dec 06 '17 10:12

Shuyun6


People also ask

What is texture2D?

The texture2D function returns a texel, i.e. the (color) value of the texture for the given coordinates. The function has one input parameter of the type sampler2D and one input parameter of the type vec2 : sampler, the uniform the texture is bound to, and coord, the 2-dimensional coordinates of the texel to look up.

How does sampler2D work?

A sampler2D is used to do lookup in a standard texture image; a samplerCube is used to do lookup in a cubemap texture (Subsection 5.3. 4). The value of a sampler variable is a reference to a texture unit. The value tells which texture unit is invoked when the sampler variable is used to do texture lookup.


1 Answers

The return type of texture2D is vec4. In GLSL the components of the vector can be separately accessed:

See the The OpenGL Shading Language specification:

5.5 Vector and Scalar Components and Length

The names of the components of a vector or scalar are denoted by a single letter. As a notational convenience, several letters are associated with each component based on common usage of position, color or texture coordinate vectors. The individual components can be selected by following the variable name with period ( . ) and then the component name.

The component names supported are:

  • {x, y, z, w} Useful when accessing vectors that represent points or normals

  • {r, g, b, a} Useful when accessing vectors that represent colors

  • {s, t, p, q} Useful when accessing vectors that represent texture coordinates

The order of the components can be different to swizzle them, or replicated:

vec4 pos = vec4(1.0, 2.0, 3.0, 4.0);
vec4 swiz= pos.wzyx; // swiz = (4.0, 3.0, 2.0, 1.0)
vec4 dup = pos.xxyy; // dup = (1.0, 1.0, 2.0, 2.0)
float f = 1.2;
vec4 dup = f.xxxx; // dup = (1.2, 1.2, 1.2, 1.2)


This means, that .r gives the 1st component of the vec4 and .a gives the 4th component of the vec4.

like image 101
Rabbid76 Avatar answered Sep 29 '22 17:09

Rabbid76