为什么 alpha 混合在正交中不起作用?
这就是我在绘制 3D 对象后打开正交投影的方法:
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0,Screen_Width,Screen_Height,0,0,1);
这就是我在绘制 3D 对象后打开混合并在正交中绘制纹理的方法:
glEnable(GL_TEXTURE_2D);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
glColor4f(1,1,1,1);
glBindTexture(GL_TEXTURE_2D,Texture1);
glBegin(GL_QUADS);
//draw 1st quad
glEnd();
glBindTexture(GL_TEXTURE_2D,Texture2);
glBegin(GL_QUADS);
//draw 2nd quad
glEnd();
glDisable(GL_BLEND);
glDisable(GL_TEXTURE_2D);
Quad1 比 Quad2 稍大,并且覆盖了 Quad2 的某些部分。两种纹理都具有 RGBA 形式的 Alpha 通道。
问题是,Quad1 & Quad2 使用 Alpha 正确覆盖 3D 对象,但 Quad1 的 Alpha 在 Quad2 之上时不起作用。它仅像 RGB 一样在 Quad2 之上绘制。
我该如何解决这个问题?
This is how I turn on ortho projection after drawing 3D objects:
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0,Screen_Width,Screen_Height,0,0,1);
And this is how I turn on blending and draw textures in ortho after drawing 3D objects:
glEnable(GL_TEXTURE_2D);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
glColor4f(1,1,1,1);
glBindTexture(GL_TEXTURE_2D,Texture1);
glBegin(GL_QUADS);
//draw 1st quad
glEnd();
glBindTexture(GL_TEXTURE_2D,Texture2);
glBegin(GL_QUADS);
//draw 2nd quad
glEnd();
glDisable(GL_BLEND);
glDisable(GL_TEXTURE_2D);
Quad1 is a bit larger than Quad2 and covers some parts of Quad2. Both textures have alpha channel as RGBA.
The problem is, Quad1 & Quad2 overlay the 3D objects with alpha correctly, but the alpha of Quad1 doesn't work when being on top of Quad2. It draws on top of Quad2 like RGB only.
How could I solve this problem?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是否启用深度缓冲?如果是,则可能会导致片段被丢弃(Z 测试失败),而不是与已有的片段进行 alpha 混合。
如果您使用深度缓冲区,则需要按从后到前的顺序绘制透明对象。
对于纯 2D 场景,您甚至可能不需要深度缓冲区,您可以将其关闭:
Is depth buffering enabled? If it is, that could cause fragments to be discarded (failing the Z test) instead of alpha-blended with what's already there.
If you're using a depth buffer, you need to draw transparent objects in back-to-front order.
For a purely 2D scene, you might not even need a depth buffer, and you can just turn it off: