简体   繁体   English

如何在OpenGL ES 1.1(iPhone)中以编程方式淡化纹理对象

[英]How to programmatically alpha fade a textured object in OpenGL ES 1.1 (iPhone)

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. 我已经在iPhone上使用OpenGL ES 1.1已经10个月了,在那个时候有一个看似简单的任务我无法做到:以编程方式淡化纹理对象。 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. 例如,我无法使用Apple的GLSprite示例代码作为起点。 It already textures a quad with a texture that has its own alpha. 它已经使用具有自己的alpha纹理纹理四边形。 I would like to fade that object in and out. 我想淡入淡出那个物体。

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) 启用混合: glEnable(GL_BLEND)
  3. select a blend mode glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA) 选择混合模式glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA)
  4. set a color to blend with: glColor4f(r * a, g * a , b * a, a) 设置要混合的颜色: glColor4f(r * a, g * a , b * a, a)
  5. draw your geometry 绘制几何图形

The blend function is for porter-duff over using premultiplied colors/textures. 混合功能适用使用预乘颜色/纹理的porter-duff The GL_TEXTURE_ENV_MODE must be set to GL_MODULATE , but that's the default. GL_TEXTURE_ENV_MODE必须设置为GL_MODULATE ,但这是默认值。

Nikolai's solution is correct; 尼古拉的解决方案是正确的; please ignore what I said on the Apple forums. 请忽略我在Apple论坛上说的话。 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: 您应该使用GL_ONE而不是GL_SRC_ALPHA ,并执行以下操作:

 
 
 
  
  
  
 
  glColor4f(1., 1., 1., myDesiredAlpha);
 

 
 
 
glColor4f(myDesiredAlpha, myDesiredAlpha, myDesiredAlpha, myDesiredAlpha);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM