将透明纹理与深度混合 [英] Blending transparent textures with depth

查看:23
本文介绍了将透明纹理与深度混合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试混合具有透明区域的纹理:

I am trying to blend textures which have transparent areas:

glEnable( GL_TEXTURE_2D );
glBindTexture( GL_TEXTURE_2D, ...);
glVertexPointer( 2, GL_FLOAT, 0, ... );
glEnable (GL_BLEND);
glBlendFunc (GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
glDrawArrays( GL_TRIANGLE_STRIP, 0, 4 );

除非我添加 glDisable(GL_DEPTH_TEST),否则顶部纹理的透明部分会覆盖它们下方的所有内容(而不是混合).有没有办法在不禁用深度的情况下做到这一点?我尝试了各种混合功能,但没有任何帮助.

Unless I add glDisable(GL_DEPTH_TEST), transparent parts of the top textures overwrite everything beneath them (instead of blending). Is there any way to do this without disabling depth? I have tried various blending functions but none of the helped.

推荐答案

启用深度测试实际上并不按深度对几何图形进行排序——在通常的 GL_LESS 情况下,它只是阻止基元绘制,如果它们并不比之前绘制的更靠近观察者.这允许您以您想要的任何顺序绘制不透明的几何体,并且仍然可以获得所需的结果,但是混合几何体的正确渲染通常需要混合对象后面的所有内容都已经被渲染.

Enabling depth test doesn’t actually sort your geometry by depth—in the usual GL_LESS case, it merely prevents primitives from drawing if they aren’t closer to the viewer than what has previously been drawn. This allows you to draw opaque geometry in whatever order you want and still get the desired result, but correct rendering of blended geometry typically requires everything behind the blended object to have already been rendered.

您应该执行以下操作以使不透明几何体和混合几何体的混合看起来正确:

Here’s what you should be doing to get a mix of opaque and blended geometry to look right:

  1. 将混合几何体与不透明几何体分离.
  2. 从后到前对混合几何进行排序.
  3. 像往常一样先绘制所有不透明的几何体.
  4. 按排序顺序绘制混合几何体.您需要启用深度测试,但使用 glDepthMask(GL_FALSE) 暂时禁用深度写入.
  1. Separate your blended geometry from your opaque geometry.
  2. Sort your blended geometry from back to front.
  3. Draw all the opaque geometry first as usual.
  4. Draw your blended geometry in sorted order. You’ll want to leave depth testing enabled but temporarily disable depth writes with glDepthMask(GL_FALSE).

或者,如果您的内容总是完全不透明或完全透明,您可以启用 Alpha 测试并禁用混合,但我猜这不是您想要的.

Alternately, if your content is always either fully opaque or fully transparent, you can just enable alpha testing and disable blending, but I’m guessing that’s not what you’re looking for.

这篇关于将透明纹理与深度混合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆