在 OpenGL 中正确绘制 Alpha 通道

发布于 2024-09-06 11:39:25 字数 1842 浏览 4 评论 0原文

加载图像后,我将每个通道的单独字节加载到无符号字符数组中。它被传递给一个函数,该函数将其作为纹理投影到四边形上。除了显示为背景颜色的 Alpha 通道之外,一切似乎都工作正常。我正在使用 OpenGL 来绘制图像。添加分层机制会让我受益吗?另外,怎样才能达到我想要的透明效果呢?

注意:这是我感觉需要更改的代码:

void SetUpView()
{
    // Set color and depth clear value
    glClearDepth(1.f);
    glClearColor(1.f, 0.f, 0.f, 0.f);

    // Enable Z-buffer read and write
    glEnable(GL_DEPTH_TEST);
    glEnable(GL_TEXTURE_2D);
    glDepthMask(GL_TRUE);

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

    // Setup a perspective projection
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(90.f, 1.f, 1.f, 500.f);
};

另外,这是渲染四边形的代码。

void DrawTexturedRect(RectBounds *Verts, Image *Texture)
{
    glBindTexture(GL_TEXTURE_2D, GetTextureID(Texture));

    glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
    glPixelZoom(1, -1);

    glBegin(GL_QUADS);
    glTexCoord2f(0.0, 1.0);
    glVertex3f(Verts->corners[0].x, Verts->corners[0].y, Verts->corners[0].z);

    glTexCoord2f(1.0, 1.0);
    glVertex3f(Verts->corners[1].x, Verts->corners[1].y, Verts->corners[1].z);

    glTexCoord2f(1.0, 0.0);
    glVertex3f(Verts->corners[2].x, Verts->corners[2].y, Verts->corners[2].z);

    glTexCoord2f(0.0, 0.0);
    glVertex3f(Verts->corners[3].x, Verts->corners[3].y, Verts->corners[3].z);
    glEnd();
};

Image 类包含使用 OpenIL 获得的无符号字符数组。

相关代码:

loaded = ilLoadImage(filename.c_str());
ilConvertImage(IL_RGBA, IL_UNSIGNED_BYTE);

unsigned char *bytes = ilGetData();

//NewImage is an instance of an Image. This is returned and passed to the above function.
NewImage->data = bytes;

“问题”

After loading an image, I have the individual bytes for each channel loaded into an array of unsigned characters. It's passed to a function that projects it as a texture onto a quad. Everything seems to work properly other than the alpha channel, which shows up as the background color. I'm using OpenGL to draw the image. Would I benefit by adding a layering mechanism? Also, how can I achieve the transparent effect that I want?

Note: This is the code that I have a feeling needs changed:

void SetUpView()
{
    // Set color and depth clear value
    glClearDepth(1.f);
    glClearColor(1.f, 0.f, 0.f, 0.f);

    // Enable Z-buffer read and write
    glEnable(GL_DEPTH_TEST);
    glEnable(GL_TEXTURE_2D);
    glDepthMask(GL_TRUE);

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

    // Setup a perspective projection
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(90.f, 1.f, 1.f, 500.f);
};

Also, here's the code to render the quad.

void DrawTexturedRect(RectBounds *Verts, Image *Texture)
{
    glBindTexture(GL_TEXTURE_2D, GetTextureID(Texture));

    glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
    glPixelZoom(1, -1);

    glBegin(GL_QUADS);
    glTexCoord2f(0.0, 1.0);
    glVertex3f(Verts->corners[0].x, Verts->corners[0].y, Verts->corners[0].z);

    glTexCoord2f(1.0, 1.0);
    glVertex3f(Verts->corners[1].x, Verts->corners[1].y, Verts->corners[1].z);

    glTexCoord2f(1.0, 0.0);
    glVertex3f(Verts->corners[2].x, Verts->corners[2].y, Verts->corners[2].z);

    glTexCoord2f(0.0, 0.0);
    glVertex3f(Verts->corners[3].x, Verts->corners[3].y, Verts->corners[3].z);
    glEnd();
};

The Image class holds an array of unsigned chars, obtained using OpenIL.

Relevant code:

loaded = ilLoadImage(filename.c_str());
ilConvertImage(IL_RGBA, IL_UNSIGNED_BYTE);

unsigned char *bytes = ilGetData();

//NewImage is an instance of an Image. This is returned and passed to the above function.
NewImage->data = bytes;

Problem

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

一张白纸 2024-09-13 11:39:26

在绘制任何透明对象之前,您应该调用:

glDepthMask(false);

然后调用:

glDepthMask(true);

另外,所有透明对象必须在所有不透明对象之后绘制。

Before drawing anything transparent you should call:

glDepthMask(false);

And then afterwards:

glDepthMask(true);

Also, all transparent objects must be drawn after all opaque ones.

祁梦 2024-09-13 11:39:26

glClearColor(1.f, 0.f, 0.f, 0.f);

我假设这是 RGBA 默认值,并且您将红色设置为 1.0。

对您想要实现的目标有一个更好的解释怎么样?

glClearColor(1.f, 0.f, 0.f, 0.f);

I'd assume that's at the RGBA default, and you're setting red to 1.0.

How about a better explanation of what you're trying to accomplish?

﹎☆浅夏丿初晴 2024-09-13 11:39:26

尝试:

glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

编辑:我知道那是什么样子。看起来您正在光标后面绘制蓝色方块(其 Z 顺序位置将其放置在光标后面)。当进行 Alpha 混合时,您必须以正确的、从后到前的 Z 顺序绘制内容,否则您会看到像您所看到的错误。

Try:

glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

Edit: I know what that looks like. It looks like you are drawing the blue square (Which has a Z-order position placing it behind the cursor) AFTER the cursor. You have to draw things in the correct, back-to-front Z-Order when alpha blending or you see errors like you are seeing.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文