How programmatically alpha fades a textured object in OpenGL ES 1.1 (iPhone)

I have been using OpenGL ES 1.1 on iPhone for 10 months, and at that time there was one seemingly simple task that I could not do: the textured object was disappearing programmatically. To keep it simple: how can alpha disappear under code control, a simple 2D triangle that has a texture (with alpha) applied to it. I would like to put it out to / from while it is above the stage, rather than a simple colored background. So far, the only technique I have to do is create a texture with several previously faded copies of the texture on it. (Yuck)

As an example, I cannot do this using the Apple GLSprite sample code as a starting point. It already textures a square with a texture that has its own alpha. I would like to remove this object.

+6
iphone alpha opengl textures fade
source share
2 answers

Perhaps I do not understand you, but for me it seems trivial, and I successfully do this in my applications. Way:

  • enable texturing and all you need
  • enable blending: glEnable(GL_BLEND)
  • select the blending mode glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA)
  • set the color to match with: glColor4f(r * a, g * a , b * a, a)
  • draw your geometry

The blending function is for porter-duff through using pre-multiplied colors / textures. GL_TEXTURE_ENV_MODE must be set to GL_MODULATE , but this is the default value.

+15
source share

Nikolai’s decision is correct; please ignore what I said on the Apple forums. Since the texture is pre-multiplied, the color must be too true. You should use GL_ONE , not GL_SRC_ALPHA , and do the following:

  glColor4f (1., 1., 1., myDesiredAlpha);
 glColor4f (myDesiredAlpha, myDesiredAlpha, myDesiredAlpha, myDesiredAlpha);
+2
source share

All Articles