当我切换到仅 alpha (A8) 像素格式时,为什么我的片段着色器无法读取 alpha 信息?
我正在创建一个使用 OpenGL ES 2.0 的 iOS 应用程序。我对 OpenGL 有点陌生,所以这可能是一个微不足道的错误。
我创建了一个简单的着色器来使用 Alpha 通道处理用另一个纹理遮蔽一个纹理。蒙版纹理初始化如下:
glGenTextures(1, &名称); glBindTexture(GL_TEXTURE_2D, 名称);
glTexParameterf(GL_TEXTURE_2D, GL_GENERATE_MIPMAP, GL_TRUE); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR ); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 1024, 768, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0);
glGenFramebuffersOES(1, &buffer); glBindFramebufferOES(GL_FRAMEBUFFER_OES, 缓冲区);
glFramebufferTexture2DOES(GL_FRAMEBUFFER_OES, GL_COLOR_ATTACHMENT0_OES, GL_TEXTURE_2D, 名称, 0);
glBindFramebufferOES(GL_FRAMEBUFFER_OES, 0);
然后(稍后)我将一些内容绘制到蒙版纹理,并将蒙版纹理链接到该片段着色器以将其应用到另一个纹理:
统一sampler2D baseTextureSampler; 均匀采样器2D maskTextureSampler; 变化 lowp vec4 颜色变化; 变化mediump vec2纹理CoordsVarying; 变化mediump vec2位置变化; 无效主(){ lowp vec4 texel =texture2D(baseTextureSampler,textureCoordsVarying); lowp vec4 maskTexel =texture2D(maskTextureSampler,positionVarying); gl_FragColor = texel*colorVarying*maskTexel.a; }
这完全按照我想要的方式呈现。然而,为了减少绑定蒙版纹理缓冲区的开销(这似乎很大),我尝试对蒙版纹理使用 8 位仅 alpha 像素格式。但是,如果我更改 RGBA 设置中的代码:
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 1024, 768, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0);
...仅限 alpha:
glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, 1024, 768, 0, GL_ALPHA, GL_UNSIGNED_BYTE, 缓冲区);
...片段着色器无法绘制任何内容。由于我一开始只使用蒙版纹理中的 Alpha,因此它似乎应该继续有效。有什么想法为什么不这样做吗?
I'm creating an iOS app that uses OpenGL ES 2.0. I'm somewhat new to OpenGL, so this may be a trivial mistake.
I've created a simple shader to handle masking one texture with another, using the alpha channel. The mask texture initialized like so:
glGenTextures(1, &name);
glBindTexture(GL_TEXTURE_2D, name);glTexParameterf(GL_TEXTURE_2D, GL_GENERATE_MIPMAP, GL_TRUE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE );glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 1024, 768, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0);
glGenFramebuffersOES(1, &buffer);
glBindFramebufferOES(GL_FRAMEBUFFER_OES, buffer);glFramebufferTexture2DOES(GL_FRAMEBUFFER_OES, GL_COLOR_ATTACHMENT0_OES, GL_TEXTURE_2D, name, 0);
glBindFramebufferOES(GL_FRAMEBUFFER_OES, 0);
I then (later) draw some stuff to the mask texture, and link the mask texture to this fragment shader to apply it to another texture:
uniform sampler2D baseTextureSampler; uniform sampler2D maskTextureSampler; varying lowp vec4 colorVarying; varying mediump vec2 textureCoordsVarying; varying mediump vec2 positionVarying; void main() { lowp vec4 texel = texture2D(baseTextureSampler, textureCoordsVarying); lowp vec4 maskTexel = texture2D(maskTextureSampler, positionVarying); gl_FragColor = texel*colorVarying*maskTexel.a; }
This renders exactly how I want it to. However, to reduce the overhead in binding the mask texture's buffer (which seems substantial) I'm trying to use an 8-bit alpha-only pixel format for the mask texture. BUT, if I change the code from the RGBA setup:
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 1024, 768, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0);
...to alpha-only:
glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, 1024, 768, 0, GL_ALPHA, GL_UNSIGNED_BYTE, buffer);
...the fragment shader fails to draw anything. Since I'm only using the alpha from the mask texture in the first place, it seems like it should continue to work. Any ideas why it doesn't?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
OpenGL ES 2.0 规范(或 GL_OES_framebuffer_object 扩展)没有定义任何仅包含 alpha 位的可渲染格式,因此不支持它们。您必须使用 RGBA 格式。
The OpenGL ES 2.0 spec (or the GL_OES_framebuffer_object extension) doesn't define any renderable formats with only alpha bits, so they are NOT supported. You will have to use a RGBA format.