Reputation: 41
I'm creating an iOS app that uses OpenGL ES 2.0. I'm somewhat new to OpenGL, so this may be a trivial mistake.
I've created a simple shader to handle masking one texture with another, using the alpha channel. The mask texture initialized like so:
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);
I then (later) draw some stuff to the mask texture, and link the mask texture to 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 renders exactly how I want it to. However, to reduce the overhead in binding the mask texture's buffer (which seems substantial) I'm trying to use an 8-bit alpha-only pixel format for the mask texture. BUT, if I change the code from the RGBA setup:
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 1024, 768, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0);
...to alpha-only:
glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, 1024, 768, 0, GL_ALPHA, GL_UNSIGNED_BYTE, buffer);
...the fragment shader fails to draw anything. Since I'm only using the alpha from the mask texture in the first place, it seems like it should continue to work. Any ideas why it doesn't?
Upvotes: 4
Views: 1064
Reputation: 56347
The OpenGL ES 2.0 spec (or the GL_OES_framebuffer_object extension) doesn't define any renderable formats with only alpha bits, so they are NOT supported. You will have to use a RGBA format.
Upvotes: 2