【问题标题】:Drawing to the alpha channel of the frame buffer on with OpenGL while ignoring RGB values?在忽略 RGB 值的情况下使用 OpenGL 绘制到帧缓冲区的 alpha 通道?
【发布时间】:2012-08-15 12:09:25
【问题描述】:

我的 OpenGL 视图的 CAEAGLLayer 上的 opaque 属性设置为 NO。 OpenGL 视图(棋盘)后面有一个 UIImageView。最初在 OpenGL 视图(鸸鹋鸟照片)上绘制纹理。现在我想在成名缓冲区的中间绘制另一个纹理。新的纹理是完全黑色的,alpha 从上到下从 0 到 255 变化。

这是我的第二个纹理...

这就是我想要的……

这就是我得到的......

棋盘纹理是 EAGLView 后面的 UIImageView 中的 UIImage。

我不想干扰帧缓冲区中的 RGB 值,我只想写入 Alpha 通道。我试过了……

  1. glDisable(GL_BLEND) 和 glColorMask(0, 0, 0, 1)
  2. glEnable(GL_BLEND) 和 glBlendFuncSeparate(GL_ZERO, GL_ONE, GL_ONE, GL_ZERO)

似乎没有任何效果。 RGB 值总是被修改,像素变得更亮。

如果源 RGBA 是 (r2, g2, b2, a2),目标是 (r1, g1, b1, a1)。我希望最终值为 (r1, g1, b1, a2) 或最好是 (r1, g1, b1, some_function_of(a1, a2))。我如何做到这一点?

这是我的代码...

用于绘制第二个纹理的 Objective C 代码...

- (void) drawTexture {
    GLfloat textureCoordinates[] = {
        0.0f, 1.0f,
        1.0f, 1.0f,
        0.0f,  0.0f,
        1.0f,  0.0f,
    };

    static const GLfloat imageVertices[] = {
        -0.5f, -0.5f,
        0.5f, -0.5f,
        -0.5f,  0.5f,
        0.5f,  0.5f,
    };

    [EAGLContext setCurrentContext:context];

    glViewport(0, 0, backingWidth, backingHeight);

    glUseProgram(program);

    glActiveTexture(GL_TEXTURE2);
    glBindTexture(GL_TEXTURE_2D, texture);

    glEnable (GL_BLEND);
    glBlendFuncSeparate(GL_ZERO, GL_ONE, GL_ONE, GL_ZERO);

    glUniform1i(inputImageTexture, 2);

    glVertexAttribPointer(position, 2, GL_FLOAT, 0, 0, imageVertices);
    glVertexAttribPointer(inputTextureCoordinate, 2, GL_FLOAT, 0, 0, textureCoordinates);

    glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);

    glBindRenderbuffer(GL_RENDERBUFFER, viewRenderbuffer);
    [context presentRenderbuffer:GL_RENDERBUFFER];
}

顶点着色器...

 attribute vec4 position;
 attribute vec4 inputTextureCoordinate;

 varying vec2 textureCoordinate;

 void main()
 {
    gl_Position = position;
    textureCoordinate = inputTextureCoordinate.xy;
 }

片段着色器...

 varying highp vec2 textureCoordinate;

 uniform sampler2D inputImageTexture;

 void main()
 {
     gl_FragColor = texture2D(inputImageTexture, textureCoordinate);
 }

【问题讨论】:

标签: iphone ios opengl-es textures


【解决方案1】:

glColorMask 必须工作 - 在我的情况下它对我有用。另外,据我所知,glBlendFuncSeparate 在 iOS 上也有问题。

(理论上)您可以执行以下操作:

  • 将混合设置为乘法模式(GL_DST_COLOR、GL_ONE_MINUS_SRC_ALPHA)
  • 让你的纹理变白
  • glColorMask 设置为启用的所有频道

如果你有 iOS 5+ 的 iDevice,你可以在 XCode 的 OpenGL Capture 窗口中查看 framebuffer alpha。

【讨论】:

    猜你喜欢
    • 2011-11-24
    • 2011-06-18
    • 2021-09-02
    • 1970-01-01
    • 2012-06-25
    • 1970-01-01
    • 1970-01-01
    • 2017-01-25
    • 2014-01-16
    相关资源
    最近更新 更多