我正在使用 OpenFrameworks 和 OpenGL ES 1.1 开发 iPad 应用程序。我需要显示带有 Alpha channel 的视频。为了模拟它,我有一个 RGB 视频(没有任何 Alpha channel )和另一个仅包含 Alpha channel 的视频(在每个 RGB channel 上,因此白色部分对应于可见部分,黑色部分对应于不可见部分)。每个视频都是一个 OpenGL 纹理。
在 OpenGL ES 1.1 中没有着色器,所以我找到了这个解决方案(此处:OpenGL - mask with multiple textures):
glEnable(GL_BLEND);
// Use a simple blendfunc for drawing the background
glBlendFunc(GL_ONE, GL_ZERO);
// Draw entire background without masking
drawQuad(backgroundTexture);
// Next, we want a blendfunc that doesn't change the color of any pixels,
// but rather replaces the framebuffer alpha values with values based
// on the whiteness of the mask. In other words, if a pixel is white in the mask,
// then the corresponding framebuffer pixel's alpha will be set to 1.
glBlendFuncSeparate(GL_ZERO, GL_ONE, GL_SRC_COLOR, GL_ZERO);
// Now "draw" the mask (again, this doesn't produce a visible result, it just
// changes the alpha values in the framebuffer)
drawQuad(maskTexture);
// Finally, we want a blendfunc that makes the foreground visible only in
// areas with high alpha.
glBlendFunc(GL_DST_ALPHA, GL_ONE_MINUS_DST_ALPHA);
drawQuad(foregroundTexture);
这正是我想要做的,但是 glBlendFuncSeparate() 在 OpenGL ES 1.1(或 iOS)中不存在。我试图用 glColorMask 来做到这一点,我发现了这个:Can't get masking to work correctly with OpenGL
但它不起作用,我猜是因为他的蒙版纹理文件包含一个“真正的”Alpha channel ,而不是我的。
最佳答案
我强烈建议您改为计算单个 RGBA 纹理。
这会更容易、更快(因为您每帧发送 2 个 RGBA 纹理 - 是的,您的 RGB 纹理实际上由硬件以 RGBA 进行编码,并且 A 被忽略)
glColorMask 不会帮助你,因为它只是说“完全打开或关闭这个 channel ”。
如果你有的话,glBlendFuncSeparate 可以帮助你,但同样,这不是一个好的解决方案:你发送两倍于所需数据的数据会破坏你的(非常有限的)iPhone 带宽。
更新:
由于您使用的是 OpenFrameworks,并且根据其源代码( https://github.com/openframeworks/openFrameworks/blob/master/libs/openFrameworks/gl/ofTexture.cpp 和 https://github.com/openframeworks/openFrameworks/blob/master/libs/openFrameworks/video/ofVideoPlayer.cpp ):
- 使用ofVideoPlayer::setUseTexture(false)使得ofVideoPlayer::update不会将数据上传到显存;
- 使用 ofVideoPlayer::getPixels 获取视频数据
- 将结果交错在 RGBA 纹理中(您可以使用 GL_RGBA ofTexture 和 ofTexture::loadData)
- 使用 ofTexture::Draw 进行绘制(这就是 ofVideoPlayer 所做的事情)
关于ios - OpenGL ES 1.1 - Alpha 蒙版,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11666825/