将透明纹理与深度混合

编程入门 行业动态 更新时间:2024-10-28 00:24:20
本文介绍了将透明纹理与深度混合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

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:

  • 将混合几何体与不透明几何体分离.
  • 从后到前对混合几何进行排序.
  • 像往常一样先绘制所有不透明的几何体.
  • 按排序顺序绘制混合几何体.您需要启用深度测试,但使用 glDepthMask(GL_FALSE) 暂时禁用深度写入.
  • Separate your blended geometry from your opaque geometry.
  • Sort your blended geometry from back to front.
  • Draw all the opaque geometry first as usual.
  • 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.

    更多推荐

    将透明纹理与深度混合

    本文发布于:2023-06-01 10:53:24,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/413443.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:纹理   深度   透明

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!