1

我正在尝试在OpenGL中的另一个纹理上放置一个纹理(带有 alpha) 。我这样做没有任何问题,但这不是我想要的:我最近的 image的颜色受到背景图像(最远的纹理)的强烈影响,导致尽管是红色,但仍显示为橙色。

任何人都知道可以解决此问题的混合(或摆脱 alpha)方法吗?

混合初始化:

glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE);

绘图场景:

glClear(GL_COLOR_BUFFER_BIT);
glLoadIdentity();

//furthest image (background)
glBindTexture(GL_TEXTURE_2D, texture[1]);
glBegin(GL_QUADS);
    glTexCoord2f(0, 0); glVertex2f(0, 0);
    glTexCoord2f(1, 0); glVertex2f(500, 0);
    glTexCoord2f(1, 1); glVertex2f(500, 500);
    glTexCoord2f(0, 1); glVertex2f(0, 500);
glEnd();

//nearest image (appears orange, should be red) 
glBindTexture(GL_TEXTURE_2D, texture[0]);
glBegin(GL_QUADS);
    glTexCoord2f(0, 0); glVertex2f(100, 100);
    glTexCoord2f(1, 0); glVertex2f(300, 100);
    glTexCoord2f(1, 1); glVertex2f(300, 300);
    glTexCoord2f(0, 1); glVertex2f(100, 300);
glEnd();

glutSwapBuffers();

编辑。这是一张描述其外观的图像: 问题

这是它的外观图像: 目标

4

3 回答 3

2

I believe what you want is 'alpha testing', not blending. See

glEnable(GL_ALPHA_TEST)
glAlphaFunc()

If you want to leave blending enabled, you should use

glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); 

This will only use the source color in places where the source alpha is 1. Currently your function adds the source color to the background color.

If you don't want any mixing of colors, then using alpha test is the better way to go, as it uses less resources then blending.

于 2012-07-22T15:53:10.487 回答
2

这种混合功能

glBlendFunc(GL_SRC_ALPHA, GL_ONE);

是你的问题的原因。GL_ONEfor 目标意味着,无论 alpha 值如何,帧缓冲区中已经存在的任何内容都将添加到传入的颜色中。

在您的情况下,您的红色纹理会添加绿色背景。由于红色+绿色=橙色,这就是你得到的。

你想要的是用你的 alpha 通道屏蔽目标帧缓冲区中的先前内容,这是使用

glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

还要记住,OpenGL 状态是根据需要设置和重置的,因此在绘制其他纹理时,您可能需要其他设置来进行混合和混合功能。

于 2012-07-22T16:13:23.330 回答
0

在大家的帮助下,我设法通过以下方式解决了这个问题:

  • 将我的纹理重新保存为 PNG(尽管有 BMP)
  • 将混合功能更改为glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

感谢所有贡献者:)

于 2012-07-22T16:25:48.300 回答