Opengl Shader, what are the alpha components of gl_FragColor?

I think this will be a little simple answer.
But I can not find the answer using googling.
This is an OpenGLES shader thing. I am using the cocos2d-x engine.

This is my fragment shader code.

precision lowp float; varying vec2 v_texCoord; uniform sampler2D u_texture; uniform vec4 u_lightPosition; void main() { vec4 col=texture2D(u_texture,v_texCoord); mediump float lightDistance = distance(gl_FragCoord, u_lightPosition); mediump float alpha = 100.0/lightDistance; alpha = min(alpha, 1.0); alpha = max(alpha, 0.0); col.w = alpha; //col.a = alpha; gl_FragColor=col; } 

I just want to give opacity in some area of ​​the circle. So I change the color value of w because I thought it was the texel alpha value. But the result was very strange. I am afraid that this is not an alpha value.

Even if I set the value to 1.0 for testing, the whole sprite will change to bright and white.


Its vertex shader is very normal, there is nothing special.


Any idea please.



Updated: For reference, I am attaching several images of the results.

case 1)

 col.w = alpha; 

enter image description here

case 2)

 col.w = 1.0 

enter image description here


and normal texture before applying the shader.)

enter image description here

+6
source share
1 answer

The GLa ES 2.0 Reference Card defines:

Variable
mediump vec4 gl_FragColor;

Description
fragment color

Units or coordinate system
RGBA color

The following is said:

Vector components
In addition to the syntax of the numeric subscript number of the array, the names of the vector components are denoted by a single letter. Components can be swizzled and replicated e.g.: pos.xx, pos.zy

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

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

{s, t, p, q} Use when accessing vectors representing texture coordinates

So, of course, using .a will be more idiomatic, but explicitly what you save in .w is the alpha output for gl_FragColor .

To answer the question that you asked as a heading, and not a question in the body, the value returned by texture2D will be correct for this texture. Either the actual stored value if the texture is GL_RGBA or GL_LUMINANCE_ALPHA , or 1.0.

So, you are outputting alpha correctly.

If your alpha version does not have the blending effect that you expect, then you should have glBlendFunc set to something unexpected, possibly involving GL_CONSTANT_COLOR .

+6
source

Source: https://habr.com/ru/post/971576/


All Articles