Why doesn't my fragment shader read alpha information when I switch to alpha-only (A8) pixelFormat?

I am creating an iOS application using OpenGL ES 2.0. I am somewhat new to OpenGL, so this might be a trivial mistake.

I created a simple shader to mask one texture with another using an alpha channel. The texture of the mask is initialized as follows:

glGenTextures (1, & name); glBindTexture (GL_TEXTURE_2D, name);

glTexParameterf (GL_TEXTURE_2D, GL_GENERATE_MIPMAP, GL_TRUE); glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);

glTexImage2D (GL_TEXTURE_2D, 0, GL_RGBA, 1024, 768, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0);

glGenFramebuffersOES (1, & buffer); glBindFramebufferOES (GL_FRAMEBUFFER_OES, buffer);

glFramebufferTexture2DOES (GL_FRAMEBUFFER_OES, GL_COLOR_ATTACHMENT0_OES, GL_TEXTURE_2D, name, 0);

glBindFramebufferOES (GL_FRAMEBUFFER_OES, 0);

Then (then) draw part of the mask texture and associate the mask texture with this fragment shader to apply it to another texture:

uniform sampler2D baseTextureSampler; uniform sampler2D maskTextureSampler; varying lowp vec4 colorVarying; varying mediump vec2 textureCoordsVarying; varying mediump vec2 positionVarying; void main() { lowp vec4 texel = texture2D(baseTextureSampler, textureCoordsVarying); lowp vec4 maskTexel = texture2D(maskTextureSampler, positionVarying); gl_FragColor = texel*colorVarying*maskTexel.a; } 

This does exactly what I want. However, in order to reduce the overhead of linking the mask texture buffer (which seems significant), I am trying to use the 8-bit alpha pixel format only for the mask texture. BUT, if I changed the code from the RGBA setting:

glTexImage2D (GL_TEXTURE_2D, 0, GL_RGBA, 1024, 768, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0);

... only for the alpha range:

glTexImage2D (GL_TEXTURE_2D, 0, GL_ALPHA, 1024, 768, 0, GL_ALPHA, GL_UNSIGNED_BYTE, buffer);

... the fragment shader did not draw anything. Since I primarily use alpha from the texture of the mask, it looks like it should continue to work. Any ideas why this is not the case?

+4
source share
1 answer

The OpenGL ES 2.0 specification (or the GL_OES_framebuffer_object extension) does not define any display formats with alpha bits only, so they are NOT supported. You will need to use the RGBA format.

+2
source

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


All Articles