views:

750

answers:

4

I've been using OpenGL ES 1.1 on the iPhone for 10 months, and in that time there is one seemingly simple task I have been unable to do: programmatically fade a textured object. To keep it simple: how can I alpha fade, under code control, a simple 2D triangle that has a texture (with alpha) applied to it. I would like to fade it in/out while it is over a scene, not a simple colored background. So far the only technique I have to do this is to create a texture with multiple pre-faded copies of the texture on it. (Yuck)

As an example, I am unable to do this using Apple's GLSprite sample code as a starting point. It already textures a quad with a texture that has its own alpha. I would like to fade that object in and out.

+6  A: 

Maybe I'm not getting you right, but to me it seems trivial and I've been doing it my apps successfully. The way to go is:

  1. enable texturing and everything you need
  2. enable blending: glEnable(GL_BLEND)
  3. select a blend mode glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA)
  4. set a color to blend with: glColor4f(r * a, g * a , b * a, a)
  5. draw your geometry

The blend function is for porter-duff over using premultiplied colors/textures. The GL_TEXTURE_ENV_MODE must be set to GL_MODULATE, but that's the default.

Nikolai Ruhe
A: 

Thanks to some help on the Apple dev forums, I got this working using a different variation thank Nikolai, also with GL_MODULATE:

glColor4f(1., 1., 1., myDesiredAlpha);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

When I tried using "GL_ONE" instead of "GL_SRC_ALPHA" it would not fade for me.

PewterSoft
That's why I explicitly specified *premultiplied* colors. That's the normal way colors are handled in Quartz bitmap contexts and therefore in textures loaded into GL. If your colors are nonpremultiplied, the blend func you specified works as expected (except for target alpha, but that's usually irrelevant).
Nikolai Ruhe
+1  A: 

Nikolai's solution is correct; please ignore what I said on the Apple forums. Since the texture is pre-multiplied, the per-vertex color should be too. You should use GL_ONE rather than GL_SRC_ALPHA, and do this:

glColor4f(1., 1., 1., myDesiredAlpha);
glColor4f(myDesiredAlpha, myDesiredAlpha, myDesiredAlpha, myDesiredAlpha);
prideout
A: 

Hello,

Could you tell me how to do this using a vertex array? I'm sure I'm missing something simple, but I want to change the alpha on some vertices within an array on the fly and can't come up with a solution.

So for example, if I had a vertex array containing data for 10 textured quads and I wanted to change the alpha of one of those quads during runtime, how would I do it?

At the moment I can only change the alpha on the whole vertex array.

Thanks for your help.

triggerfish