Opengl Shader, what's the gl_FragColor's alpha components?
Asked Answered
I

1

10

I think it'll be a little bit simple answer.
But I can't find the answer with googling.
It's OpenGLES shader thing. I am using 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 circle area. So I change the color's w value because I thought it's the alpha value of the texel. But the result was very odd. I am afraid it's not alpha value.

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


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


Any idea please.



Updated: For reference, I attach some result image.

case 1)

col.w = alpha;

enter image description here

case 2)

col.w = 1.0

enter image description here


and normal texture before applying shader.)

enter image description here

Interaction answered 1/7, 2014 at 2:15 Comment(7)
Sounds like you're asking, What is the meaning of the w component of the vec4 assigned to gl_FragColor.Collywobbles
Resultingly, you're right, gl_FragColor's w. :)Interaction
As far as I know, you're doing it right. You may need to show us (a) what the input texture looks like, and (b) what your current output looks like.Collywobbles
I put the result sample. :)Interaction
What's your blend mode?Nearby
As Tommy said, you should add your current blend mode to the question. On a side note, you can replace your min/max lines with clamp like so: clamp(alpha, 0.0, 1.0).Tumefy
Thanks Tommy. I checked the Blend mode again. it's GL_ONE, GL_ONE_MINUS_SRC_ALPHA. I think it's reason to be brighten.Interaction
N
10

The GL ES 2.0 reference card defines:

Variable
mediump vec4 gl_FragColor;

Description
fragment color

Units or coordinate system
RGBA color

It further states:

Vector Components
In addition to array numeric subscript syntax, names of 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 that represent texture coordinates

So, sure, using .a would be more idiomatic but it's explicitly the case that what you store to .w is the output alpha for gl_FragColor.

To answer the question you've set as a title rather than the question in the body, the value returned by texture2D will be whatever is correct for that texture. Either an actual stored value if the texture is GL_RGBA or GL_LUMINANCE_ALPHA or else 1.0.

So you're outputting alpha correctly.

If your output alpha isn't having the mixing effect that you expect then you must have glBlendFunc set to something unexpected, possibly involving GL_CONSTANT_COLOR.

Nearby answered 1/7, 2014 at 3:53 Comment(1)
Thanks. Now it works well. vec4 col=texture2D(u_texture,v_texCoord); mediump float lightDistance = distance(gl_FragCoord, u_lightPosition); mediump float alpha = 100.0/lightDistance; alpha = clamp(alpha, 0.0, 1.0); gl_FragColor.a = 1.0 - alpha;Interaction

© 2022 - 2024 — McMap. All rights reserved.