2
votes

So my application uses mostly rgba textures for rendering, but in a few cases I use alpha masks for things like rendering text.

I'd like the output of my fragment shader to be the value of the texture sampler at the given coordinate multiplied by a color uniform I pass in. The problem is that I can't find a simple solution that handles both cases.

So for rgba textures this gives the desired result:

gl_FragColor = texture2D(diffuseTexture, varTexcoord.st, 0.0) * color;

But for alpha textures, the output is always black. I'm assuming the texture sampler treats every pixel of an alpha texture as: rgba(0, 0, 0, alpha), and I want it to be treated as rgba(1, 1, 1, alpha). I can get the desired result on alpha textures with this:

gl_FragColor = texture2D(diffuseTexture, varTexcoord.st, 0.0).w * color;

But obviously this breaks the rgba textures.

I'm fairly new to GLSL, and I'm wondering if there's a good way to handle both cases, or if I'm going to have to have 2 different shaders, or else convert all my aplha textures to rgba.

edit:

I think I'm going to end up re-packing my alpha textures as GL_LUMINANCE_ALPHA textures when loading assets.

1
Your assumption is correct, GL_ALPHA maps to RGBA as { R=0, G=0, B=0, A=A }Richard Taylor
Are you using desktop OpenGL or OpenGL ES?Nicol Bolas
.w is semantically incorrect.Jessy

1 Answers

1
votes

You have two different ways to process data. So you have two different shaders. Switch them. Shaders reflect what is to happen.

If you use the old, depreceated fixed function pipeline for each setting of glTexEnv, lighting, etc. a specific shader is created in situ.

Switching shaders is as simple as calling glUseProgramm