【发布时间】:2012-08-15 12:09:25
【问题描述】:
我的 OpenGL 视图的 CAEAGLLayer 上的 opaque 属性设置为 NO。 OpenGL 视图(棋盘)后面有一个 UIImageView。最初在 OpenGL 视图(鸸鹋鸟照片)上绘制纹理。现在我想在成名缓冲区的中间绘制另一个纹理。新的纹理是完全黑色的,alpha 从上到下从 0 到 255 变化。
这是我的第二个纹理...
这就是我想要的……
这就是我得到的......
棋盘纹理是 EAGLView 后面的 UIImageView 中的 UIImage。
我不想干扰帧缓冲区中的 RGB 值,我只想写入 Alpha 通道。我试过了……
- glDisable(GL_BLEND) 和 glColorMask(0, 0, 0, 1)
- 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);
}
【问题讨论】:
-
我认为您的想法是正确的,但您应该尝试opengl.org/sdk/docs/man/xhtml/glBlendFuncSeparate.xml,使用其他参数 GL_SRC_ALPHA、GL_ONE_MINUS_SRC_ALPHA、GL_DST_ALPHA、GL_ONE_MINUS_DST_ALPHA 也适用于 rgb 通道。
标签: iphone ios opengl-es textures