0

我并没有真正glBlendFunc()了解该功能及其工作原理。

是否有一种混合模式可以让目标黑色变为黑色,同时覆盖任何其他 dst 颜色?

我有一个包含着色边缘和形状的黑白画布。我想在画布上的任何地方画一个画笔,但不要覆盖边缘,这样一旦图像完全着色,它们就会保持黑色。理想情况下,由于目标边缘周围有一些灰色以保持平滑,灰色阴影将被画笔颜色着色。

谢谢!

4

1 回答 1

0

我相信你需要的是glBlendFunc(GL_DST_COLOR, GL_ZERO).

向您解释混合功能的工作原理:混合时,您有 2 种颜色,源和目标。来源是您尝试应用的颜色(文本,纯色......)。Destination 是缓冲区中当前的颜色。中的 2 个参数glBlendFunc告诉如何在将它们相加以获得结果颜色之前将源颜色和目标颜色相乘。

因此,在glBlendFunc(GL_DST_COLOR, GL_ZERO)具有黑白目标缓冲区的情况下(如您所描述的)将像这样:

  • 如果目标颜色是白色 (1,1,1,1) 并且您将应用一些颜色,例如 (.1, .2, .3, 1),结果将是R = (.1, .2, .3, 1)*(1,1,1,1) + (1,1,1,1)*(0,0,0,0) = (.1, .2, .3, 1)
  • 如果目标颜色是黑色 (0,0,0,0) 并且您将应用一些颜色,例如 (.1, .2, .3, 1),结果将是R = (.1, .2, .3, 1)*(0,0,0,0) + (0,0,0,0)*(0,0,0,0) = (0, 0, 0, 0)
  • 如果目标颜色是灰色 (.5,.5,.5,1.0) 并且您将应用一些颜色,例如 (.1, .2, .3, 1),结果将是R = (.1, .2, .3, 1)*(.5,.5,.5,1.0) + (.5,.5,.5,1.0)*(0,0,0,0) = (.05, .1, .15, 1)

您的这种情况有点具体,但在大多数情况下,仅使用 alpha 通道。例如,最常见的混合glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)意味着源颜色将与源 alpha 相乘,而目标颜色将与 1.0 源 alpha 相乘,因此在灰色背景 RGB 0.5(任何 alpha)上的 alpha 为 0.75 的白色形状将导致 RGB all 1.0*.75 + .5*(1.0-.75) = .75 + .125 = .875

Note that this procedure of yours might cost you if you will need some other blending type on the overlay. In that case you will need to look into stencil buffers. Another issue is you might not want to see the black and white part on places you do not draw the overlay. In this case you can simply use the destination alpha to do this blending for you: glColorMask can help you draw only to the alpha channel of your buffer where you can use alpha .0 for "black" areas and 1.0 for "white" areas, when drawing a shape over the buffer you would then use glBlendFunc(GL_DST_ALPHA, GL_ZERO).

于 2014-06-12T07:09:40.573 回答