Search code examples
opengl-es-2.0glkit

Changing the alpha/opacity channel on a texture using GLKit


I am new to opengl es, and I can't seem to figure out how you would change the alpha / opacity on a texture loaded with GLKTextureLoader.

Right now I just draw the texture with the following code.

self.texture.effect.texture2d0.enabled = YES;
self.texture.effect.texture2d0.name = self.texture.textureInfo.name;
self.texture.effect.transform.modelviewMatrix = [self modelMatrix];
[self.texture.effect prepareToDraw];
glEnableVertexAttribArray(GLKVertexAttribPosition);
glEnableVertexAttribArray(GLKVertexAttribTexCoord0);

NKTexturedQuad _quad = self.texture.quad;
long offset = (long)&_quad;
glVertexAttribPointer(GLKVertexAttribPosition,
                      2,
                      GL_FLOAT,
                      GL_FALSE,
                      sizeof(NKTexturedVertex),
                      (void *)(offset + offsetof(NKTexturedVertex, geometryVertex)));

glVertexAttribPointer(GLKVertexAttribTexCoord0,
                      2,
                      GL_FLOAT, GL_FALSE,
                      sizeof(NKTexturedVertex),
                      (void *)(offset + offsetof(NKTexturedVertex, textureVertex)));

glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);

Any advice would be very helpful :)


Solution

  • i am no gl expert but to draw with a changed alpha value does not seem to work as described by rickster.

    as far as i understand, the values passed to glBlendColor are only used when using glBlendFunc constants like: GL_CONSTANT_…

    this will overwrite the textures alpha values and use a defined value to draw with:

    glEnable(GL_BLEND);
    glBlendFunc(GL_CONSTANT_ALPHA, GL_ONE_MINUS_CONSTANT_ALPHA);
    glBlendColor(1.0, 1.0, 1.0, yourAlphaValue);
    glDraw... // the draw operations
    

    further reference can be found here http://www.opengl.org/wiki/Blending#Blend_Color