0

I'm new to OpenGL so I'm not sure how to do this. Currently I'm doing this to create an Alpha Texture in iOS :

GLuint framebuffer, renderBufferT;
glGenFramebuffers(1, &framebuffer);
glGenTextures(1, &renderBufferT);
glBindFramebuffer(GL_FRAMEBUFFER, framebuffer);
glBindTexture(GL_TEXTURE_2D, renderBufferT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, width, height, 0, GL_ALPHA, GL_UNSIGNED_BYTE, NULL);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, renderBufferT, 0);

GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
if(status != GL_FRAMEBUFFER_COMPLETE)
{
    NSLog(@"createAlphaBufferWithSize not complete %x", status);
    return NO;
}

But it returns an error : GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT

And I also wonder how to write to this texture in the fragment shader. Is it simply the same with RGBA, like this :

gl_FragColor = vec1(0.5);

My intention is to use an efficient texture, because there are so much texture reading in my code, while I only need one component of color.

Thanks for any direction where I might go with this.

Coolant
  • 448
  • 6
  • 13

2 Answers2

1

I'm not an iOS guy but that error indicates your OpenGL ES driver (PowerVR) does not support rendering to the GL_ALPHA format. I have not seen any OpenGL ES drivers that will do that on any platform. You can create GL_ALPHA textures to use with OpenGL ES using the texture compression tool in the PowerVR SDK, but I think the smallest format you can render to will be 16 bit color - if that is even available.

A better way to make textures efficient is to use compressed formats because the GPU decodes them with dedicated hardware. You really need the PowerVR Texture compression tool. It is a free download:

http://www.imgtec.com/powervr/insider/sdkdownloads/sdk_licence_agreement.asp

ClayMontgomery
  • 2,786
  • 1
  • 15
  • 14
0

So I still haven't found the answers for my questions above, but I found a workaround. In essence, since each pixel comprises of 4 color components, and I only need one for alpha only, then what I do is I use one texture to store 4 different logical alpha textures that I need. It takes a little effort to maintain these logical alpha textures.

And to draw to this one texture that contains 4 logical alpha textures, I use a shader and create a sign bit that marks which color component I intend to write to. The blend I used is

 (GL_ONE, GL_ONE_MINUS_SRC_COLOR), 

And the fragment shader is like this :

 uniform lowp vec4 uSignBit;
 void main()
 {
     lowp vec4 textureColor = texture2D(uTexture,vTexCoord);
     gl_FragColor = uSignBit * textureColor.a;
 }

So, when I intend to write an alpha value of some texture to a logical alpha texture number 2, I write :

 glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_COLOR);
 glUniform4f(uSignBit, 0.0, 1.0, 0.0, 0.0);
Coolant
  • 448
  • 6
  • 13