Java OpenGL 混合图像颜色

发布于 2024-09-18 06:00:44 字数 1486 浏览 3 评论 0原文

我尝试通过以下方式将 2 个图像混合在一起:

图像 1 应绘制为基础图像。图像 2 应绘制在图像 1 之上。图像 2 在任何不透明的地方都应替换图像 1 的内容(不是混合,而是覆盖其中的内容)。只要图像 2 是透明的,图像 1 就应该显示出来。我尝试使用以下代码来执行此操作,但显然我在混合方面做错了一些事情。

            gl.glEnable(GL.GL_BLEND);
            if (iconTexture1 != null)
            {
                gl.glEnable(GL.GL_TEXTURE_2D);
                iconTexture1.bind();
                double red = (double) fillColor.getRed() / 255.0;
                double green = (double) fillColor.getGreen() / 255.0;
                double blue = (double) fillColor.getBlue() / 255.0;
                gl.glColor4d(red, green, blue, this.getOpacity());
                gl.glBlendFunc(GL.GL_SRC_ALPHA, GL.GL_ONE_MINUS_SRC_ALPHA);
                TextureCoords texCoords = iconTexture1.getImageTexCoords();
                gl.glScaled(width, height, 1d);
                dc.drawUnitQuad(texCoords);
            }
            if (iconTexture2 != null)
            {
                gl.glEnable(GL.GL_TEXTURE_2D);
                iconTexture2.bind();
                // image2 is all white, so color it here
                gl.glColor4d(1d, 0d, 0d, 1d);

                // TODO: What blend function should I be using here to allow image 2 to overwrite what is already there?

                TextureCoords texCoords = iconTexture2.getImageTexCoords();
                gl.glScaled(width, height, 1d);
                dc.drawUnitQuad(texCoords);
            }

任何使这项工作正常工作的帮助将不胜感激。谢谢。

杰夫

I'm trying to blend 2 images together in the following way:

Image 1 should be drawn as the base image. Image 2 should be drawn overtop of image 1. Anywhere image 2 is non-transparent, it should replace the contents of image 1 (not blend, but overwrite what is there). Wherever image 2 is transparent, image 1 should show through. I've tried to do this with the following code, but I'm obviously doing something incorrectly with the blending.

            gl.glEnable(GL.GL_BLEND);
            if (iconTexture1 != null)
            {
                gl.glEnable(GL.GL_TEXTURE_2D);
                iconTexture1.bind();
                double red = (double) fillColor.getRed() / 255.0;
                double green = (double) fillColor.getGreen() / 255.0;
                double blue = (double) fillColor.getBlue() / 255.0;
                gl.glColor4d(red, green, blue, this.getOpacity());
                gl.glBlendFunc(GL.GL_SRC_ALPHA, GL.GL_ONE_MINUS_SRC_ALPHA);
                TextureCoords texCoords = iconTexture1.getImageTexCoords();
                gl.glScaled(width, height, 1d);
                dc.drawUnitQuad(texCoords);
            }
            if (iconTexture2 != null)
            {
                gl.glEnable(GL.GL_TEXTURE_2D);
                iconTexture2.bind();
                // image2 is all white, so color it here
                gl.glColor4d(1d, 0d, 0d, 1d);

                // TODO: What blend function should I be using here to allow image 2 to overwrite what is already there?

                TextureCoords texCoords = iconTexture2.getImageTexCoords();
                gl.glScaled(width, height, 1d);
                dc.drawUnitQuad(texCoords);
            }

Any help to make this work correctly would be appreciated. Thanks.

Jeff

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

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

发布评论

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

评论(1

无人问我粥可暖 2024-09-25 06:00:44

有一些事情可能会出现问题:

  1. 在绘制图像 1 之前,不应打开混合功能。在此之前这样做会将图像 1 与已有的内容混合。
  2. 我不是一个大的纹理用户,但我认为使用纹理会覆盖四边形的颜色,包括您指定的任何 alpha;所以除非纹理有 Alpha,否则你不会得到任何混合。
  3. 如果启用了 z 缓冲,则图像 2 可能位于图像 1 的后面;即使图像 2 是透明的,这也会使其变得模糊。必须使用特殊方法来绘制透明 3D。

处理看似不起作用的 OpenGL 事物的一个好方法是消除所有复杂性,然后一点一点地将其添加回来。纹理是最复杂的部分 - 把它留到最后。

There are a few things that might be issues:

  1. You shouldn't turn the blending function on until after you have drawn image 1. Doing so before will blend image 1 with whatever was there already.
  2. I'm not a big texture user, but I think using texture will override the color of the quad, including any alpha you have specified; so unless the texture has alpha you won't get any blending.
  3. If you have z-buffering enabled, then maybe image 2 is behind image1; that would obscure it, even if image 2 is transparent. Special methods have to be used to draw transparent 3d.

A good way of working with OpenGL things that don't seem to work is to remove all the complexity, and then add it back bit by bit. The texture is your most complex part - leave that 'til last.

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